From c3e4b02d4fb7c118b8be251f07cec2c6486e286b Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Mon, 30 May 2016 10:43:10 +0000
Subject: [PATCH] OPENDJ-2987 Tranform CommonSchemaElements to an utility class

---
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/renderer/SchemaElementComboBoxCellRenderer.java |   49 +++---
 opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java                                       |    5 
 opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java                                   |  307 +------------------------------------------
 3 files changed, 36 insertions(+), 325 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/renderer/SchemaElementComboBoxCellRenderer.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/renderer/SchemaElementComboBoxCellRenderer.java
index f609888..57544c0 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/renderer/SchemaElementComboBoxCellRenderer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/renderer/SchemaElementComboBoxCellRenderer.java
@@ -23,11 +23,12 @@
 import javax.swing.JComboBox;
 import javax.swing.JList;
 
-import org.forgerock.opendj.ldap.schema.Syntax;
-import org.forgerock.opendj.ldap.schema.MatchingRule;
+import org.forgerock.opendj.ldap.schema.AttributeType;
 import org.forgerock.opendj.ldap.schema.AttributeUsage;
-import org.opends.server.types.CommonSchemaElements;
+import org.forgerock.opendj.ldap.schema.MatchingRule;
+import org.forgerock.opendj.ldap.schema.ObjectClass;
 import org.forgerock.opendj.ldap.schema.ObjectClassType;
+import org.forgerock.opendj.ldap.schema.Syntax;
 
 /** The cell renderer to be used to render schema elements in a combo box. */
 public class SchemaElementComboBoxCellRenderer extends CustomListCellRenderer
@@ -54,33 +55,35 @@
   public Component getListCellRendererComponent(JList list, Object value,
       int index, boolean isSelected, boolean cellHasFocus)
   {
+    return super.getListCellRendererComponent(
+        list, getLabel(value), index, isSelected, cellHasFocus);
+  }
+
+  private Object getLabel(Object value)
+  {
     if (value instanceof Syntax)
     {
-      String syntaxName = ((Syntax)value).getName();
-      if (syntaxName == null)
-      {
-        value = ((Syntax)value).getOID();
-      }
-      else
-      {
-        value = syntaxName;
-      }
+      Syntax syntax = (Syntax) value;
+      return syntax.getName() != null ? syntax.getName() : syntax.getOID();
     }
-    else if (value instanceof CommonSchemaElements)
+    else if (value instanceof AttributeType)
     {
-      value = ((CommonSchemaElements)value).getNameOrOID();
+      return ((AttributeType) value).getNameOrOID();
+    }
+    else if (value instanceof ObjectClass)
+    {
+      return ((ObjectClass) value).getNameOrOID();
     }
     else if (value instanceof MatchingRule)
     {
-      value = ((MatchingRule)value).getNameOrOID();
+      return ((MatchingRule) value).getNameOrOID();
     }
     else if (value instanceof AttributeUsage)
     {
       boolean isOperational = ((AttributeUsage)value).isOperational();
       if (isOperational)
       {
-        value = INFO_CTRL_PANEL_ATTRIBUTE_USAGE_OPERATIONAL.get(
-            value.toString());
+        return INFO_CTRL_PANEL_ATTRIBUTE_USAGE_OPERATIONAL.get(value.toString());
       }
     }
     else if (value instanceof ObjectClassType)
@@ -88,17 +91,13 @@
       switch ((ObjectClassType)value)
       {
       case AUXILIARY:
-        value = INFO_CTRL_PANEL_OBJECTCLASS_AUXILIARY_LABEL.get().toString();
-        break;
+        return INFO_CTRL_PANEL_OBJECTCLASS_AUXILIARY_LABEL.get().toString();
       case STRUCTURAL:
-        value = INFO_CTRL_PANEL_OBJECTCLASS_STRUCTURAL_LABEL.get().toString();
-        break;
+        return INFO_CTRL_PANEL_OBJECTCLASS_STRUCTURAL_LABEL.get().toString();
       case ABSTRACT:
-        value = INFO_CTRL_PANEL_OBJECTCLASS_ABSTRACT_LABEL.get().toString();
-        break;
+        return INFO_CTRL_PANEL_OBJECTCLASS_ABSTRACT_LABEL.get().toString();
       }
     }
