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.data;
019
020 import com.unboundid.scim.schema.AttributeDescriptor;
021 import com.unboundid.scim.sdk.InvalidResourceException;
022 import com.unboundid.scim.sdk.SCIMAttributeValue;
023
024 import java.util.Date;
025
026 /**
027 * Used to resolve SCIM attribute values to Java instances.
028 *
029 * @param <T> The Java class to resolve.
030 */
031 public abstract class AttributeValueResolver<T>
032 {
033 /**
034 * Create an instance from the given attribute value.
035 *
036 * @param value The value to create an instance from.
037 * @return The instance created from the attribute value.
038 */
039 public abstract T toInstance(final SCIMAttributeValue value);
040
041 /**
042 * Create a SCIM attribute value from the given instance.
043 *
044 * @param attributeDescriptor The descriptor for the attribute to create.
045 * @param value The instance.
046 * @return The SCIM attribute value created from the instance.
047 * @throws InvalidResourceException if the value violates the schema.
048 */
049 public abstract SCIMAttributeValue fromInstance(
050 final AttributeDescriptor attributeDescriptor, final T value)
051 throws InvalidResourceException;
052
053 /**
054 * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
055 * to/from <code>String</code> instances.
056 */
057 public static final AttributeValueResolver<String> STRING_RESOLVER =
058 new AttributeValueResolver<String>() {
059
060 /**
061 * {@inheritDoc}
062 */
063 public String toInstance(final SCIMAttributeValue value) {
064 return value.getStringValue();
065 }
066
067 /**
068 * {@inheritDoc}
069 */
070 public SCIMAttributeValue fromInstance(
071 final AttributeDescriptor attributeDescriptor, final String value) {
072 return SCIMAttributeValue.createStringValue(value);
073 }
074 };
075
076
077 /**
078 * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
079 * to/from <code>String</code> instances.
080 */
081 public static final AttributeValueResolver<Date> DATE_RESOLVER =
082 new AttributeValueResolver<Date>() {
083 /**
084 * {@inheritDoc}
085 */
086 public Date toInstance(final SCIMAttributeValue value) {
087 return value.getDateValue();
088 }
089
090 /**
091 * {@inheritDoc}
092 */
093 public SCIMAttributeValue fromInstance(
094 final AttributeDescriptor attributeDescriptor, final Date value) {
095 return SCIMAttributeValue.createDateValue(value);
096 }
097 };
098
099 /**
100 * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
101 * to/from <code>Boolean</code> instances.
102 */
103 public static final AttributeValueResolver<Boolean> BOOLEAN_RESOLVER =
104 new AttributeValueResolver<Boolean>() {
105 /**
106 * {@inheritDoc}
107 */
108 public Boolean toInstance(final SCIMAttributeValue value) {
109 return value.getBooleanValue();
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 public SCIMAttributeValue fromInstance(
116 final AttributeDescriptor attributeDescriptor,
117 final Boolean value) {
118 return SCIMAttributeValue.createBooleanValue(value);
119 }
120 };
121
122 /**
123 * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
124 * to/from <code>byte[]</code> instances.
125 */
126 public static final AttributeValueResolver<byte[]> BINARY_RESOLVER =
127 new AttributeValueResolver<byte[]>() {
128
129 /**
130 * {@inheritDoc}
131 */
132 @Override
133 public byte[] toInstance(final SCIMAttributeValue value) {
134 return value.getBinaryValue();
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 @Override
141 public SCIMAttributeValue fromInstance(
142 final AttributeDescriptor attributeDescriptor,
143 final byte[] value) {
144 return SCIMAttributeValue.createBinaryValue(value);
145 }
146 };
147
148 /**
149 * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
150 * to/from <code>Decimal</code> instances.
151 */
152 public static final AttributeValueResolver<Double> DECIMAL_RESOLVER =
153 new AttributeValueResolver<Double>() {
154 /**
155 * {@inheritDoc}
156 */
157 public Double toInstance(final SCIMAttributeValue value) {
158 return value.getDecimalValue();
159 }
160
161 /**
162 * {@inheritDoc}
163 */
164 public SCIMAttributeValue fromInstance(
165 final AttributeDescriptor attributeDescriptor,
166 final Double value) {
167 return SCIMAttributeValue.createStringValue(String.valueOf(value));
168 }
169 };
170
171 /**
172 * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
173 * to/from <code>Integer</code> instances.
174 */
175 public static final AttributeValueResolver<Long> INTEGER_RESOLVER =
176 new AttributeValueResolver<Long>() {
177 /**
178 * {@inheritDoc}
179 */
180 public Long toInstance(final SCIMAttributeValue value) {
181 return value.getIntegerValue();
182 }
183
184 /**
185 * {@inheritDoc}
186 */
187 public SCIMAttributeValue fromInstance(
188 final AttributeDescriptor attributeDescriptor,
189 final Long value) {
190 return SCIMAttributeValue.createStringValue(String.valueOf(value));
191 }
192 };
193
194
195 }