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            return setValue(value, null);
061        }
062
063        public Builder setValue(Jid value, String rawValue) {
064            this.value = value;
065            this.rawValue = new Value(value);
066            return getThis();
067        }
068
069        public Builder setValue(Value value) throws XmppStringprepException {
070            this.value = JidCreate.from(value.getValue());
071            this.rawValue = value;
072            return this;
073        }
074
075        @Override
076        public JidSingleFormField build() {
077            return new JidSingleFormField(this);
078        }
079
080        @Override
081        public Builder getThis() {
082            return this;
083        }
084    }
085}