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
018
019package com.unboundid.scim.sdk;
020
021import java.util.Date;
022import java.util.HashMap;
023
024/**
025 * This class represents a SCIM complex attribute value.
026 */
027public class ComplexValue
028    extends HashMap<String,SimpleValue> {
029  private static final long serialVersionUID = -2270345354493263396L;
030
031
032  /**
033   * Overridden from HashMap.  Converts string-valued keys to lower-case.
034   * @param name the sub-attribute name.
035   * @param value the sub-attribute value as a SimpleValue object.
036   * @return the previous value of the sub-attribute, or null if there was no
037   * previous mapping for the sub-attribute name.
038   */
039  @Override
040  public SimpleValue put(
041      final String name,
042      final SimpleValue value) {
043
044    return super.put(name.toLowerCase(), value);
045  }
046
047
048  /**
049   * Returns a SimpleValue for the specified sub-attribute name.
050   * The attribute name is case-insensitive.
051   * @param name sub-attribute name.
052   * @return SimpleValue object, or null if no sub-attribute
053   * exists with the specified name.
054   */
055  public SimpleValue get(final String name) {
056    // overridden from HashMap to convert key to lower-case
057    return super.get(name.toLowerCase());
058  }
059
060
061  /**
062   * Returns true if the ComplexAttribute contains a sub-attribute with the
063   * specified name.
064   * @param name sub-attribute name.
065   * @return true if the sub-attribute exists, false otherwise.
066   */
067  public boolean containsKey(final String name) {
068    // overridden from HashMap to convert key to lower-case
069    return super.containsKey(name.toLowerCase());
070  }
071
072
073  /**
074   * Set the value of a sub-attribute as a string.
075   * @param name sub-attribute name.
076   * @param value sub-attribute value.
077   */
078  public void putStringValue(final String name, final String value) {
079    put(name, new SimpleValue(value));
080  }
081
082
083  /**
084   * Set the value of a Boolean-valued sub-attribute.
085   * @param name sub-attribute name.
086   * @param value sub-attribute value.
087   */
088  public void putBooleanValue(final String name, final Boolean value) {
089    put(name, new SimpleValue(value));
090  }
091
092
093  /**
094   * Set the value of a Double-valued sub-attribute. This method can be used
095   * for attributes of the SCIM type 'Decimal'.
096   * @param name sub-attribute name.
097   * @param value sub-attribute value.
098   */
099  public void putDoubleValue(final String name, final Double value) {
100    put(name, new SimpleValue(value));
101  }
102
103
104  /**
105   * Set the value of a Long-valued sub-attribute.  This method can be used
106   * for attributes of the SCIM type 'Integer'.
107   * @param name sub-attribute name.
108   * @param value sub-attribute value.
109   */
110  public void putLongValue(final String name, final Long value) {
111    put(name, new SimpleValue(value));
112  }
113
114  /**
115   * Set the value of a Date-valued sub-attribute.  This method can be used
116   * for attributes of the SCIM type 'DateTime'.
117   * @param name sub-attribute name.
118   * @param value sub-attribute value.
119   */
120  @Deprecated
121  public void puDateValue(final String name, final Date value) {
122    put(name, new SimpleValue(value));
123  }
124
125  /**
126   * Set the value of a Date-valued sub-attribute.  This method can be used
127   * for attributes of the SCIM type 'DateTime'.
128   * @param name sub-attribute name.
129   * @param value sub-attribute value.
130   */
131  public void putDateValue(final String name, final Date value) {
132    put(name, new SimpleValue(value));
133  }
134
135
136  /**
137   * Set the value of a Binary-valued sub-attribute.
138   * @param name sub-attribute name.
139   * @param value sub-attribute value.
140   */
141  public void putBinaryValue(final String name, final byte[] value) {
142    put(name, new SimpleValue(value));
143  }
144
145
146  /**
147   * Get the value of a sub-attribute as a string.
148   * @param name sub-attribute name.
149   * @return the sub-attribute value as a String, or null if no sub-attribute
150   * exists with the specified name.
151   */
152  public String getStringValue(final String name) {
153    SimpleValue sub = get(name);
154    if (sub == null) {
155      return null;
156    }
157    return sub.getStringValue();
158  }
159
160
161  /**
162   * Get the value of a Boolean-valued sub-attribute.
163   * @param name sub-attribute name.
164   * @return the sub-attribute value as a Boolean, or null if no sub-attribute
165   * exists with the specified name.
166   */
167  public Boolean getBooleanValue(final String name) {
168    SimpleValue sub = get(name);
169    if (sub == null) {
170      return null;
171    }
172    return sub.getBooleanValue();
173  }
174
175
176  /**
177   * Get the value of a Double-valued sub-attribute.  This method can be used
178   * for attributes of the SCIM type 'Decimal'.
179   * @param name sub-attribute name.
180   * @return the sub-attribute value as a Double, or null if no sub-attribute
181   * exists with the specified name.
182   */
183  public Double getDoubleValue(final String name) {
184    SimpleValue sub = get(name);
185    if (sub == null) {
186      return null;
187    }
188    return sub.getDoubleValue();
189  }
190
191
192  /**
193   * Get the value of a Long-valued sub-attribute.  This method can be used
194   * for attributes of the SCIM type 'Integer'.
195   * @param name sub-attribute name.
196   * @return the sub-attribute value as a Long, or null if no sub-attribute
197   * exists with the specified name.
198   */
199  public Long getLongValue(final String name) {
200    SimpleValue sub = get(name);
201    if (sub == null) {
202      return null;
203    }
204    return sub.getLongValue();
205  }
206
207
208  /**
209   * Get the value of a Date-valued sub-attribute.  This method can be used
210   * for attributes of the SCIM type 'DateTime'.
211   * @param name sub-attribute name.
212   * @return the sub-attribute value as a Date object, or null if no
213   * sub-attribute exists with the specified name.
214   */
215  public Date getDateValue(final String name) {
216    SimpleValue sub = get(name);
217    if (sub == null) {
218      return null;
219    }
220    return sub.getDateValue();
221  }
222
223
224  /**
225   * Get the value of a Binary-valued sub-attribute.
226   * @param name sub-attribute name.
227   * @return the sub-attribute value as a byte array, or null if no
228   * sub-attribute exists with the specified name.
229   */
230  public byte[] getBinaryValue(final String name) {
231    SimpleValue sub = get(name);
232    if (sub == null) {
233      return null;
234    }
235    return sub.getBinaryValue();
236  }
237}