001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.util;
018
019import static org.forgerock.util.Reject.*;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026
027import org.opends.server.types.Attribute;
028import org.forgerock.opendj.ldap.schema.AttributeType;
029import org.forgerock.opendj.ldap.DN;
030
031
032
033/**
034 * This class defines a data structure for a change record entry for
035 * an add operation.  It includes a DN and a set of attributes, as well as
036 * methods to decode the entry.
037 */
038@org.opends.server.types.PublicAPI(
039     stability=org.opends.server.types.StabilityLevel.VOLATILE,
040     mayInstantiate=true,
041     mayExtend=false,
042     mayInvoke=true)
043public final class AddChangeRecordEntry extends ChangeRecordEntry
044{
045
046  /**
047   * The entry attributes for this operation.
048   */
049  private final List<Attribute> attributes;
050
051
052
053  /**
054   * Creates a new entry with the provided information.
055   *
056   * @param dn
057   *          The distinguished name for this entry.  It must not be
058   *          <CODE>null</CODE>.
059   * @param attributes
060   *          The entry attributes for this operation.  It must not be
061   *          <CODE>null</CODE>.
062   */
063  public AddChangeRecordEntry(DN dn,
064      Map<AttributeType,List<Attribute>> attributes)
065  {
066    super(dn);
067
068
069    ifNull(attributes);
070
071
072    this.attributes = new ArrayList<>(attributes.size());
073    for (List<Attribute> list : attributes.values())
074    {
075      this.attributes.addAll(list);
076    }
077  }
078
079
080
081  /**
082   * Retrieves the name of the change operation type.
083   *
084   * @return  The name of the change operation type.
085   */
086  public ChangeOperationType getChangeOperationType()
087  {
088    return ChangeOperationType.ADD;
089  }
090
091
092
093  /**
094   * Retrieves the entire set of attributes for this entry.
095   * <p>
096   * The returned list is read-only.
097   *
098   * @return The entire unmodifiable list of attributes for this entry.
099   */
100  public List<Attribute> getAttributes()
101  {
102    return Collections.unmodifiableList(attributes);
103  }
104
105
106
107  /** {@inheritDoc} */
108  @Override
109  public String toString()
110  {
111    StringBuilder buffer = new StringBuilder();
112    buffer.append("AddChangeRecordEntry(dn=\"");
113    buffer.append(getDN());
114    buffer.append("\", attrs={");
115
116    Iterator<Attribute> iterator = attributes.iterator();
117    while (iterator.hasNext())
118    {
119      buffer.append(iterator.next().getName());
120      if (iterator.hasNext())
121      {
122        buffer.append(", ");
123      }
124    }
125    buffer.append("})");
126
127    return buffer.toString();
128  }
129}
130