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 BULK configuration options.
032     */
033    public class BulkConfig
034    {
035      private final boolean supported;
036      private final long maxOperations;
037      private final long maxPayloadSize;
038    
039    
040    
041      /**
042       * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
043       * to/from <code>BulkConfig</code> instances.
044       */
045      public static final AttributeValueResolver<BulkConfig>
046          BULK_CONFIG_RESOLVER =
047          new AttributeValueResolver<BulkConfig>()
048          {
049            /**
050             * {@inheritDoc}
051             */
052            @Override
053            public BulkConfig toInstance(final SCIMAttributeValue value) {
054              return new BulkConfig(
055                  value.getSubAttributeValue("supported",
056                      BOOLEAN_RESOLVER),
057                  value.getSubAttributeValue("maxOperations",
058                      INTEGER_RESOLVER),
059                  value.getSubAttributeValue("maxPayloadSize",
060                      INTEGER_RESOLVER));
061            }
062    
063            /**
064             * {@inheritDoc}
065             */
066            @Override
067            public SCIMAttributeValue fromInstance(
068                final AttributeDescriptor attributeDescriptor,
069                final BulkConfig value)
070                throws InvalidResourceException
071            {
072              final List<SCIMAttribute> subAttributes =
073                  new ArrayList<SCIMAttribute>(3);
074    
075              final AttributeDescriptor supportedDescriptor =
076                  attributeDescriptor.getSubAttribute("supported");
077              subAttributes.add(
078                  SCIMAttribute.create(
079                      supportedDescriptor,
080                      BOOLEAN_RESOLVER.fromInstance(supportedDescriptor,
081                          value.supported)));
082    
083              final AttributeDescriptor maxOperationsDescriptor =
084                  attributeDescriptor.getSubAttribute("maxOperations");
085              subAttributes.add(
086                  SCIMAttribute.create(
087                      maxOperationsDescriptor,
088                      INTEGER_RESOLVER.fromInstance(maxOperationsDescriptor,
089                          value.maxOperations)));
090    
091              final AttributeDescriptor maxPayloadSizeDescriptor =
092                  attributeDescriptor.getSubAttribute("maxPayloadSize");
093              subAttributes.add(
094                  SCIMAttribute.create(
095                      maxPayloadSizeDescriptor,
096                      INTEGER_RESOLVER.fromInstance(maxPayloadSizeDescriptor,
097                          value.maxPayloadSize)));
098    
099              return SCIMAttributeValue.createComplexValue(subAttributes);
100            }
101          };
102    
103    
104    
105      /**
106       * Create a <code>BulkConfig</code> instance.
107       *
108       * @param supported       Specifies whether the BULK operation is supported.
109       * @param maxOperations   Specifies the maximum number of operations.
110       * @param maxPayloadSize  Specifies the maximum payload size in bytes.
111       */
112      public BulkConfig(final boolean supported,
113                        final long maxOperations,
114                        final long maxPayloadSize)
115      {
116        this.supported = supported;
117        this.maxOperations = maxOperations;
118        this.maxPayloadSize = maxPayloadSize;
119      }
120    
121    
122    
123      /**
124       * Indicates whether the PATCH operation is supported.
125       * @return  {@code true} if the PATCH operation is supported.
126       */
127      public boolean isSupported()
128      {
129        return supported;
130      }
131    
132    
133    
134      /**
135       * Retrieves the maximum number of operations.
136       * @return The maximum number of operations.
137       */
138      public long getMaxOperations()
139      {
140        return maxOperations;
141      }
142    
143    
144    
145      /**
146       * Retrieves the maximum payload size in bytes.
147       * @return The maximum payload size in bytes.
148       */
149      public long getMaxPayloadSize()
150      {
151        return maxPayloadSize;
152      }
153    
154    
155      @Override
156      public boolean equals(final Object o)
157      {
158        if (this == o)
159        {
160          return true;
161        }
162        if (o == null || getClass() != o.getClass())
163        {
164          return false;
165        }
166    
167        final BulkConfig that = (BulkConfig) o;
168    
169        if (maxOperations != that.maxOperations)
170        {
171          return false;
172        }
173        if (maxPayloadSize != that.maxPayloadSize)
174        {
175          return false;
176        }
177        if (supported != that.supported)
178        {
179          return false;
180        }
181    
182        return true;
183      }
184    
185    
186    
187      @Override
188      public int hashCode()
189      {
190        int result = (supported ? 1 : 0);
191        result = 31 * result + (int) (maxOperations ^ (maxOperations >>> 32));
192        result = 31 * result + (int) (maxPayloadSize ^ (maxPayloadSize >>> 32));
193        return result;
194      }
195    
196    
197    
198      @Override
199      public String toString()
200      {
201        final StringBuilder sb = new StringBuilder();
202        sb.append("BulkConfig");
203        sb.append("{supported=").append(supported);
204        sb.append(", maxOperations=").append(maxOperations);
205        sb.append(", maxPayloadSize=").append(maxPayloadSize);
206        sb.append('}');
207        return sb.toString();
208      }
209    }