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 java.net.URL; 020import java.util.Date; 021 022import org.jxmpp.util.XmppDateTime; 023 024public class AbstractSingleStringValueFormField extends SingleValueFormField { 025 026 private final String value; 027 028 protected AbstractSingleStringValueFormField(Builder<?, ?> builder) { 029 super(builder); 030 value = builder.value; 031 } 032 033 @Override 034 public final String getValue() { 035 return value; 036 } 037 038 public final Integer getValueAsInt() { 039 if (value == null) { 040 return null; 041 } 042 return Integer.valueOf(value); 043 } 044 045 public abstract static class Builder<F extends FormField, B extends FormField.Builder<F, B>> extends FormField.Builder<F, B> { 046 047 private String value; 048 049 protected Builder(AbstractSingleStringValueFormField abstractSingleFormField) { 050 super(abstractSingleFormField); 051 value = abstractSingleFormField.getValue(); 052 } 053 054 protected Builder(String fieldName, FormField.Type type) { 055 super(fieldName, type); 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 B addValue(CharSequence value) { 073 return setValue(value); 074 } 075 076 public B setValue(CharSequence value) { 077 this.value = value.toString(); 078 return getThis(); 079 } 080 081 public B setValue(Enum<?> value) { 082 this.value = value.toString(); 083 return getThis(); 084 } 085 086 public B setValue(int value) { 087 this.value = Integer.toString(value); 088 return getThis(); 089 } 090 091 public B setValue(URL value) { 092 return setValue(value.toString()); 093 } 094 095 public B setValue(Date date) { 096 String dateString = XmppDateTime.formatXEP0082Date(date); 097 return setValue(dateString); 098 } 099 } 100}