From fc615c70ff00e54a13711275fcbb97a764c2ca0e Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Mon, 16 Dec 2013 10:17:02 +0000
Subject: [PATCH] Remove duplicate code.

---
 opendj-core/src/main/java/org/forgerock/opendj/ldap/SortKey.java |   71 ++++++++++++++---------------------
 1 files changed, 28 insertions(+), 43 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/SortKey.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/SortKey.java
index fda9bd7..bf79bc8 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/SortKey.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/SortKey.java
@@ -115,57 +115,42 @@
          * attributes sort last.
          */
         public int compare(final Entry entry1, final Entry entry2) {
-            // Find an normalize the lowest value attribute in entry1
-            ByteString lowestNormalizedValue1 = null;
-            for (final Attribute attribute : entry1.getAllAttributes(attributeDescription)) {
-                for (final ByteString value : attribute) {
-                    try {
-                        final ByteString tmp = matchingRule.normalizeAttributeValue(value);
-                        if (lowestNormalizedValue1 == null) {
-                            lowestNormalizedValue1 = tmp;
-                        } else if (valueComparator.compare(tmp, lowestNormalizedValue1) < 0) {
-                            lowestNormalizedValue1 = tmp;
-                        }
-                    } catch (final DecodeException ignored) {
-                        // Ignore the error - treat the value as missing.
-                    }
-                }
-            }
-
-            // Find an normalize the lowest value attribute in entry2
-            ByteString lowestNormalizedValue2 = null;
-            for (final Attribute attribute : entry2.getAllAttributes(attributeDescription)) {
-                for (final ByteString value : attribute) {
-                    try {
-                        final ByteString tmp = matchingRule.normalizeAttributeValue(value);
-                        if (lowestNormalizedValue2 == null) {
-                            lowestNormalizedValue2 = tmp;
-                        } else if (valueComparator.compare(tmp, lowestNormalizedValue2) < 0) {
-                            lowestNormalizedValue2 = tmp;
-                        }
-                    } catch (final DecodeException ignored) {
-                        // Ignore the error - treat the value as missing.
-                    }
-                }
-            }
+            // Find and normalize the lowest value attribute in each entry.
+            final ByteString normalizedValue1 = lowestValueOf(entry1);
+            final ByteString normalizedValue2 = lowestValueOf(entry2);
 
             // Entries with missing attributes always sort after (regardless of
             // order).
-            if (lowestNormalizedValue1 == null) {
-                return lowestNormalizedValue2 != null ? 1 : 0;
-            }
-
-            if (lowestNormalizedValue2 == null) {
+            if (normalizedValue1 == null) {
+                return normalizedValue2 != null ? 1 : 0;
+            } else if (normalizedValue2 == null) {
                 return -1;
-            }
-
-            if (isReverseOrder) {
-                return valueComparator.compare(lowestNormalizedValue2, lowestNormalizedValue1);
+            } else if (isReverseOrder) {
+                return valueComparator.compare(normalizedValue2, normalizedValue1);
             } else {
-                return valueComparator.compare(lowestNormalizedValue1, lowestNormalizedValue2);
+                return valueComparator.compare(normalizedValue1, normalizedValue2);
             }
         }
 
+        private ByteString lowestValueOf(final Entry entry) {
+            ByteString normalizedValue = null;
+            for (final Attribute attribute : entry.getAllAttributes(attributeDescription)) {
+                for (final ByteString value : attribute) {
+                    try {
+                        final ByteString tmp = matchingRule.normalizeAttributeValue(value);
+                        if (normalizedValue == null) {
+                            normalizedValue = tmp;
+                        } else if (valueComparator.compare(tmp, normalizedValue) < 0) {
+                            normalizedValue = tmp;
+                        }
+                    } catch (final DecodeException ignored) {
+                        // Ignore the error - treat the value as missing.
+                    }
+                }
+            }
+            return normalizedValue;
+        }
+
     }
 
     /**

--
Gitblit v1.10.0