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    public Boolean getValueAsBoolean() {
039        return value;
040    }
041
042    public Builder asBuilder() {
043        return new Builder(this);
044    }
045
046    public static final class Builder extends SingleValueFormField.Builder<BooleanFormField, BooleanFormField.Builder> {
047        private Boolean value;
048
049        private Builder(BooleanFormField booleanFormField) {
050            super(booleanFormField);
051            value = booleanFormField.value;
052        }
053
054        Builder(String fieldName) {
055            super(fieldName, FormField.Type.bool);
056        }
057
058        @Override
059        protected void resetInternal() {
060            super.resetInternal();
061            value = null;
062        }
063
064        /**
065         * Set the value.
066         *
067         * @param value the value to set.
068         * @return a reference to this builder.
069         * @deprecated use {@link #setValue(CharSequence)} instead.
070         */
071        @Deprecated
072        // TODO: Remove in Smack 4.6.
073        public Builder addValue(CharSequence value) {
074            return setValue(new Value(value));
075        }
076
077        public Builder setValue(CharSequence value) {
078            return setValue(new Value(value));
079        }
080
081        public Builder setValue(Value value) {
082            this.value = ParserUtils.parseXmlBoolean(value.getValue().toString());
083            rawValue = value;
084            return getThis();
085        }
086
087        public Builder setValue(boolean value) {
088            rawValue = new Value(Boolean.toString(value));
089            this.value = value;
090            return this;
091        }
092
093        @Override
094        public BooleanFormField build() {
095            return new BooleanFormField(this);
096        }
097
098        @Override
099        public Builder getThis() {
100            return this;
101        }
102
103    }
104
105}