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.jxmpp.jid.Jid;
020import org.jxmpp.jid.impl.JidCreate;
021import org.jxmpp.stringprep.XmppStringprepException;
022
023public class JidSingleFormField extends SingleValueFormField {
024
025    private final Jid value;
026
027    protected JidSingleFormField(Builder builder) {
028        super(builder);
029        value = builder.value;
030    }
031
032    @Override
033    public Jid getValue() {
034        return value;
035    }
036
037    public Builder asBuilder() {
038        return new Builder(this);
039    }
040
041    public static final class Builder extends SingleValueFormField.Builder<JidSingleFormField, JidSingleFormField.Builder> {
042        private Jid value;
043
044        private Builder(JidSingleFormField jidSingleFormField) {
045            super(jidSingleFormField);
046            value = jidSingleFormField.getValue();
047        }
048
049        Builder(String fieldName) {
050            super(fieldName, FormField.Type.jid_single);
051        }
052
053        @Override
054        protected void resetInternal() {
055            super.resetInternal();
056            value = null;
057        }
058
059        public Builder setValue(Jid value) {
060            this.value = value;
061            this.rawValue = new Value(value);
062            return getThis();
063        }
064
065        public Builder setValue(Value value) throws XmppStringprepException {
066            this.value = JidCreate.from(value.getValue());
067            this.rawValue = value;
068            return this;
069        }
070
071        @Override
072        public JidSingleFormField build() {
073            return new JidSingleFormField(this);
074        }
075
076        @Override
077        public Builder getThis() {
078            return this;
079        }
080    }
081}