001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2009 Sun Microsystems, Inc.
025 */
026package org.forgerock.opendj.ldap;
027
028import java.util.Arrays;
029import java.util.Collections;
030import java.util.List;
031
032/**
033 * A Modify operation change type as defined in RFC 4511 section 4.6 is used to
034 * specify the type of modification being performed on an attribute.
035 *
036 * @see <a href="http://tools.ietf.org/html/rfc4511#section-4.6">RFC 4511 -
037 *      Lightweight Directory Access Protocol (LDAP): The Protocol </a>
038 * @see <a href="http://tools.ietf.org/html/rfc4525">RFC 4525 - Lightweight
039 *      Directory Access Protocol (LDAP) Modify-Increment Extension </a>
040 */
041public final class ModificationType {
042
043    /**
044     * Contains equivalent values for the ModificationType values.
045     * This allows easily using ModificationType values with switch statements.
046     */
047    public static enum Enum {
048        //@Checkstyle:off
049        /** @see ModificationType#ADD */
050        ADD,
051        /** @see ModificationType#DELETE */
052        DELETE,
053        /** @see ModificationType#REPLACE */
054        REPLACE,
055        /** @see ModificationType#INCREMENT */
056        INCREMENT,
057        /** Used for unknown modification types. */
058        UNKNOWN;
059        //@Checkstyle:on
060    }
061
062    private static final ModificationType[] ELEMENTS = new ModificationType[4];
063
064    private static final List<ModificationType> IMMUTABLE_ELEMENTS = Collections
065            .unmodifiableList(Arrays.asList(ELEMENTS));
066
067    /**
068     * Add the values listed in the modification to the attribute, creating the
069     * attribute if necessary.
070     */
071    public static final ModificationType ADD = register(0, "add", Enum.ADD);
072
073    /**
074     * Delete the values listed in the modification from the attribute. If no
075     * values are listed, or if all current values of the attribute are listed,
076     * the entire attribute is removed.
077     */
078    public static final ModificationType DELETE = register(1, "delete", Enum.DELETE);
079
080    /**
081     * Replace all existing values of the attribute with the new values listed
082     * in the modification, creating the attribute if it did not already exist.
083     * A replace with no listed values will delete the entire attribute if it
084     * exists, and it is ignored if the attribute does not exist.
085     */
086    public static final ModificationType REPLACE = register(2, "replace", Enum.REPLACE);
087
088    /**
089     * Increment all existing values of the attribute by the amount specified in
090     * the modification value.
091     */
092    public static final ModificationType INCREMENT = register(3, "increment", Enum.INCREMENT);
093
094    /**
095     * Returns the modification change type having the specified integer value
096     * as defined in RFC 4511 section 4.6.
097     *
098     * @param intValue
099     *            The integer value of the modification change type.
100     * @return The modification change type, or {@code null} if there was no
101     *         modification change type associated with {@code intValue}.
102     */
103    public static ModificationType valueOf(final int intValue) {
104        ModificationType result = null;
105        if (0 <= intValue && intValue < ELEMENTS.length) {
106            result = ELEMENTS[intValue];
107        }
108        if (result == null) {
109            result = new ModificationType(intValue, "unknown(" + intValue + ")", Enum.UNKNOWN);
110        }
111        return result;
112    }
113
114    /**
115     * Returns an unmodifiable list containing the set of available modification
116     * change types indexed on their integer value as defined in RFC 4511
117     * section 4.6.
118     *
119     * @return An unmodifiable list containing the set of available modification
120     *         change types.
121     */
122    public static List<ModificationType> values() {
123        return IMMUTABLE_ELEMENTS;
124    }
125
126    /**
127     * Creates and registers a new modification change type with the
128     * application.
129     *
130     * @param intValue
131     *            The integer value of the modification change type as defined
132     *            in RFC 4511 section 4.6.
133     * @param name
134     *            The name of the modification change type.
135     * @param modificationTypeEnum
136     *            The enum equivalent for this modification type
137     * @return The new modification change type.
138     */
139    private static ModificationType register(final int intValue, final String name, final Enum modificationTypeEnum) {
140        final ModificationType t = new ModificationType(intValue, name, modificationTypeEnum);
141        ELEMENTS[intValue] = t;
142        return t;
143    }
144
145    private final int intValue;
146
147    private final String name;
148
149    private final Enum modificationTypeEnum;
150
151    /** Prevent direct instantiation. */
152    private ModificationType(final int intValue, final String name, final Enum modificationTypeEnum) {
153        this.intValue = intValue;
154        this.name = name;
155        this.modificationTypeEnum = modificationTypeEnum;
156    }
157
158    /** {@inheritDoc} */
159    @Override
160    public boolean equals(final Object obj) {
161        if (this == obj) {
162            return true;
163        } else if (obj instanceof ModificationType) {
164            return this.intValue == ((ModificationType) obj).intValue;
165        } else {
166            return false;
167        }
168    }
169
170    /** {@inheritDoc} */
171    @Override
172    public int hashCode() {
173        return intValue;
174    }
175
176    /**
177     * Returns the integer value of this modification change type as defined in
178     * RFC 4511 section 4.6.
179     *
180     * @return The integer value of this modification change type.
181     */
182    public int intValue() {
183        return intValue;
184    }
185
186    /**
187     * Returns the enum equivalent for this modification type.
188     *
189     * @return The enum equivalent for this modification type when a known mapping exists,
190     *         or {@link Enum#UNKNOWN} if this is an unknown modification type.
191     */
192    public Enum asEnum() {
193        return this.modificationTypeEnum;
194    }
195
196    /**
197     * Returns the string representation of this modification change type.
198     *
199     * @return The string representation of this modification change type.
200     */
201    @Override
202    public String toString() {
203        return name;
204    }
205}