001 /*
002 * Copyright 2011-2012 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
018 package com.unboundid.scim.sdk;
019
020 import javax.xml.bind.DatatypeConverter;
021 import java.util.Calendar;
022 import java.util.Date;
023 import java.util.GregorianCalendar;
024 import 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 */
032 public 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 binary value.
085 *
086 * @param bytes The binary value.
087 */
088 public SimpleValue(final byte[] bytes)
089 {
090 this.value = bytes;
091 }
092
093
094
095 /**
096 * Retrieves the simple value as a string.
097 *
098 * @return The simple value as a string.
099 */
100 public String getStringValue()
101 {
102 if (value instanceof byte[])
103 {
104 return DatatypeConverter.printBase64Binary((byte[])value);
105 }
106 else
107 {
108 return (String)value;
109 }
110 }
111
112
113
114 /**
115 * Retrieves the simple value as a boolean.
116 *
117 * @return The simple value as a boolean.
118 */
119 public Boolean getBooleanValue()
120 {
121 return Boolean.valueOf((String)value);
122 }
123
124
125
126 /**
127 * Retrieves the simple value as a double.
128 *
129 * @return The simple value as a double.
130 */
131 public Double getDoubleValue()
132 {
133 return Double.valueOf((String)value);
134 }
135
136
137
138 /**
139 * Retrieves the simple value as a long.
140 *
141 * @return The simple value as a long.
142 */
143 public Long getLongValue()
144 {
145 return Long.valueOf((String)value);
146 }
147
148
149
150 /**
151 * Retrieves the simple value as a date.
152 *
153 * @return The simple value as a date.
154 */
155 public Date getDateValue()
156 {
157 final Calendar calendar = DatatypeConverter.parseDateTime((String)value);
158 calendar.setTimeZone(utcTimeZone);
159 return calendar.getTime();
160 }
161
162
163
164 /**
165 * Retrieves the simple binary value.
166 *
167 * @return The simple binary value.
168 */
169 public byte[] getBinaryValue()
170 {
171 return (byte[])value;
172 }
173
174
175 /**
176 * {@inheritDoc}
177 */
178 @Override
179 public String toString()
180 {
181 final StringBuilder sb = new StringBuilder();
182 sb.append("SimpleValue");
183 sb.append("{value=").append(value);
184 sb.append('}');
185 return sb.toString();
186 }
187
188
189 /**
190 * {@inheritDoc}
191 */
192 @Override
193 public boolean equals(final Object o) {
194 if (this == o) {
195 return true;
196 }
197 if (o == null || getClass() != o.getClass()) {
198 return false;
199 }
200
201 SimpleValue that = (SimpleValue) o;
202
203 if (!value.equals(that.value)) {
204 return false;
205 }
206
207 return true;
208 }
209
210
211 /**
212 * {@inheritDoc}
213 */
214 @Override
215 public int hashCode() {
216 return value.hashCode();
217 }
218 }