001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim.sdk;
019
020import javax.xml.bind.DatatypeConverter;
021import java.util.Calendar;
022import java.util.Date;
023import java.util.GregorianCalendar;
024import java.util.TimeZone;
025
026
027
028/**
029 * This class represents a SCIM simple value. Simple values can be String,
030 * Boolean, DateTime or Binary.
031 */
032public class SimpleValue
033{
034  /**
035   * The UTC time zone.
036   */
037  private static TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
038
039  /**
040   * The simple value stored as a String or byte[].
041   */
042  private final Object value;
043
044
045
046  /**
047   * Create a simple string value.
048   *
049   * @param stringValue  The string value.
050   */
051  public SimpleValue(final String stringValue)
052  {
053    this.value = stringValue;
054  }
055
056
057
058  /**
059   * Create a simple boolean value.
060   *
061   * @param booleanValue  The boolean value.
062   */
063  public SimpleValue(final Boolean booleanValue)
064  {
065    this.value = booleanValue.toString();
066  }
067
068
069
070  /**
071   * Create a simple datetime value.
072   *
073   * @param dateValue  The datetime value.
074   */
075  public SimpleValue(final Date dateValue)
076  {
077    final Calendar calendar = new GregorianCalendar(utcTimeZone);
078    calendar.setTime(dateValue);
079    this.value = DatatypeConverter.printDateTime(calendar);
080  }
081
082
083  /**
084   * Create a simple integer value.
085   *
086   * @param longValue The integer value, as a Long.
087   */
088  public SimpleValue(final Long longValue)
089  {
090    this.value = longValue.toString();
091  }
092
093
094  /**
095   * Create a simple integer value.
096   *
097   * @param intValue the integer value.
098   */
099  public SimpleValue(final Integer intValue)
100  {
101    this.value = intValue.toString();
102  }
103
104  /**
105   * Create a simple decimal value.
106   *
107   * @param doubleValue The decimal value, as a Double.
108   */
109  public SimpleValue(final Double doubleValue)
110  {
111    this.value = doubleValue.toString();
112  }
113
114
115  /**
116   * Create a simple binary value.
117   *
118   * @param bytes  The binary value.
119   */
120  public SimpleValue(final byte[] bytes)
121  {
122    this.value = bytes;
123  }
124
125
126
127  /**
128   * Retrieves the simple value as a string.
129   *
130   * @return  The simple value as a string.
131   */
132  public String getStringValue()
133  {
134    if (value instanceof byte[])
135    {
136      return DatatypeConverter.printBase64Binary((byte[])value);
137    }
138    else
139    {
140      return (String)value;
141    }
142  }
143
144
145
146  /**
147   * Retrieves the simple value as a boolean.
148   *
149   * @return  The simple value as a boolean.
150   */
151  public Boolean getBooleanValue()
152  {
153    return Boolean.valueOf((String)value);
154  }
155
156
157
158  /**
159   * Retrieves the simple value as a double.
160   *
161   * @return  The simple value as a double.
162   */
163  public Double getDoubleValue()
164  {
165    return Double.valueOf((String)value);
166  }
167
168
169
170  /**
171   * Retrieves the simple value as a long.
172   *
173   * @return  The simple value as a long.
174   */
175  public Long getLongValue()
176  {
177    return Long.valueOf((String)value);
178  }
179
180
181
182  /**
183   * Retrieves the simple value as a date.
184   *
185   * @return  The simple value as a date.
186   */
187  public Date getDateValue()
188  {
189    return DatatypeConverter.parseDateTime((String)value).getTime();
190  }
191
192
193
194  /**
195   * Retrieves the simple binary value.
196   *
197   * @return  The simple binary value.
198   */
199  public byte[] getBinaryValue()
200  {
201    return (byte[])value;
202  }
203
204
205  /**
206   * {@inheritDoc}
207   */
208  @Override
209  public String toString()
210  {
211    final StringBuilder sb = new StringBuilder();
212    sb.append("SimpleValue");
213    sb.append("{value=").append(value);
214    sb.append('}');
215    return sb.toString();
216  }
217
218
219  /**
220   * {@inheritDoc}
221   */
222  @Override
223  public boolean equals(final Object o) {
224    if (this == o) {
225      return true;
226    }
227    if (o == null || getClass() != o.getClass()) {
228      return false;
229    }
230
231    SimpleValue that = (SimpleValue) o;
232
233    if (!value.equals(that.value)) {
234      return false;
235    }
236
237    return true;
238  }
239
240
241  /**
242   * {@inheritDoc}
243   */
244  @Override
245  public int hashCode() {
246    return value.hashCode();
247  }
248}