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.forgerock.opendj.ldap.DN;
028import org.forgerock.opendj.ldap.schema.AttributeType;
029import org.opends.server.types.Attribute;
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  @Override
080  public ChangeOperationType getChangeOperationType()
081  {
082    return ChangeOperationType.ADD;
083  }
084
085  /**
086   * Retrieves the entire set of attributes for this entry.
087   * <p>
088   * The returned list is read-only.
089   *
090   * @return The entire unmodifiable list of attributes for this entry.
091   */
092  public List<Attribute> getAttributes()
093  {
094    return Collections.unmodifiableList(attributes);
095  }
096
097  @Override
098  public String toString()
099  {
100    StringBuilder buffer = new StringBuilder();
101    buffer.append("AddChangeRecordEntry(dn=\"");
102    buffer.append(getDN());
103    buffer.append("\", attrs={");
104
105    Iterator<Attribute> iterator = attributes.iterator();
106    while (iterator.hasNext())
107    {
108      buffer.append(iterator.next().getAttributeDescription());
109      if (iterator.hasNext())
110      {
111        buffer.append(", ");
112      }
113    }
114    buffer.append("})");
115
116    return buffer.toString();
117  }
118}