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.data;
019
020import com.unboundid.scim.schema.AttributeDescriptor;
021import com.unboundid.scim.sdk.InvalidResourceException;
022import com.unboundid.scim.sdk.SCIMAttribute;
023import com.unboundid.scim.sdk.SCIMAttributeValue;
024
025import java.util.ArrayList;
026import java.util.List;
027
028
029
030/**
031 * A complex type that specifies SORT configuration options.
032 */
033public class SortConfig
034{
035  private final boolean supported;
036
037
038
039  /**
040   * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
041   * to/from <code>SortConfig</code> instances.
042   */
043  public static final AttributeValueResolver<SortConfig>
044      SORT_CONFIG_RESOLVER =
045      new AttributeValueResolver<SortConfig>()
046      {
047        /**
048         * {@inheritDoc}
049         */
050        @Override
051        public SortConfig toInstance(final SCIMAttributeValue value) {
052          return new SortConfig(
053              value.getSubAttributeValue("supported",
054                  BOOLEAN_RESOLVER));
055        }
056
057        /**
058         * {@inheritDoc}
059         */
060        @Override
061        public SCIMAttributeValue fromInstance(
062            final AttributeDescriptor attributeDescriptor,
063            final SortConfig value)
064            throws InvalidResourceException
065        {
066          final List<SCIMAttribute> subAttributes =
067              new ArrayList<SCIMAttribute>(1);
068
069          final AttributeDescriptor supportedDescriptor =
070              attributeDescriptor.getSubAttribute("supported");
071          subAttributes.add(
072              SCIMAttribute.create(
073                  supportedDescriptor,
074                  BOOLEAN_RESOLVER.fromInstance(supportedDescriptor,
075                      value.supported)));
076
077          return SCIMAttributeValue.createComplexValue(subAttributes);
078        }
079      };
080
081
082
083  /**
084   * Create a <code>SortConfig</code> instance.
085   *
086   * @param supported  Specifies whether sorting is supported.
087   */
088  public SortConfig(final boolean supported)
089  {
090    this.supported = supported;
091  }
092
093
094
095  /**
096   * Indicates whether sorting is supported.
097   * @return  {@code true} if sorting is supported.
098   */
099  public boolean isSupported()
100  {
101    return supported;
102  }
103
104
105
106  @Override
107  public boolean equals(final Object o)
108  {
109    if (this == o)
110    {
111      return true;
112    }
113    if (o == null || getClass() != o.getClass())
114    {
115      return false;
116    }
117
118    final SortConfig that = (SortConfig) o;
119
120    if (supported != that.supported)
121    {
122      return false;
123    }
124
125    return true;
126  }
127
128
129
130  @Override
131  public int hashCode()
132  {
133    return (supported ? 1 : 0);
134  }
135
136
137
138  @Override
139  public String toString()
140  {
141    final StringBuilder sb = new StringBuilder();
142    sb.append("SortConfig");
143    sb.append("{supported=").append(supported);
144    sb.append('}');
145    return sb.toString();
146  }
147}