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 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 SingleValueFormField, B extends SingleValueFormField.Builder<F, B>> 046 extends SingleValueFormField.Builder<F, B> { 047 048 private String value; 049 050 protected Builder(AbstractSingleStringValueFormField abstractSingleFormField) { 051 super(abstractSingleFormField); 052 value = abstractSingleFormField.getValue(); 053 } 054 055 protected Builder(String fieldName, FormField.Type type) { 056 super(fieldName, type); 057 } 058 059 @Override 060 protected void resetInternal() { 061 value = null; 062 } 063 064 /** 065 * Set the value. 066 * 067 * @param value the value to set. 068 * @return a reference to this builder. 069 * @deprecated use {@link #setValue(CharSequence)} instead. 070 */ 071 @Deprecated 072 // TODO: Remove in Smack 4.6. 073 public B addValue(CharSequence value) { 074 return setValue(value); 075 } 076 077 public B setValue(Value value) { 078 this.value = value.getValue().toString(); 079 this.rawValue = value; 080 return getThis(); 081 } 082 083 public B setValue(CharSequence value) { 084 this.value = value.toString(); 085 rawValue = new Value(this.value); 086 return getThis(); 087 } 088 089 public B setValue(Enum<?> value) { 090 return setValue(value.toString()); 091 } 092 093 public B setValue(int value) { 094 return setValue(Integer.toString(value)); 095 } 096 097 public B setValue(URL value) { 098 return setValue(value.toString()); 099 } 100 101 public B setValue(Date date) { 102 String dateString = XmppDateTime.formatXEP0082Date(date); 103 return setValue(dateString); 104 } 105 } 106}