commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r1167673 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/compare/ test/java/org/apache/commons/lang3/compare/
Date Sun, 11 Sep 2011 05:47:34 GMT
Author: bayard
Date: Sun Sep 11 05:47:33 2011
New Revision: 1167673

URL: http://svn.apache.org/viewvc?rev=1167673&view=rev
Log:
Making ComparatorChain implement Iterable. Including test and package private copy of Collections'
UnmodifiableIterator

Added:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java
  (with props)
Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java?rev=1167673&r1=1167672&r2=1167673&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java
Sun Sep 11 05:47:33 2011
@@ -43,7 +43,7 @@ import java.util.List;
  * @since Commons Collections 2.0
  * @version $Revision$ $Date$
  */
-public class ComparatorChain<E> implements Comparator<E>, Serializable {
+public class ComparatorChain<E> implements Comparator<E>, Serializable, Iterable
{
 
     /** The list of comparators in the chain. */
     protected List<Comparator<E>> comparatorChain = null;
@@ -109,6 +109,16 @@ public class ComparatorChain<E> implemen
 
     //-----------------------------------------------------------------------
     /**
+     * Iterate through the chained comparators. 
+     *
+     * @return Unmodifiable iterator over the chained comparators
+     */
+    public Iterator<Comparator<E>> iterator() {
+        return new UnmodifiableIterator(comparatorChain.iterator());
+    }
+
+    //-----------------------------------------------------------------------
+    /**
      * Implement a hash code for this comparator that is consistent with
      * {@link #equals(Object) equals}.
      *

Added: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java?rev=1167673&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java
(added)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java
Sun Sep 11 05:47:33 2011
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.compare;
+
+import java.util.Iterator;
+
+/** 
+ * Decorates an iterator such that it cannot be modified.
+ * <p>
+ * Attempts to modify it will result in an UnsupportedOperationException. 
+ *
+ * @since Commons Collections 3.0
+ * @version $Revision: 1148801 $ $Date: 2011-07-20 07:44:46 -0700 (Wed, 20 Jul 2011) $
+ */
+final class UnmodifiableIterator<E> implements Iterator<E> {
+
+    /** The iterator being decorated */
+    private final Iterator<E> iterator;
+
+    //-----------------------------------------------------------------------
+    /**
+     * Constructor.
+     *
+     * @param iterator  the iterator to decorate
+     */
+    public UnmodifiableIterator(Iterator<E> iterator) {
+        super();
+        if (iterator == null) {
+            throw new IllegalArgumentException("Iterator must not be null");
+        }
+        this.iterator = iterator;
+    }
+
+    //-----------------------------------------------------------------------
+    public boolean hasNext() {
+        return iterator.hasNext();
+    }
+
+    public E next() {
+        return iterator.next();
+    }
+
+    public void remove() {
+        throw new UnsupportedOperationException("remove() is not supported");
+    }
+
+}

Propchange: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java?rev=1167673&r1=1167672&r2=1167673&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java
Sun Sep 11 05:47:33 2011
@@ -20,6 +20,7 @@ package org.apache.commons.lang3.compare
 import static org.junit.Assert.*;
 
 import java.util.Comparator;
+import java.util.Iterator;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -51,4 +52,20 @@ public class ComparatorChainTest {
         assertTrue("Comparison failed", cc.compare( "ABC-123", "ABC-123" ) == 0 );
     }
 
+    @Test
+    public void testIterate() {
+        Comparator c1 = ComparableComparator.INSTANCE;
+        Comparator c2 = new ComparableComparator();
+        Comparator c3 = new NullComparator();
+        Comparator c4 = new ReverseComparator();
+        Iterable cc = new ComparatorChain(c1, c2, c3, c4);
+
+        Iterator itr = cc.iterator();
+        assertEquals( "Iteration failed", c1, itr.next() );
+        assertEquals( "Iteration failed", c2, itr.next() );
+        assertEquals( "Iteration failed", c3, itr.next() );
+        assertEquals( "Iteration failed", c4, itr.next() );
+        assertFalse( "Iteration failed", itr.hasNext() );
+    }
+
 }



Mime
View raw message