001/**
002 *
003 * Copyright 2020 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 FormField.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            value = null;
061        }
062
063        /**
064         * Set the value.
065         *
066         * @param value the value to set.
067         * @return a reference to this builder.
068         * @deprecated use {@link #setValue(CharSequence)} instead.
069         */
070        @Deprecated
071        // TODO: Remove in Smack 4.6.
072        public Builder addValue(CharSequence value) {
073            return setValue(value);
074        }
075
076        public Builder setValue(CharSequence value) {
077            boolean valueBoolean = ParserUtils.parseXmlBoolean(value.toString());
078            return setValue(valueBoolean);
079        }
080
081        public Builder setValue(boolean value) {
082            this.value = value;
083            return this;
084        }
085
086        @Override
087        public BooleanFormField build() {
088            return new BooleanFormField(this);
089        }
090
091        @Override
092        public Builder getThis() {
093            return this;
094        }
095
096    }
097
098}