BooleanFormField.java

  1. /**
  2.  *
  3.  * Copyright 2020-2021 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.xdata;

  18. import org.jivesoftware.smack.util.ParserUtils;

  19. public class BooleanFormField extends SingleValueFormField {

  20.     private final Boolean value;

  21.     protected BooleanFormField(Builder builder) {
  22.         super(builder);
  23.         value = builder.value;
  24.     }

  25.     @Override
  26.     public String getValue() {
  27.         if (value == null) {
  28.             return null;
  29.         }
  30.         return value.toString();
  31.     }

  32.     /**
  33.      * Get the value of the boolean field. Note that, if no explicit boolean value is provided, in the form of "true",
  34.      * "false", "0", or "1", then the default value of a boolean field is <code>false</code>, according to
  35.      * XEP-0004 ยง 3.3.
  36.      *
  37.      * @return the boolean value of this form field.
  38.      */
  39.     public boolean getValueAsBoolean() {
  40.         if (value == null) {
  41.             return false;
  42.         }
  43.         return value;
  44.     }

  45.     /**
  46.      * Get the value of the boolean field or maybe <code>null</code>. Note that you usually want to use
  47.      * {@link #getValueAsBoolean()} instead of this method, as {@link #getValueAsBoolean()} considers the default value
  48.      * of boolean fields. That is, boolean form fields have the value <code>false</code> if not explicitly set to
  49.      * something else.
  50.      *
  51.      * @return the boolean value of this form field or <code>null</code> if no value was explicitly provided.
  52.      * @see #getValueAsBoolean()
  53.      * @since 4.4.5
  54.      */
  55.     public Boolean getValueAsBooleanOrNull() {
  56.         return value;
  57.     }

  58.     public Builder asBuilder() {
  59.         return new Builder(this);
  60.     }

  61.     public static final class Builder extends SingleValueFormField.Builder<BooleanFormField, BooleanFormField.Builder> {
  62.         private Boolean value;

  63.         private Builder(BooleanFormField booleanFormField) {
  64.             super(booleanFormField);
  65.             value = booleanFormField.value;
  66.         }

  67.         Builder(String fieldName) {
  68.             super(fieldName, FormField.Type.bool);
  69.         }

  70.         @Override
  71.         protected void resetInternal() {
  72.             super.resetInternal();
  73.             value = null;
  74.         }

  75.         /**
  76.          * Set the value.
  77.          *
  78.          * @param value the value to set.
  79.          * @return a reference to this builder.
  80.          * @deprecated use {@link #setValue(CharSequence)} instead.
  81.          */
  82.         @Deprecated
  83.         // TODO: Remove in Smack 4.6.
  84.         public Builder addValue(CharSequence value) {
  85.             return setValue(new Value(value));
  86.         }

  87.         public Builder setValue(CharSequence value) {
  88.             return setValue(new Value(value));
  89.         }

  90.         public Builder setValue(Value value) {
  91.             this.value = ParserUtils.parseXmlBoolean(value.getValue().toString());
  92.             rawValue = value;
  93.             return getThis();
  94.         }

  95.         public Builder setValue(boolean value) {
  96.             rawValue = new Value(Boolean.toString(value));
  97.             this.value = value;
  98.             return this;
  99.         }

  100.         @Override
  101.         public BooleanFormField build() {
  102.             return new BooleanFormField(this);
  103.         }

  104.         @Override
  105.         public Builder getThis() {
  106.             return this;
  107.         }

  108.     }

  109. }