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.SCIMAttribute;
023    import com.unboundid.scim.sdk.SCIMAttributeValue;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    
029    
030    /**
031     * A complex type that specifies FILTER configuration options.
032     */
033    public class FilterConfig
034    {
035      private final boolean supported;
036      private final long maxResults;
037    
038    
039    
040      /**
041       * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
042       * to/from <code>FilterConfig</code> instances.
043       */
044      public static final AttributeValueResolver<FilterConfig>
045          FILTER_CONFIG_RESOLVER =
046          new AttributeValueResolver<FilterConfig>()
047          {
048            /**
049             * {@inheritDoc}
050             */
051            @Override
052            public FilterConfig toInstance(final SCIMAttributeValue value) {
053              return new FilterConfig(
054                  value.getSubAttributeValue("supported",
055                      BOOLEAN_RESOLVER),
056                  value.getSubAttributeValue("maxResults",
057                      INTEGER_RESOLVER));
058            }
059    
060            /**
061             * {@inheritDoc}
062             */
063            @Override
064            public SCIMAttributeValue fromInstance(
065                final AttributeDescriptor attributeDescriptor,
066                final FilterConfig value)
067                throws InvalidResourceException
068            {
069              final List<SCIMAttribute> subAttributes =
070                  new ArrayList<SCIMAttribute>(2);
071    
072              final AttributeDescriptor supportedDescriptor =
073                  attributeDescriptor.getSubAttribute("supported");
074              subAttributes.add(
075                  SCIMAttribute.create(
076                      supportedDescriptor,
077                      BOOLEAN_RESOLVER.fromInstance(supportedDescriptor,
078                          value.supported)));
079    
080              final AttributeDescriptor maxResultsDescriptor =
081                  attributeDescriptor.getSubAttribute("maxResults");
082              subAttributes.add(
083                  SCIMAttribute.create(
084                      maxResultsDescriptor,
085                      INTEGER_RESOLVER.fromInstance(maxResultsDescriptor,
086                          value.maxResults)));
087    
088              return SCIMAttributeValue.createComplexValue(subAttributes);
089            }
090          };
091    
092    
093    
094      /**
095       * Create a <code>FilterConfig</code> instance.
096       *
097       * @param supported    Specifies whether the FILTER operation is supported.
098       * @param maxResults   Specifies the maximum number of resources returned in
099       *                     a response.
100       */
101      public FilterConfig(final boolean supported,
102                          final long maxResults)
103      {
104        this.supported = supported;
105        this.maxResults = maxResults;
106      }
107    
108    
109    
110      /**
111       * Indicates whether the FILTER operation is supported.
112       * @return  {@code true} if the FILTER operation is supported.
113       */
114      public boolean isSupported()
115      {
116        return supported;
117      }
118    
119    
120    
121      /**
122       * Retrieves the maximum number of resources returned in a response.
123       * @return The maximum number of resources returned in a response.
124       */
125      public long getMaxResults()
126      {
127        return maxResults;
128      }
129    
130    
131    
132      @Override
133      public boolean equals(final Object o)
134      {
135        if (this == o)
136        {
137          return true;
138        }
139        if (o == null || getClass() != o.getClass())
140        {
141          return false;
142        }
143    
144        final FilterConfig that = (FilterConfig) o;
145    
146        if (maxResults != that.maxResults)
147        {
148          return false;
149        }
150        if (supported != that.supported)
151        {
152          return false;
153        }
154    
155        return true;
156      }
157    
158    
159    
160      @Override
161      public int hashCode()
162      {
163        int result = (supported ? 1 : 0);
164        result = 31 * result + (int) (maxResults ^ (maxResults >>> 32));
165        return result;
166      }
167    
168    
169    
170      @Override
171      public String toString()
172      {
173        final StringBuilder sb = new StringBuilder();
174        sb.append("FilterConfig");
175        sb.append("{supported=").append(supported);
176        sb.append(", maxResults=").append(maxResults);
177        sb.append('}');
178        return sb.toString();
179      }
180    }