-    return super.getListCellRendererComponent(
-        list, value, index, isSelected, cellHasFocus);
+    return value;
   }
 }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
index 2c35c6a..4073a3c 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
@@ -93,7 +93,6 @@
 import org.opends.server.types.Attributes;
 import org.opends.server.types.BackupConfig;
 import org.opends.server.types.BackupDirectory;
-import org.opends.server.types.CommonSchemaElements;
 import org.opends.server.types.DITContentRule;
 import org.opends.server.types.DITStructureRule;
 import org.opends.server.types.DirectoryException;
@@ -660,9 +659,9 @@
     {
       /* Add the file name to the description of the element if this was requested by the caller. */
       String value;
-      if (includeSchemaFile && element instanceof CommonSchemaElements)
+      if (includeSchemaFile && element instanceof SchemaElement)
       {
-        value = getDefinitionWithFileName((CommonSchemaElements) element);
+        value = getDefinitionWithFileName((SchemaElement) element);
       }
       else
       {
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java b/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java
index 1e7a334..4c1eee7 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java
@@ -16,25 +16,19 @@
  */
 package org.opends.server.types;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
+import static org.forgerock.util.Reject.*;
+import static org.opends.messages.SchemaMessages.*;
+import static org.opends.server.util.CollectionUtils.*;
+import static org.opends.server.util.ServerConstants.*;
+
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.ldap.ResultCode;
 import org.forgerock.opendj.ldap.schema.SchemaElement;
 import org.opends.server.util.RemoveOnceSDKSchemaIsUsed;
 
-import static org.forgerock.util.Reject.*;
-import static org.opends.messages.SchemaMessages.*;
-import static org.opends.server.util.CollectionUtils.*;
-import static org.opends.server.util.ServerConstants.*;
-import static org.opends.server.util.StaticUtils.*;
-
 /**
  * An abstract base class for LDAP schema definitions which contain an
  * OID, optional names, description, an obsolete flag, and an optional
@@ -60,118 +54,11 @@
  * protocol may be associated with a particular schema file.
  */
 @RemoveOnceSDKSchemaIsUsed
-@org.opends.server.types.PublicAPI(
-     stability=org.opends.server.types.StabilityLevel.VOLATILE,
-     mayInstantiate=false,
-     mayExtend=false,
-     mayInvoke=true)
-public abstract class CommonSchemaElements implements SchemaElement {
-  /** Indicates whether this definition is declared "obsolete". */
-  private final boolean isObsolete;
-
-  /** The hash code for this definition. */
-  private final int hashCode;
-
-  /** The set of additional name-value pairs associated with this definition. */
-  private final Map<String, List<String>> extraProperties;
-
-  /**
-   * The set of names for this definition, in a mapping between
-   * the all-lowercase form and the user-defined form.
-   */
-  private final Map<String, String> names;
-
-  /** The description for this definition. */
-  private final String description;
-
-  /** The OID that may be used to reference this definition. */
-  private final String oid;
-
-  /** The primary name to use for this definition. */
-  private final String primaryName;
-
-  /** The lower case name for this definition. */
-  private final String lowerName;
-
-  /**
-   * Creates a new definition with the provided information.
-   * <p>
-   * If no <code>primaryName</code> is specified, but a set of
-   * <code>names</code> is specified, then the first name retrieved
-   * from the set of <code>names</code> will be used as the primary
-   * name.
-   *
-   * @param primaryName
-   *          The primary name for this definition, or
-   *          <code>null</code> if there is no primary name.
-   * @param names
-   *          The full set of names for this definition, or
-   *          <code>null</code> if there are no names.
-   * @param oid
-   *          The OID for this definition (must not be
-   *          <code>null</code>).
-   * @param description
-   *          The description for the definition, or <code>null</code>
-   *          if there is no description.
-   * @param isObsolete
-   *          Indicates whether this definition is declared
-   *          "obsolete".
-   * @param extraProperties
-   *          A set of extra properties for this definition, or
-   *          <code>null</code> if there are no extra properties.
-   * @throws NullPointerException
-   *           If the provided OID was <code>null</code>.
-   */
-  protected CommonSchemaElements(String primaryName,
-      Collection<String> names, String oid, String description,
-      boolean isObsolete, Map<String, List<String>> extraProperties)
-      throws NullPointerException {
-    // Make sure mandatory parameters are specified.
-    if (oid == null) {
-      throw new NullPointerException(
-          "No oid specified in constructor");
-    }
-
-    this.oid = oid;
-    this.description = description;
-    this.isObsolete = isObsolete;
-
-    // Make sure we have a primary name if possible.
-    if (primaryName != null) {
-      this.primaryName = primaryName;
-    } else if (names != null && !names.isEmpty()) {
-      this.primaryName = names.iterator().next();
-    } else {
-      this.primaryName = null;
-    }
-    this.lowerName = this.primaryName != null ? toLowerCase(this.primaryName) : oid;
-
-    // OPENDJ-1645: oid changes during server bootstrap, so prefer using lowername if available
-    hashCode = this.lowerName.hashCode();
-
-    // Construct the normalized attribute name mapping.
-    if (names != null) {
-      this.names = new LinkedHashMap<>(names.size());
-
-      // Make sure the primary name is first (never null).
-      this.names.put(lowerName, this.primaryName);
-
-      // Add the remaining names in the order specified.
-      for (String name : names) {
-        this.names.put(toLowerCase(name), name);
-      }
-    } else if (this.primaryName != null) {
-      this.names = Collections.singletonMap(lowerName, this.primaryName);
-    } else {
-      this.names = Collections.emptyMap();
-    }
-
-    // FIXME: should really be a deep-copy.
-    if (extraProperties != null) {
-      this.extraProperties = new LinkedHashMap<>(extraProperties);
-    } else {
-      this.extraProperties = Collections.emptyMap();
-    }
+public final class CommonSchemaElements
+{
+  private CommonSchemaElements()
+  {
+    // private for utility classes
   }
 
   /**
@@ -201,91 +88,6 @@
   }
 
   /**
-   * Retrieves an iterable over the set of normalized names that may
-   * be used to reference this schema definition. The normalized form
-   * of an attribute name is defined as the user-defined name
-   * converted to lower-case.
-   *
-   * @return Returns an iterable over the set of normalized names that
-   *         may be used to reference this schema definition.
-   */
-  public final Set<String> getNormalizedNames() {
-    return names.keySet();
-  }
-
-  /**
-   * Retrieves an iterable over the set of user-defined names that may
-   * be used to reference this schema definition.
-   *
-   * @return Returns an iterable over the set of user-defined names
-   *         that may be used to reference this schema definition.
-   */
-  public final Iterable<String> getUserDefinedNames() {
-    return names.values();
-  }
-
-  /**
-   * Indicates whether this schema definition has the specified name.
-   *
-   * @param lowerName
-   *          The lowercase name for which to make the determination.
-   * @return <code>true</code> if the specified name is assigned to
-   *         this schema definition, or <code>false</code> if not.
-   */
-  public final boolean hasName(String lowerName) {
-    return names.containsKey(lowerName);
-  }
-
-  /**
-   * Retrieves the OID for this schema definition.
-   *
-   * @return The OID for this schema definition.
-   */
-  public final String getOID() {
-    return oid;
-  }
-
-  /**
-   * Retrieves the name or OID for this schema definition. If it has
-   * one or more names, then the primary name will be returned. If it
-   * does not have any names, then the OID will be returned.
-   *
-   * @return The name or OID for this schema definition.
-   */
-  public final String getNameOrOID() {
-    if (primaryName != null) {
-      return primaryName;
-    }
-    // Guaranteed not to be null.
-    return oid;
-  }
-
-  /**
-   * Retrieves the normalized primary name or OID for this schema
-   * definition. If it does not have any names, then the OID will be
-   * returned.
-   *
-   * @return The name or OID for this schema definition.
-   */
-  public final String getNormalizedPrimaryNameOrOID() {
-    return lowerName;
-  }
-
-  /**
-   * Indicates whether this schema definition has the specified name
-   * or OID.
-   *
-   * @param lowerValue
-   *          The lowercase value for which to make the determination.
-   * @return <code>true</code> if the provided value matches the OID
-   *         or one of the names assigned to this schema definition,
-   *         or <code>false</code> if not.
-   */
-  public final boolean hasNameOrOID(String lowerValue) {
-    return names.containsKey(lowerValue) || oid.equals(lowerValue);
-  }
-
-  /**
    * Retrieves the name of the schema file that contains the
    * definition for this schema definition.
    *
@@ -334,33 +136,6 @@
   }
 
   /**
-   * Retrieves the description for this schema definition.
-   *
-   * @return The description for this schema definition, or
-   *         <code>null</code> if there is no description.
-   */
-  @Override
-  public final String getDescription() {
-    return description;
-  }
-
-  /**
-   * Indicates whether this schema definition is declared "obsolete".
-   *
-   * @return <code>true</code> if this schema definition is declared
-   *         "obsolete", or <code>false</code> if not.
-   */
-  public final boolean isObsolete() {
-    return isObsolete;
-  }
-
-  @Override
-  public final Map<String, List<String>> getExtraProperties()
-  {
-    return extraProperties;
-  }
-
-  /**
    * Sets the value for an "extra" property for this schema element.
    * If a property already exists with the specified name, then it
    * will be overwritten.  If the value is {@code null}, then any
@@ -388,68 +163,6 @@
   }
 
   /**
-   * Sets the values for an "extra" property for this schema element.
-   * If a property already exists with the specified name, then it
-   * will be overwritten.  If the set of values is {@code null} or
-   * empty, then any existing property with the given name will be
-   * removed.
-   *
-   * @param  name    The name for the "extra" property.  It must not
-   *                 be {@code null}.
-   * @param  values  The set of values for the "extra" property.  If
-   *                 it is {@code null} or empty, then any existing
-   *                 definition will be removed.
-   */
-  public final void setExtraProperty(String name,
-                                     List<String> values) {
-    ifNull(name);
-
-    if (values == null || values.isEmpty())
-    {
-      extraProperties.remove(name);
-    }
-    else
-    {
-      LinkedList<String> valuesCopy = new LinkedList<>(values);
-      extraProperties.put(name, valuesCopy);
-    }
-  }
-
-  /**
-   * Indicates whether the provided object is equal to this attribute
-   * type. The object will be considered equal if it is an attribute
-   * type with the same OID as the current type.
-   *
-   * @param o
-   *          The object for which to make the determination.
-   * @return <code>true</code> if the provided object is equal to
-   *         this schema definition, or <code>false</code> if not.
-   */
-  @Override
-  public final boolean equals(Object o) {
-    if (this == o) {
-      return true;
-    }
-
-    if (o instanceof CommonSchemaElements) {
-      CommonSchemaElements other = (CommonSchemaElements) o;
-      return lowerName.equals(other.lowerName);
-    }
-    return false;
-  }
-
-  /**
-   * Retrieves the hash code for this schema definition. It will be
-   * based on the sum of the bytes of the OID.
-   *
-   * @return The hash code for this schema definition.
-   */
-  @Override
-  public final int hashCode() {
-    return hashCode;
-  }
-
-  /**
    * Retrieves the definition string used to create this attribute
    * type and including the X-SCHEMA-FILE extension.
    *

--
Gitblit v1.10.0