001/*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: ModSet.java,v 1.4 2009/01/28 05:34:49 ww203982 Exp $
026 *
027 * Portions Copyright 2015 ForgeRock AS.
028 */
029
030package com.iplanet.services.ldap;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import org.forgerock.opendj.ldap.Attribute;
036import org.forgerock.opendj.ldap.Modification;
037import org.forgerock.opendj.ldap.ModificationType;
038
039/**
040 * Represents a set of modification on attributes
041 * @supported.api
042 */
043public class ModSet implements java.io.Serializable {
044    // TODO: This is an incomplete implementation. Currently subclass from
045    // LDAPModificationSet is used to get things going. Need internal
046    // representation overhaul to move away from "extends LDAPModification"
047
048    /**
049     * Modification specifiers for ADD
050     */
051    public static final int ADD = ModificationType.ADD.intValue();
052
053    /**
054     * Modification specifiers for REPLACE
055     */
056    public static final int REPLACE = ModificationType.REPLACE.intValue();
057
058    /**
059     * Modification specifiers for DELETE
060     */
061    public static final int DELETE = ModificationType.DELETE.intValue();
062
063    static final long serialVersionUID = 4650238666753391214L;
064    private int current = 0;
065    private final List<Modification> modifications = new ArrayList<>();
066
067    /**
068     * Default consturctor
069     */
070    public ModSet() {
071        current = 0;
072    }
073
074    /**
075     * Constructor with an attribute set defaulting all operation types to
076     * ModSet.ADD
077     * 
078     * @param attrSet
079     *            Attribute set to construct the modSet. All operations are
080     *            default to ModSet.ADD
081     */
082    public ModSet(AttrSet attrSet) {
083        this(attrSet, ModSet.ADD);
084    }
085
086    /**
087     * Construct ModSet given the same operation on a set of attributes
088     * 
089     * @param attrSet
090     *            Attribute set to construct the ModSet
091     * @param op
092     *            Operation type for ADD, REPLACE or DELETE
093     */
094    public ModSet(AttrSet attrSet, int op) {
095        for (int i = 0; i < attrSet.size(); i++) {
096            this.add(op, attrSet.elementAt(i).toLDAPAttribute());
097        }
098    }
099
100    /**
101     * Retrieves the number of <CODE>LDAPModification</CODE>
102     * objects in this set.
103     * @return the number of <CODE>LDAPModification</CODE>
104     * objects in this set.
105     */
106    public int size() {
107        return modifications.size();
108    }
109
110    /**
111     * Retrieves a particular <CODE>LDAPModification</CODE> object at
112     * the position specified by the index.
113     * @param index position of the <CODE>LDAPModification</CODE>
114     * object that you want to retrieve.
115     * @return <CODE>LDAPModification</CODE> object representing
116     * a change to make to an attribute.
117     */
118    public Modification elementAt(int index) {
119        return modifications.get(index);
120    }
121
122    /**
123     * Removes a particular <CODE>LDAPModification</CODE> object at
124     * the position specified by the index.
125     * @param index position of the <CODE>LDAPModification</CODE>
126     * object that you want to remove
127     */
128    public void removeElementAt(int index) {
129        modifications.remove(index);
130    }
131
132    /**
133     * Specifies another modification to be added to the set of modifications.
134     * @param op the type of modification to make. This can be one of the following:
135     *   <P>
136     *   <UL>
137     *   <LI><CODE>LDAPModification.ADD</CODE> (the value should be added to the attribute)
138     *   <LI><CODE>LDAPModification.DELETE</CODE> (the value should be removed from the attribute)
139     *   <LI><CODE>LDAPModification.REPLACE</CODE> (the value should replace the existing value of the attribute)
140     *   </UL><P>
141     * If you are working with a binary value (not a string value), you need to bitwise OR (|) the
142     * modification type with <CODE>LDAPModification.BVALUES</CODE>.
143     * <P>
144     *
145     * @param attr the attribute (possibly with values) to modify
146     */
147    public synchronized void add(int op, Attribute attr) {
148        Modification mod = new Modification(ModificationType.valueOf(op), attr);
149        modifications.add(mod);
150    }
151
152    /**
153     * Removes the first attribute with the specified name in the set of modifications.
154     * @param name name of the attribute to remove
155     */
156    public synchronized void remove( String name ) {
157        for (int i = 0; i < modifications.size(); i++) {
158            Modification mod = modifications.get(i);
159            Attribute attr = mod.getAttribute();
160            if (name.equalsIgnoreCase(attr.getAttributeDescriptionAsString())) {
161                modifications.remove(i);
162                return;
163            }
164        }
165    }
166
167    /**
168     * Retrieves the string representation of the
169     * modification set.
170     *
171     * @return string representation of the modification set.
172     */
173    public String toString() {
174        return "LDAPModificationSet: " + modifications.toString();
175    }
176}