Repository: cassandra
Updated Branches:
  refs/heads/cassandra-2.1 0c1432ac7 -> 6039d0ef7
  refs/heads/cassandra-2.2 4a849efeb -> 5fe40a117
  refs/heads/cassandra-3.0 32d761684 -> 5d918b8fc
  refs/heads/trunk 52a10696a -> 4a0d1caa2


Simplify MultiCBuilder implementation

We had 2 implementations of MultiCBuilder but one was now unused. The
patch thus simplify the implementation by getting rid of the unused
wrap method and making the whole class not abstract.


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/53dc42d4
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/53dc42d4
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/53dc42d4

Branch: refs/heads/trunk
Commit: 53dc42d4243766f0a828dc167175c7036c1b3942
Parents: 2e87c43
Author: Sylvain Lebresne <sylv...@datastax.com>
Authored: Thu Sep 24 09:41:57 2015 -0700
Committer: Sylvain Lebresne <sylv...@datastax.com>
Committed: Thu Sep 24 10:08:13 2015 -0700

----------------------------------------------------------------------
 .../org/apache/cassandra/db/MultiCBuilder.java  | 507 ++++++++-----------
 1 file changed, 199 insertions(+), 308 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/53dc42d4/src/java/org/apache/cassandra/db/MultiCBuilder.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/MultiCBuilder.java 
b/src/java/org/apache/cassandra/db/MultiCBuilder.java
index ab1c94d..be654fa 100644
--- a/src/java/org/apache/cassandra/db/MultiCBuilder.java
+++ b/src/java/org/apache/cassandra/db/MultiCBuilder.java
@@ -26,101 +26,64 @@ import org.apache.cassandra.utils.btree.BTreeSet;
 /**
  * Builder that allow to build multiple Clustering/Slice.Bound at the same 
time.
  */
