From f20fea692975b8541715a34ff5e67dd30b93ac9e Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 18 Feb 2014 09:04:40 +0000
Subject: [PATCH] OPENDJ-1308 Migrate schema support

---
 opendj-core/src/main/java/org/forgerock/opendj/ldap/ResultCode.java         |   32 +++++++++++++++++++++-----------
 opendj-core/src/test/java/org/forgerock/opendj/ldap/ResultCodeTestCase.java |   21 +++++++++++----------
 opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties    |    1 +
 3 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ResultCode.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/ResultCode.java
index 4f499cd..cbacde2 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ResultCode.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/ResultCode.java
@@ -24,12 +24,13 @@
  *      Copyright 2009 Sun Microsystems, Inc.
  *      Portions copyright 2013 ForgeRock AS.
  */
-
 package org.forgerock.opendj.ldap;
 
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import org.forgerock.i18n.LocalizableMessage;
 
@@ -54,6 +55,8 @@
      */
     public static enum Enum {
         //@Checkstyle:off
+        /** @see ResultCode#UNDEFINED */
+        UNDEFINED,
         /** @see ResultCode#SUCCESS */
         SUCCESS,
         /** @see ResultCode#OPERATIONS_ERROR */
@@ -191,10 +194,20 @@
         //@Checkstyle:on
     }
 
-    private static final ResultCode[] ELEMENTS = new ResultCode[16655];
+    private static final Map<Integer, ResultCode> ELEMENTS = new LinkedHashMap<Integer, ResultCode>();
 
-    private static final List<ResultCode> IMMUTABLE_ELEMENTS = Collections.unmodifiableList(Arrays
-            .asList(ELEMENTS));
+    private static final List<ResultCode> IMMUTABLE_ELEMENTS = Collections.unmodifiableList(new ArrayList<ResultCode>(
+            ELEMENTS.values()));
+
+    /**
+     * The result code that should only be used if the actual result code has
+     * not yet been determined.
+     * <p>
+     * Despite not being a standard result code, it is an implementation of the
+     * null object design pattern for this type. 
+     */
+    public static final ResultCode UNDEFINED = registerErrorResultCode(-1,
+            INFO_RESULT_UNDEFINED.get(), Enum.UNDEFINED);
 
     /**
      * The result code that indicates that the operation completed successfully.
@@ -846,10 +859,7 @@
      * @return The result code.
      */
     public static ResultCode valueOf(final int intValue) {
-        ResultCode result = null;
-        if (0 <= intValue && intValue < ELEMENTS.length) {
-            result = ELEMENTS[intValue];
-        }
+        ResultCode result = ELEMENTS.get(intValue);
         if (result == null) {
             result = new ResultCode(
                 intValue, LocalizableMessage.raw("unknown(" + intValue + ")"), true, Enum.UNKNOWN);
@@ -883,7 +893,7 @@
     private static ResultCode registerErrorResultCode(final int intValue,
             final LocalizableMessage name, final Enum resultCodeEnum) {
         final ResultCode t = new ResultCode(intValue, name, true, resultCodeEnum);
-        ELEMENTS[intValue] = t;
+        ELEMENTS.put(intValue, t);
         return t;
     }
 
@@ -902,7 +912,7 @@
     private static ResultCode registerSuccessResultCode(final int intValue,
             final LocalizableMessage name, final Enum resultCodeEnum) {
         final ResultCode t = new ResultCode(intValue, name, false, resultCodeEnum);
-        ELEMENTS[intValue] = t;
+        ELEMENTS.put(intValue, t);
         return t;
     }
 
diff --git a/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties b/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
index c30b968..c546db1 100755
--- a/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
+++ b/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
@@ -514,6 +514,7 @@
 ERR_SCHEMA_CONFLICTING_NAME_FORM_OID=Unable to register name form %s \
  with the server schema because its OID %s conflicts with the OID for an \
  existing name form %s
+INFO_RESULT_UNDEFINED=Undefined
 INFO_RESULT_SUCCESS=Success
 INFO_RESULT_OPERATIONS_ERROR=Operations Error
 INFO_RESULT_PROTOCOL_ERROR=Protocol Error
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/ResultCodeTestCase.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/ResultCodeTestCase.java
index 743cb1c..9118cb9 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/ResultCodeTestCase.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/ResultCodeTestCase.java
@@ -26,7 +26,6 @@
 
 import java.util.EnumSet;
 import java.util.Iterator;
-import java.util.LinkedList;
 
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
@@ -39,13 +38,7 @@
 
     @DataProvider
     public Iterator<Object[]> valuesDataProvider() {
-        final LinkedList<ResultCode> values = new LinkedList<ResultCode>(ResultCode.values());
-        for (Iterator<ResultCode> iter = values.iterator(); iter.hasNext();) {
-            if (iter.next() == null) {
-                iter.remove();
-            }
-        }
-        return new DataProviderIterator(values);
+        return new DataProviderIterator(ResultCode.values());
     }
 
     @Test(dataProvider = "valuesDataProvider")
@@ -55,8 +48,16 @@
 
     @Test
     public void valueOfIntUnknown() throws Exception {
-        int intValue = -1;
-        ResultCode unknown = ResultCode.valueOf(intValue);
+        int intValue;
+        ResultCode unknown;
+
+        intValue = -2;
+        unknown = ResultCode.valueOf(intValue);
+        assertSame(unknown.intValue(), intValue);
+        assertSame(unknown.asEnum(), ResultCode.Enum.UNKNOWN);
+
+        intValue = Integer.MAX_VALUE;
+        unknown = ResultCode.valueOf(intValue);
         assertSame(unknown.intValue(), intValue);
         assertSame(unknown.asEnum(), ResultCode.Enum.UNKNOWN);
     }

--
Gitblit v1.10.0