From ef7d4374f775f38f8ca938705b0ba0f0ba1c3066 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Wed, 03 Apr 2013 10:45:51 +0000
Subject: [PATCH] Additional change for OPENDJ-354: Implement a RequestHandler which provides an in-memory backend

---
 opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Iterables.java       |  101 ++++++++++++++++++++++++++++++++++++++------------
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AttributeFilter.java |    7 ---
 2 files changed, 78 insertions(+), 30 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Iterables.java b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Iterables.java
index 1d4cf19..72ffab9 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Iterables.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Iterables.java
@@ -27,6 +27,7 @@
 
 package com.forgerock.opendj.util;
 
+import java.util.Collection;
 import java.util.Iterator;
 
 import org.forgerock.opendj.ldap.Function;
@@ -50,12 +51,14 @@
             this.a = a;
         }
 
+        @Override
         public Iterator<M> iterator() {
             return Iterators.arrayIterator(a);
         }
     }
 
     private static final class EmptyIterable<M> extends AbstractIterable<M> {
+        @Override
         public Iterator<M> iterator() {
             return Iterators.emptyIterator();
         }
@@ -74,6 +77,7 @@
             this.parameter = p;
         }
 
+        @Override
         public Iterator<M> iterator() {
             return Iterators.filteredIterator(iterable.iterator(), predicate, parameter);
         }
@@ -87,6 +91,7 @@
             this.value = value;
         }
 
+        @Override
         public Iterator<M> iterator() {
             return Iterators.singletonIterator(value);
         }
@@ -105,6 +110,7 @@
             this.parameter = p;
         }
 
+        @Override
         public Iterator<N> iterator() {
             return Iterators.transformedIterator(iterable.iterator(), function, parameter);
         }
@@ -118,6 +124,7 @@
             this.iterable = iterable;
         }
 
+        @Override
         public Iterator<M> iterator() {
             return Iterators.unmodifiableIterator(iterable.iterator());
         }
@@ -199,6 +206,24 @@
     }
 
     /**
+     * Returns {@code true} if the provided iterable does not contain any
+     * elements.
+     *
+     * @param iterable
+     *            The iterable.
+     * @return {@code true} if the provided iterable does not contain any
+     *         elements.
+     */
+    public static boolean isEmpty(final Iterable<?> iterable) {
+        if (iterable instanceof Collection) {
+            // Fall-through if possible and potentially avoid allocation.
+            return ((Collection<?>) iterable).isEmpty();
+        } else {
+            return !iterable.iterator().hasNext();
+        }
+    }
+
+    /**
      * Returns an iterable containing the single element {@code value}. The
      * returned iterable's iterator does not support element removal via the
      * {@code remove()} method.
@@ -214,6 +239,58 @@
     }
 
     /**
+     * Returns the number of elements contained in the provided iterable.
+     *
+     * @param iterable
+     *            The iterable.
+     * @return The number of elements contained in the provided iterable.
+     */
+    public static int size(final Iterable<?> iterable) {
+        if (iterable instanceof Collection) {
+            // Fall-through if possible and potentially benefit from constant time calculation.
+            return ((Collection<?>) iterable).size();
+        } else {
+            final Iterator<?> i = iterable.iterator();
+            int sz = 0;
+            while (i.hasNext()) {
+                i.next();
+                sz++;
+            }
+            return sz;
+        }
+    }
+
+    /**
+     * Returns a string representation of the provided iterable composed of an
+     * opening square bracket, followed by each element separated by commas, and
+     * then a closing square bracket.
+     *
+     * @param iterable
+     *            The iterable whose string representation is to be returned.
+     * @return A string representation of the provided iterable.
+     * @see java.util.AbstractCollection#toString()
+     */
+    public static String toString(final Iterable<?> iterable) {
+        if (iterable instanceof Collection) {
+            // Fall-through if possible.
+            return ((Collection<?>) iterable).toString();
+        } else {
+            final StringBuilder builder = new StringBuilder();
+            boolean firstValue = true;
+            builder.append('[');
+            for (final Object value : iterable) {
+                if (!firstValue) {
+                    builder.append(", ");
+                }
+                builder.append(String.valueOf(value));
+                firstValue = false;
+            }
+            builder.append(']');
+            return builder.toString();
+        }
+    }
+
+    /**
      * Returns a view of {@code iterable} whose values have been mapped to
      * elements of type {@code N} using {@code function}. The returned
      * iterable's iterator supports element removal via the {@code remove()}
@@ -280,30 +357,6 @@
         return new UnmodifiableIterable<M>(iterable);
     }
 
-    /**
-     * Returns a string representation of the provided iterable composed of an
-     * opening square bracket, followed by each element separated by commas, and
-     * then a closing square bracket.
-     *
-     * @param iterable
-     *            The iterable whose string representation is to be returned.
-     * @return A string representation of the provided iterable.
-     */
-    public static String toString(Iterable<?> iterable) {
-        final StringBuilder builder = new StringBuilder();
-        boolean firstValue = true;
-        builder.append('[');
-        for (Object value : iterable) {
-            if (!firstValue) {
-                builder.append(',');
-            }
-            builder.append(String.valueOf(value));
-            firstValue = false;
-        }
-        builder.append(']');
-        return builder.toString();
-    }
-
     // Prevent instantiation
     private Iterables() {
         // Do nothing.
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AttributeFilter.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AttributeFilter.java
index cb66c19..b394c6e 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AttributeFilter.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AttributeFilter.java
@@ -253,13 +253,8 @@
             }
 
             @Override
-            @SuppressWarnings("unused")
             public int getAttributeCount() {
-                int i = 0;
-                for (final Attribute attribute : getAllAttributes()) {
-                    i++;
-                }
-                return i;
+                return Iterables.size(getAllAttributes());
             }
 
             @Override

--
Gitblit v1.10.0