-public abstract class MultiCBuilder
+public class MultiCBuilder
 {
     /**
-     * Creates a new empty {@code MultiCBuilder}.
+     * The table comparator.
      */
-    public static MultiCBuilder create(ClusteringComparator comparator)
-    {
-        return new ConcreteMultiCBuilder(comparator);
-    }
+    private final ClusteringComparator comparator;
 
     /**
-     * Wraps an existing {@code CBuilder} to provide him with a MultiCBuilder 
interface
-     * for the sake of passing it to {@link Restriction.appendTo}. The 
resulting
-     * {@code MultiCBuilder} will still only be able to create a single 
clustering/bound
-     * and an {@code IllegalArgumentException} will be thrown if elements that 
added that
-     * would correspond to building multiple clusterings.
+     * The elements of the clusterings
      */
-    public static MultiCBuilder wrap(final CBuilder builder)
-    {
-        return new MultiCBuilder()
-        {
-            private boolean containsNull;
-            private boolean containsUnset;
-            private boolean hasMissingElements;
-
-            public MultiCBuilder addElementToAll(ByteBuffer value)
-            {
-                builder.add(value);
-
-                if (value == null)
-                    containsNull = true;
-                if (value == ByteBufferUtil.UNSET_BYTE_BUFFER)
-                    containsUnset = true;
-
-                return this;
-            }
-
-            public MultiCBuilder addEachElementToAll(List<ByteBuffer> values)
-            {
-                if (values.isEmpty())
-                {
-                    hasMissingElements = true;
-                    return this;
-                }
+    private final List<List<ByteBuffer>> elementsList = new ArrayList<>();
 
-                if (values.size() > 1)
-                    throw new IllegalArgumentException();
-
-                return addElementToAll(values.get(0));
-            }
-
-            public MultiCBuilder addAllElementsToAll(List<List<ByteBuffer>> 
values)
-            {
-                if (values.isEmpty())
-                {
-                    hasMissingElements = true;
-                    return this;
-                }
-
-                if (values.size() > 1)
-                    throw new IllegalArgumentException();
+    /**
+     * The number of elements that have been added.
+     */
+    private int size;
 
-                return addEachElementToAll(values.get(0));
-            }
+    /**
+     * <code>true</code> if the clusterings have been build, 
<code>false</code> otherwise.
+     */
+    private boolean built;
 
-            public int remainingCount()
-            {
-                return builder.remainingCount();
-            }
+    /**
+     * <code>true</code> if the clusterings contains some <code>null</code> 
elements.
+     */
+    private boolean containsNull;
 
-            public boolean containsNull()
-            {
-                return containsNull;
-            }
+    /**
+     * <code>true</code> if the composites contains some <code>unset</code> 
elements.
+     */
+    private boolean containsUnset;
 
-            public boolean containsUnset()
-            {
-                return containsUnset;
-            }
+    /**
+     * <code>true</code> if some empty collection have been added.
+     */
+    private boolean hasMissingElements;
 
-            public boolean hasMissingElements()
-            {
-                return hasMissingElements;
-            }
+    private MultiCBuilder(ClusteringComparator comparator)
+    {
+        this.comparator = comparator;
+    }
 
-            public NavigableSet<Clustering> build()
-            {
-                return BTreeSet.of(builder.comparator(), builder.build());
-            }
+    /**
+     * Creates a new empty {@code MultiCBuilder}.
+     */
+    public static MultiCBuilder create(ClusteringComparator comparator)
+    {
+        return new MultiCBuilder(comparator);
+    }
 
-            public NavigableSet<Slice.Bound> buildBound(boolean isStart, 
boolean isInclusive)
-            {
-                return BTreeSet.of(builder.comparator(), 
builder.buildBound(isStart, isInclusive));
-            }
-        };
+    /**
+     * Checks if this builder is empty.
+     *
+     * @return <code>true</code> if this builder is empty, <code>false</code> 
otherwise.
+     */
+    private boolean isEmpty()
+    {
+        return elementsList.isEmpty();
     }
 
     /**
@@ -133,7 +96,25 @@ public abstract class MultiCBuilder
      * @param value the value of the next element
      * @return this <code>MulitCBuilder</code>
      */
-    public abstract MultiCBuilder addElementToAll(ByteBuffer value);
+    public MultiCBuilder addElementToAll(ByteBuffer value)
+    {
+        checkUpdateable();
+
+        if (isEmpty())
+            elementsList.add(new ArrayList<ByteBuffer>());
+
+        for (int i = 0, m = elementsList.size(); i < m; i++)
+        {
+            if (value == null)
+                containsNull = true;
+            if (value == ByteBufferUtil.UNSET_BYTE_BUFFER)
+                containsUnset = true;
+
+            elementsList.get(i).add(value);
+        }
+        size++;
+        return this;
+    }
 
     /**
      * Adds individually each of the specified elements to the end of all of 
the existing clusterings.
@@ -145,7 +126,42 @@ public abstract class MultiCBuilder
      * @param values the elements to add
      * @return this <code>CompositeBuilder</code>
      */
-    public abstract MultiCBuilder addEachElementToAll(List<ByteBuffer> values);
+    public MultiCBuilder addEachElementToAll(List<ByteBuffer> values)
+    {
+        checkUpdateable();
+
+        if (isEmpty())
+            elementsList.add(new ArrayList<ByteBuffer>());
+
+        if (values.isEmpty())
+        {
+            hasMissingElements = true;
+        }
+        else
+        {
+            for (int i = 0, m = elementsList.size(); i < m; i++)
+            {
+                List<ByteBuffer> oldComposite = elementsList.remove(0);
+
+                for (int j = 0, n = values.size(); j < n; j++)
+                {
+                    List<ByteBuffer> newComposite = new 
ArrayList<>(oldComposite);
+                    elementsList.add(newComposite);
+
+                    ByteBuffer value = values.get(j);
+
+                    if (value == null)
+                        containsNull = true;
+                    if (value == ByteBufferUtil.UNSET_BYTE_BUFFER)
+                        containsUnset = true;
+
+                    newComposite.add(values.get(j));
+                }
+            }
+        }
+        size++;
+        return this;
+    }
 
     /**
      * Adds individually each of the specified list of elements to the end of 
all of the existing composites.
@@ -157,48 +173,138 @@ public abstract class MultiCBuilder
      * @param values the elements to add
      * @return this <code>CompositeBuilder</code>
      */
-    public abstract MultiCBuilder addAllElementsToAll(List<List<ByteBuffer>> 
values);
+    public MultiCBuilder addAllElementsToAll(List<List<ByteBuffer>> values)
+    {
+        checkUpdateable();
+
+        if (isEmpty())
+            elementsList.add(new ArrayList<ByteBuffer>());
+
+        if (values.isEmpty())
+        {
+            hasMissingElements = true;
+        }
+        else
+        {
+            for (int i = 0, m = elementsList.size(); i < m; i++)
+            {
+                List<ByteBuffer> oldComposite = elementsList.remove(0);
+
+                for (int j = 0, n = values.size(); j < n; j++)
+                {
+                    List<ByteBuffer> newComposite = new 
ArrayList<>(oldComposite);
+                    elementsList.add(newComposite);
+
+                    List<ByteBuffer> value = values.get(j);
+
+                    if (value.isEmpty())
+                        hasMissingElements = true;
+
+                    if (value.contains(null))
+                        containsNull = true;
+                    if (value.contains(ByteBufferUtil.UNSET_BYTE_BUFFER))
+                        containsUnset = true;
+
+                    newComposite.addAll(value);
+                }
+            }
+            size += values.get(0).size();
+        }
+        return this;
+    }
 
     /**
      * Returns the number of elements that can be added to the clusterings.
      *
      * @return the number of elements that can be added to the clusterings.
      */
-    public abstract int remainingCount();
+    public int remainingCount()
+    {
+        return comparator.size() - size;
+    }
 
     /**
      * Checks if the clusterings contains null elements.
      *
      * @return <code>true</code> if the clusterings contains <code>null</code> 
elements, <code>false</code> otherwise.
      */
-    public abstract boolean containsNull();
+    public boolean containsNull()
+    {
+        return containsNull;
+    }
 
     /**
      * Checks if the clusterings contains unset elements.
      *
      * @return <code>true</code> if the clusterings contains 
<code>unset</code> elements, <code>false</code> otherwise.
      */
-    public abstract boolean containsUnset();
+    public boolean containsUnset()
+    {
+        return containsUnset;
+    }
 
     /**
      * Checks if some empty list of values have been added
      * @return <code>true</code> if the clusterings have some missing 
elements, <code>false</code> otherwise.
      */
-    public abstract boolean hasMissingElements();
+    public boolean hasMissingElements()
+    {
+        return hasMissingElements;
+    }
 
     /**
      * Builds the <code>clusterings</code>.
      *
      * @return the clusterings
      */
-    public abstract NavigableSet<Clustering> build();
+    public NavigableSet<Clustering> build()
+    {
+        built = true;
+
+        if (hasMissingElements)
+            return BTreeSet.empty(comparator);
+
+        CBuilder builder = CBuilder.create(comparator);
+
+        if (elementsList.isEmpty())
+            return BTreeSet.of(builder.comparator(), builder.build());
+
+        BTreeSet.Builder<Clustering> set = 
BTreeSet.builder(builder.comparator());
+        for (int i = 0, m = elementsList.size(); i < m; i++)
+        {
+            List<ByteBuffer> elements = elementsList.get(i);
+            set.add(builder.buildWith(elements));
+        }
+        return set.build();
+    }
 
     /**
      * Builds the <code>clusterings</code> with the specified EOC.
      *
      * @return the clusterings
      */
-    public abstract NavigableSet<Slice.Bound> buildBound(boolean isStart, 
boolean isInclusive);
+    public NavigableSet<Slice.Bound> buildBound(boolean isStart, boolean 
isInclusive)
+    {
+        built = true;
+
+        if (hasMissingElements)
+            return BTreeSet.empty(comparator);
+
+        CBuilder builder = CBuilder.create(comparator);
+
+        if (elementsList.isEmpty())
+            return BTreeSet.of(comparator, builder.buildBound(isStart, 
isInclusive));
+
+        // Use a TreeSet to sort and eliminate duplicates
+        BTreeSet.Builder<Slice.Bound> set = BTreeSet.builder(comparator);
+
+        for (int i = 0, m = elementsList.size(); i < m; i++)
+        {
+            List<ByteBuffer> elements = elementsList.get(i);
+            set.add(builder.buildBoundWith(elements, isStart, isInclusive));
+        }
+        return set.build();
+    }
 
     /**
      * Checks if some elements can still be added to the clusterings.
@@ -210,224 +316,9 @@ public abstract class MultiCBuilder
         return remainingCount() > 0;
     }
 
-
-    private static class ConcreteMultiCBuilder extends MultiCBuilder
+    private void checkUpdateable()
     {
-        /**
-         * The table comparator.
-         */
-        private final ClusteringComparator comparator;
-
-        /**
-         * The elements of the clusterings
-         */
-        private final List<List<ByteBuffer>> elementsList = new ArrayList<>();
-
-        /**
-         * The number of elements that have been added.
-         */
-        private int size;
-
-        /**
-         * <code>true</code> if the clusterings have been build, 
<code>false</code> otherwise.
-         */
-        private boolean built;
-
-        /**
-         * <code>true</code> if the clusterings contains some 
<code>null</code> elements.
-         */
-        private boolean containsNull;
-
-        /**
-         * <code>true</code> if the composites contains some 
<code>unset</code> elements.
-         */
-        private boolean containsUnset;
-
-        /**
-         * <code>true</code> if some empty collection have been added.
-         */
-        private boolean hasMissingElements;
-
-        public ConcreteMultiCBuilder(ClusteringComparator comparator)
-        {
-            this.comparator = comparator;
-        }
-
-        public MultiCBuilder addElementToAll(ByteBuffer value)
-        {
-            checkUpdateable();
-
-            if (isEmpty())
-                elementsList.add(new ArrayList<ByteBuffer>());
-
-            for (int i = 0, m = elementsList.size(); i < m; i++)
-            {
-                if (value == null)
-                    containsNull = true;
-                if (value == ByteBufferUtil.UNSET_BYTE_BUFFER)
-                    containsUnset = true;
-
-                elementsList.get(i).add(value);
-            }
-            size++;
-            return this;
-        }
-
-        public MultiCBuilder addEachElementToAll(List<ByteBuffer> values)
-        {
-            checkUpdateable();
-
-            if (isEmpty())
-                elementsList.add(new ArrayList<ByteBuffer>());
-
-            if (values.isEmpty())
-            {
-                hasMissingElements = true;
-            }
-            else
-            {
-                for (int i = 0, m = elementsList.size(); i < m; i++)
-                {
-                    List<ByteBuffer> oldComposite = elementsList.remove(0);
-
-                    for (int j = 0, n = values.size(); j < n; j++)
-                    {
-                        List<ByteBuffer> newComposite = new 
ArrayList<>(oldComposite);
-                        elementsList.add(newComposite);
-
-                        ByteBuffer value = values.get(j);
-
-                        if (value == null)
-                            containsNull = true;
-                        if (value == ByteBufferUtil.UNSET_BYTE_BUFFER)
-                            containsUnset = true;
-
-                        newComposite.add(values.get(j));
-                    }
-                }
-            }
-            size++;
-            return this;
-        }
-
-        public MultiCBuilder addAllElementsToAll(List<List<ByteBuffer>> values)
-        {
-            checkUpdateable();
-
-            if (isEmpty())
-                elementsList.add(new ArrayList<ByteBuffer>());
-
-            if (values.isEmpty())
-            {
-                hasMissingElements = true;
-            }
-            else
-            {
-                for (int i = 0, m = elementsList.size(); i < m; i++)
-                {
-                    List<ByteBuffer> oldComposite = elementsList.remove(0);
-
-                    for (int j = 0, n = values.size(); j < n; j++)
-                    {
-                        List<ByteBuffer> newComposite = new 
ArrayList<>(oldComposite);
-                        elementsList.add(newComposite);
-
-                        List<ByteBuffer> value = values.get(j);
-
-                        if (value.isEmpty())
-                            hasMissingElements = true;
-
-                        if (value.contains(null))
-                            containsNull = true;
-                        if (value.contains(ByteBufferUtil.UNSET_BYTE_BUFFER))
-                            containsUnset = true;
-
-                        newComposite.addAll(value);
-                    }
-                }
-                size += values.get(0).size();
-            }
-            return this;
-        }
-
-        public int remainingCount()
-        {
-            return comparator.size() - size;
-        }
-
-        /**
-         * Checks if this builder is empty.
-         *
-         * @return <code>true</code> if this builder is empty, 
<code>false</code> otherwise.
-         */
-        public boolean isEmpty()
-        {
-            return elementsList.isEmpty();
-        }
-
-        public boolean containsNull()
-        {
-            return containsNull;
-        }
-
-        public boolean containsUnset()
-        {
-            return containsUnset;
-        }
-
-        public boolean hasMissingElements()
-        {
-            return hasMissingElements;
-        }
-
-        public NavigableSet<Clustering> build()
-        {
-            built = true;
-
-            if (hasMissingElements)
-                return BTreeSet.empty(comparator);
-
-            CBuilder builder = CBuilder.create(comparator);
-
-            if (elementsList.isEmpty())
-                return BTreeSet.of(builder.comparator(), builder.build());
-
-            BTreeSet.Builder<Clustering> set = 
BTreeSet.builder(builder.comparator());
-            for (int i = 0, m = elementsList.size(); i < m; i++)
-            {
-                List<ByteBuffer> elements = elementsList.get(i);
-                set.add(builder.buildWith(elements));
-            }
-            return set.build();
-        }
-
-        public NavigableSet<Slice.Bound> buildBound(boolean isStart, boolean 
isInclusive)
-        {
-            built = true;
-
-            if (hasMissingElements)
-                return BTreeSet.empty(comparator);
-
-            CBuilder builder = CBuilder.create(comparator);
-
-            if (elementsList.isEmpty())
-                return BTreeSet.of(comparator, builder.buildBound(isStart, 
isInclusive));
-
-            // Use a TreeSet to sort and eliminate duplicates
-            BTreeSet.Builder<Slice.Bound> set = BTreeSet.builder(comparator);
-
-            for (int i = 0, m = elementsList.size(); i < m; i++)
-            {
-                List<ByteBuffer> elements = elementsList.get(i);
-                set.add(builder.buildBoundWith(elements, isStart, 
isInclusive));
-            }
-            return set.build();
-        }
-
-        private void checkUpdateable()
-        {
-            if (!hasRemaining() || built)
-                throw new IllegalStateException("this builder cannot be 
updated anymore");
-        }
+        if (!hasRemaining() || built)
+            throw new IllegalStateException("this builder cannot be updated 
anymore");
     }
 }

Reply via email to