mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
29.59.2016 f3c2fff8d4282592a95e169a73cbccb423cbe451
Prep work for OPENDJ-2803 Migrate Attribute

AttributeBuilder.java:
Replaced attributeType, name, options, normalizedOptions fields by a single attributeDescription field.
Replaced setAttributeType(AttributeType, String name) by setAttributeDescription().
Removed getAttributeType().
4 files modified
305 ■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/types/AttributeBuilder.java 177 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/types/Entry.java 28 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java 3 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/types/AttributeBuilderTest.java 97 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/types/AttributeBuilder.java
@@ -16,8 +16,6 @@
 */
package org.opends.server.types;
import static org.opends.server.util.StaticUtils.*;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
@@ -26,8 +24,6 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.Assertion;
@@ -39,7 +35,6 @@
import org.forgerock.opendj.ldap.schema.MatchingRule;
import org.forgerock.util.Reject;
import org.forgerock.util.Utils;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.Attribute.RemoveOnceSwitchingAttributes;
import org.opends.server.util.CollectionUtils;
@@ -174,7 +169,7 @@
    @Override
    public final boolean contains(ByteString value)
    {
      return values.contains(createAttributeValue(getAttributeType(), value));
      return values.contains(createAttributeValue(attributeDescription, value));
    }
    @Override
@@ -623,7 +618,7 @@
   */
  private static final class AttributeValue
  {
    private final AttributeType attributeType;
    private final AttributeDescription attributeDescription;
    /** User-provided value. */
    private final ByteString value;
@@ -634,14 +629,14 @@
    /**
     * Construct a new attribute value.
     *
     * @param attributeType
     *          The attribute type.
     * @param attributeDescription
     *          The attribute description.
     * @param value
     *          The value of the attribute.
     */
    private AttributeValue(AttributeType attributeType, ByteString value)
    private AttributeValue(AttributeDescription attributeDescription, ByteString value)
    {
      this.attributeType = attributeType;
      this.attributeDescription = attributeDescription;
      this.value = value;
    }
@@ -654,7 +649,7 @@
    {
      if (normalizedValue == null)
      {
        normalizedValue = normalize(attributeType, value);
        normalizedValue = normalize(attributeDescription, value);
      }
      return normalizedValue;
    }
@@ -757,14 +752,8 @@
    return builder.toAttribute();
  }
  /** The attribute type for this attribute. */
  private AttributeType attributeType;
  /** The name of this attribute as provided by the end user. */
  private String name;
  /** The normalized set of options if there are more than one. */
  private SortedSet<String> normalizedOptions;
  /** The set of options. */
  private final SmallSet<String> options = new SmallSet<>();
  /** The attribute description for this attribute. */
  private AttributeDescription attributeDescription;
  /** The set of attribute values, which are lazily normalized. */
  private Set<AttributeValue> values = new SmallSet<>();
@@ -804,8 +793,7 @@
   */
  public AttributeBuilder(AttributeDescription attributeDescription)
  {
    this(attributeDescription.getAttributeType(), attributeDescription.getNameOrOID());
    setOptions(attributeDescription.getOptions());
    this.attributeDescription = attributeDescription;
  }
  /**
@@ -835,8 +823,7 @@
  {
    Reject.ifNull(attributeType, name);
    this.attributeType = attributeType;
    this.name = name;
    this.attributeDescription = AttributeDescription.create(name, attributeType);
  }
@@ -853,7 +840,7 @@
   */
  public AttributeBuilder(String attributeName)
  {
    this(DirectoryServer.getAttributeType(attributeName), attributeName);
    this(AttributeDescription.valueOf(attributeName));
  }
