AbstractSingleStringValueFormField.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 java.net.URL;
  19. import java.util.Date;

  20. import org.jxmpp.util.XmppDateTime;

  21. public class AbstractSingleStringValueFormField extends SingleValueFormField {

  22.     private final String value;

  23.     protected AbstractSingleStringValueFormField(Builder<?, ?> builder) {
  24.         super(builder);
  25.         value = builder.value;
  26.     }

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

  31.     public final Integer getValueAsInt() {
  32.         if (value == null) {
  33.             return null;
  34.         }
  35.         return Integer.valueOf(value);
  36.     }

  37.     public abstract static class Builder<F extends SingleValueFormField, B extends SingleValueFormField.Builder<F, B>>
  38.                     extends SingleValueFormField.Builder<F, B> {

  39.         private String value;

  40.         protected Builder(AbstractSingleStringValueFormField abstractSingleFormField) {
  41.             super(abstractSingleFormField);
  42.             value = abstractSingleFormField.getValue();
  43.         }

  44.         protected Builder(String fieldName, FormField.Type type) {
  45.             super(fieldName, type);
  46.         }

  47.         @Override
  48.         protected void resetInternal() {
  49.             value = null;
  50.         }

  51.         /**
  52.          * Set the value.
  53.          *
  54.          * @param value the value to set.
  55.          * @return a reference to this builder.
  56.          * @deprecated use {@link #setValue(CharSequence)} instead.
  57.          */
  58.         @Deprecated
  59.         // TODO: Remove in Smack 4.6.
  60.         public B addValue(CharSequence value) {
  61.             return setValue(value);
  62.         }

  63.         public B setValue(Value value) {
  64.             this.value = value.getValue().toString();
  65.             this.rawValue = value;
  66.             return getThis();
  67.         }

  68.         public B setValue(CharSequence value) {
  69.             this.value = value.toString();
  70.             rawValue = new Value(this.value);
  71.             return getThis();
  72.         }

  73.         public B setValue(Enum<?> value) {
  74.             return setValue(value.toString());
  75.         }

  76.         public B setValue(int value) {
  77.             return setValue(Integer.toString(value));
  78.         }

  79.         public B setValue(URL value) {
  80.             return setValue(value.toString());
  81.         }

  82.         public B setValue(Date date) {
  83.             String dateString = XmppDateTime.formatXEP0082Date(date);
  84.             return setValue(dateString);
  85.         }
  86.     }
  87. }