JidSingleFormField.java

  1. /**
  2.  *
  3.  * Copyright 2020-2021 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.xdata;

  18. import org.jxmpp.jid.Jid;
  19. import org.jxmpp.jid.impl.JidCreate;
  20. import org.jxmpp.stringprep.XmppStringprepException;

  21. public class JidSingleFormField extends SingleValueFormField {

  22.     private final Jid value;

  23.     protected JidSingleFormField(Builder builder) {
  24.         super(builder);
  25.         value = builder.value;
  26.     }

  27.     @Override
  28.     public Jid getValue() {
  29.         return value;
  30.     }

  31.     public Builder asBuilder() {
  32.         return new Builder(this);
  33.     }

  34.     public static final class Builder extends SingleValueFormField.Builder<JidSingleFormField, JidSingleFormField.Builder> {
  35.         private Jid value;

  36.         private Builder(JidSingleFormField jidSingleFormField) {
  37.             super(jidSingleFormField);
  38.             value = jidSingleFormField.getValue();
  39.         }

  40.         Builder(String fieldName) {
  41.             super(fieldName, FormField.Type.jid_single);
  42.         }

  43.         @Override
  44.         protected void resetInternal() {
  45.             super.resetInternal();
  46.             value = null;
  47.         }

  48.         public Builder setValue(Jid value) {
  49.             return setValue(value, null);
  50.         }

  51.         public Builder setValue(Jid value, String rawValue) {
  52.             this.value = value;
  53.             this.rawValue = new Value(value);
  54.             return getThis();
  55.         }

  56.         public Builder setValue(Value value) throws XmppStringprepException {
  57.             this.value = JidCreate.from(value.getValue());
  58.             this.rawValue = value;
  59.             return this;
  60.         }

  61.         @Override
  62.         public JidSingleFormField build() {
  63.             return new JidSingleFormField(this);
  64.         }

  65.         @Override
  66.         public Builder getThis() {
  67.             return this;
  68.         }
  69.     }
  70. }