@@ -887,7 +874,7 @@
   */
  public boolean add(ByteString attributeValue)
  {
    AttributeValue value = createAttributeValue(attributeType, attributeValue);
    AttributeValue value = createAttributeValue(attributeDescription, attributeValue);
    boolean isNewValue = values.add(value);
    if (!isNewValue)
    {
@@ -901,18 +888,19 @@
  }
  /** Creates an attribute value with delayed normalization. */
  private static AttributeValue createAttributeValue(AttributeType attributeType, ByteString attributeValue)
  private static AttributeValue createAttributeValue(AttributeDescription attributeDescription,
      ByteString attributeValue)
  {
    return new AttributeValue(attributeType, attributeValue);
    return new AttributeValue(attributeDescription, attributeValue);
  }
  private static ByteString normalize(AttributeType attributeType, ByteString attributeValue)
  private static ByteString normalize(AttributeDescription attributeDescription, ByteString attributeValue)
  {
    try
    {
      if (attributeType != null)
      if (attributeDescription != null)
      {
        final MatchingRule eqRule = attributeType.getEqualityMatchingRule();
        final MatchingRule eqRule = attributeDescription.getAttributeType().getEqualityMatchingRule();
        return eqRule.normalizeAttributeValue(attributeValue);
      }
    }
@@ -1001,7 +989,7 @@
   */
  public boolean contains(ByteString value)
  {
    return values.contains(createAttributeValue(attributeType, value));
    return values.contains(createAttributeValue(attributeDescription, value));
  }
  /**
@@ -1027,21 +1015,6 @@
    return true;
  }
  /**
   * Retrieves the attribute type for this attribute builder.
   *
   * @return The attribute type for this attribute builder, or
   *         <code>null</code> if one has not yet been specified.
   */
  public AttributeType getAttributeType()
  {
    return attributeType;
  }
  /**
   * Returns <code>true</code> if this attribute builder contains no
   * attribute values.
@@ -1085,7 +1058,7 @@
   */
  public boolean remove(ByteString value)
  {
    return values.remove(createAttributeValue(attributeType, value));
    return values.remove(createAttributeValue(attributeDescription, value));
  }
  /**
@@ -1208,55 +1181,15 @@
    addAll(values);
  }
  /**
   * Sets the attribute type associated with this attribute builder.
   * Sets the attribute description associated with this attribute builder.
   *
   * @param attributeType
   *          The attribute type for this attribute builder.
   * @param attrDesc
   *          The attribute description for this attribute builder.
   */
  public void setAttributeType(AttributeType attributeType)
  void setAttributeDescription(AttributeDescription attrDesc)
  {
    setAttributeType(attributeType, attributeType.getNameOrOID());
  }
  /**
   * Sets the attribute type and user-provided name associated with
   * this attribute builder.
   *
   * @param attributeType
   *          The attribute type for this attribute builder.
   * @param name
   *          The user-provided name for this attribute builder.
   */
  public void setAttributeType(
      AttributeType attributeType,
      String name)
  {
    Reject.ifNull(attributeType, name);
    this.attributeType = attributeType;
    this.name = name;
  }
  /**
   * Sets the attribute type associated with this attribute builder
   * using the provided attribute type name.
   * <p>
   * If the attribute name cannot be found in the schema, a new
   * attribute type is created using the default attribute syntax.
   *
   * @param attributeName
   *          The attribute name for this attribute builder.
   */
  public void setAttributeType(String attributeName)
  {
    setAttributeType(DirectoryServer.getAttributeType(attributeName), attributeName);
    attributeDescription = attrDesc;
  }
  /**
@@ -1270,29 +1203,13 @@
   */
  public boolean setOption(String option)
  {
    switch (options.size())
    AttributeDescription newAD = attributeDescription.withOption(option);
    if (attributeDescription != newAD)
    {
    case 0:
      return options.add(option);
    case 1:
      // Normalize and add the first option to normalized set.
      normalizedOptions = new TreeSet<>();
      normalizedOptions.add(toLowerCase(options.firstElement));
      if (normalizedOptions.add(toLowerCase(option)))
      {
        options.add(option);
        return true;
      }
      return false;
    default:
      if (normalizedOptions.add(toLowerCase(option)))
      {
        options.add(option);
        return true;
      }
      return false;
      attributeDescription = newAD;
      return true;
    }
    return false;
  }
@@ -1397,7 +1314,7 @@
   */
  public Attribute toAttribute() throws IllegalStateException
  {
    if (attributeType == null)
    if (attributeDescription == null)
    {
      throw new IllegalStateException("Undefined attribute type or name");
    }
@@ -1406,10 +1323,7 @@
    Attribute attribute = toAttribute0();
    // Reset the state of this builder.
    attributeType = null;
    name = null;
    normalizedOptions = null;
    options.clear();
    attributeDescription = null;
    values = new SmallSet<>();
    return attribute;
@@ -1417,20 +1331,7 @@
  private Attribute toAttribute0()
  {
    return new RealAttribute(toAttributeDescription(name), values);
  }
  private AttributeDescription toAttributeDescription(String name)
  {
    switch (options.size())
    {
    case 0:
      return AttributeDescription.create(name, attributeType);
    case 1:
      return AttributeDescription.create(name, attributeType, options.firstElement);
    default:
      return AttributeDescription.create(name, attributeType, options.elements);
    }
    return new RealAttribute(attributeDescription, values);
  }
  /**
@@ -1454,18 +1355,10 @@
  {
    StringBuilder builder = new StringBuilder();
    builder.append("AttributeBuilder(");
    builder.append(name);
    for (String option : options)
    {
      builder.append(';');
      builder.append(option);
    }
    builder.append(attributeDescription);
    builder.append(", {");
    Utils.joinAsString(builder, ", ", values);
    builder.append("})");
    return builder.toString();
  }
}
opendj-server-legacy/src/main/java/org/opends/server/types/Entry.java
@@ -3565,8 +3565,7 @@
        entryBuffer.skip(1);
        AttributeDescription attrDesc = AttributeDescription.valueOf(name);
        builder.setAttributeType(attrDesc.getAttributeType(), attrDesc.getNameOrOID());
        builder.setOptions(attrDesc.getOptions());
        builder.setAttributeDescription(attrDesc);
        // Next, we have the number of values.
        int numValues = entryBuffer.readBERLength();
@@ -3595,8 +3594,6 @@
    return attributes;
  }
  /**
   * Retrieves a list of the lines for this entry in LDIF form.  Long
   * lines will not be wrapped automatically.
@@ -4484,7 +4481,7 @@
                  {
                    // User requested non-default object class type name.
                    AttributeBuilder builder = new AttributeBuilder(ocAttr);
                    builder.setAttributeType(ocType, attrName);
                    builder.setAttributeDescription(AttributeDescription.create(attrName, ocType));
                    ocAttr = builder.toAttribute();
                  }
@@ -4531,10 +4528,11 @@
    final String attrName = attrDesc.getNameOrOID();
    for (Attribute attribute : sourceList)
    {
      AttributeDescription subAttrDesc = attribute.getAttributeDescription();
      if (attribute.isEmpty()
          || (omitReal && attribute.isReal())
          || (omitVirtual && attribute.isVirtual())
          || !attribute.getAttributeDescription().isSubTypeOf(attrDesc))
          || !subAttrDesc.isSubTypeOf(attrDesc))
      {
        continue;
      }
@@ -4543,32 +4541,32 @@
        // If a non-default attribute name was provided or if the
        // attribute has options then we will need to rebuild the
        // attribute so that it contains the user-requested names and options.
        AttributeDescription subAttrDesc = attribute.getAttributeDescription();
        AttributeType subAttrType = subAttrDesc.getAttributeType();
        final AttributeType subAttrType = subAttrDesc.getAttributeType();
        if ((attrName != null && !attrName.equals(attribute.getAttributeDescription().getNameOrOID()))
        if ((attrName != null && !attrName.equals(subAttrDesc.getNameOrOID()))
            || attrDesc.hasOptions())
        {
          AttributeBuilder builder = new AttributeBuilder();
          // We want to use the user-provided name only if this attribute has
          // the same type as the requested type. This might not be the case for
          // sub-types e.g. requesting "name" and getting back "cn" - we don't
          // want to rename "name" to "cn".
          if (attrName == null || !subAttrType.equals(attrDesc.getAttributeType()))
          AttributeType attrType = attrDesc.getAttributeType();
          AttributeDescription newAttrDesc;
          if (attrName == null || !subAttrType.equals(attrType))
          {
            builder.setAttributeType(subAttrType, attribute.getAttributeDescription().getNameOrOID());
            newAttrDesc = AttributeDescription.create(subAttrDesc.getNameOrOID(), subAttrDesc.getAttributeType());
          }
          else
          {
            builder.setAttributeType(subAttrType, attrName);
            newAttrDesc = AttributeDescription.create(attrName, subAttrDesc.getAttributeType());
          }
          AttributeBuilder builder = new AttributeBuilder(newAttrDesc);
          builder.setOptions(attrDesc.getOptions());
          // Now add in remaining options from original attribute
          // (this will not overwrite options already present).
          builder.setOptions(attribute.getAttributeDescription().getOptions());
          builder.setOptions(subAttrDesc.getOptions());
          if (!omitValues)
          {
opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java
@@ -42,6 +42,7 @@
import org.forgerock.i18n.LocalizableMessageDescriptor.Arg0;
import org.forgerock.i18n.LocalizedIllegalArgumentException;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.AttributeDescription;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ModificationType;
import org.forgerock.opendj.ldap.ResultCode;
@@ -2454,7 +2455,7 @@
                                builder.toAttribute()));
    }
    builder.setAttributeType(elementType);
    builder.setAttributeDescription(AttributeDescription.create(elementType));
    for (String s : newElements)
    {
      if (!oldElements.contains(s))
opendj-server-legacy/src/test/java/org/opends/server/types/AttributeBuilderTest.java
@@ -722,20 +722,6 @@
    Assert.assertFalse(builder.containsAll(Arrays.asList(av1, av2, av3)));
  }
  /**
   * Tests {@link AttributeBuilder#getAttributeType()}.
   */
  @Test
  public void testAttributeBuilderGetAttributeType() throws Exception
  {
    AttributeBuilder builder = new AttributeBuilder(cnType);
    Assert.assertEquals(builder.getAttributeType(), cnType);
  }
  /**
   * Tests {@link AttributeBuilder#toAttribute()} throws
   * IllegalStateException after default constructor.
@@ -1024,89 +1010,6 @@
    Assert.assertTrue(a.contains(ByteString.valueOfUtf8("value4")));
  }
  /**
   * Tests {@link AttributeBuilder#setAttributeType(AttributeType)}.
   */
  @Test
  public void testAttributeBuilderSetAttributeType1() throws Exception
  {
    AttributeBuilder builder = new AttributeBuilder();
    Assert.assertNull(builder.getAttributeType());
    builder.setAttributeType(cnType);
    Assert.assertEquals(builder.getAttributeType(), cnType);
    Attribute a = builder.toAttribute();
    Assert.assertEquals(a.getAttributeDescription().getAttributeType(), cnType);
    Assert.assertEquals(a.getAttributeDescription().getNameOrOID(), "cn");
  }
  /**
   * Tests {@link AttributeBuilder#setAttributeType(String)}.
   */
  @Test
  public void testAttributeBuilderSetAttributeType2() throws Exception
  {
    AttributeBuilder builder = new AttributeBuilder();
    Assert.assertNull(builder.getAttributeType());
    builder.setAttributeType("cn");
    Assert.assertEquals(builder.getAttributeType(), cnType);
    Attribute a = builder.toAttribute();
    Assert.assertEquals(a.getAttributeDescription().getAttributeType(), cnType);
    Assert.assertEquals(a.getAttributeDescription().getNameOrOID(), "cn");
  }
  /**
   * Tests {@link AttributeBuilder#setAttributeType(String)}.
   */
  @Test
  public void testAttributeBuilderSetAttributeType3() throws Exception
  {
    AttributeBuilder builder = new AttributeBuilder();
    Assert.assertNull(builder.getAttributeType());
    builder.setAttributeType("CN");
    Assert.assertEquals(builder.getAttributeType(), cnType);
    Attribute a = builder.toAttribute();
    Assert.assertEquals(a.getAttributeDescription().getAttributeType(), cnType);
    Assert.assertEquals(a.getAttributeDescription().getNameOrOID(), "CN");
  }
  /**
   * Tests
   * {@link AttributeBuilder#setAttributeType(AttributeType, String)}.
   */
  @Test
  public void testAttributeBuilderSetAttributeType4() throws Exception
  {
    AttributeBuilder builder = new AttributeBuilder();
    Assert.assertNull(builder.getAttributeType());
    builder.setAttributeType(cnType, "CN");
    Assert.assertEquals(builder.getAttributeType(), cnType);
    Attribute a = builder.toAttribute();
    Assert.assertEquals(a.getAttributeDescription().getAttributeType(), cnType);
    Assert.assertEquals(a.getAttributeDescription().getNameOrOID(), "CN");
  }
  /**
   * Tests {@link AttributeBuilder#setOptions(java.util.Collection)}.
   */