001/**
002 *
003 * Copyright 2020-2021 Florian Schmaus.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.xdata;
018
019import org.jivesoftware.smack.util.ParserUtils;
020
021public class BooleanFormField extends SingleValueFormField {
022
023    private final Boolean value;
024
025    protected BooleanFormField(Builder builder) {
026        super(builder);
027        value = builder.value;
028    }
029
030    @Override
031    public String getValue() {
032        if (value == null) {
033            return null;
034        }
035        return value.toString();
036    }
037
038    /**
039     * Get the value of the booelan field. Note that, if no explicit boolean value is provided, in the form of "true",
040     * "false", "0", or "1", then the default value of a boolean field is <code>false</code>, according to
041     * XEP-0004 ยง 3.3.
042     *
043     * @return the boolean value of this form field.
044     */
045    public boolean getValueAsBoolean() {
046        if (value == null) {
047            return false;
048        }
049        return value;
050    }
051
052    /**
053     * Get the value of the boolean field or maybe <code>null</code>. Note that you usually want to use
054     * {@link #getValueAsBoolean()} instead of this method, as {@link #getValueAsBoolean()} considers the default value
055     * of boolean fields. That is, boolean form fields have the value <code>false</code> if not explicitly set to
056     * something else.
057     *
058     * @return the boolean value of this form field or <code>null</code> if no value was explicitly provided.
059     * @see #getValueAsBoolean()
060     * @since 4.4.5
061     */
062    public Boolean getValueAsBooleanOrNull() {
063        return value;
064    }
065
066    public Builder asBuilder() {
067        return new Builder(this);
068    }
069
070    public static final class Builder extends SingleValueFormField.Builder<BooleanFormField, BooleanFormField.Builder> {
071        private Boolean value;
072
073        private Builder(BooleanFormField booleanFormField) {
074            super(booleanFormField);
075            value = booleanFormField.value;
076        }
077
078        Builder(String fieldName) {
079            super(fieldName, FormField.Type.bool);
080        }
081
082        @Override
083        protected void resetInternal() {
084            super.resetInternal();
085            value = null;
086        }
087
088        /**
089         * Set the value.
090         *
091         * @param value the value to set.
092         * @return a reference to this builder.
093         * @deprecated use {@link #setValue(CharSequence)} instead.
094         */
095        @Deprecated
096        // TODO: Remove in Smack 4.6.
097        public Builder addValue(CharSequence value) {
098            return setValue(new Value(value));
099        }
100
101        public Builder setValue(CharSequence value) {
102            return setValue(new Value(value));
103        }
104
105        public Builder setValue(Value value) {
106            this.value = ParserUtils.parseXmlBoolean(value.getValue().toString());
107            rawValue = value;
108            return getThis();
109        }
110
111        public Builder setValue(boolean value) {
112            rawValue = new Value(Boolean.toString(value));
113            this.value = value;
114            return this;
115        }
116
117        @Override
118        public BooleanFormField build() {
119            return new BooleanFormField(this);
120        }
121
122        @Override
123        public Builder getThis() {
124            return this;
125        }
126
127    }
128
129}