commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r618776 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/ test/java/org/apache/commons/configuration2/
Date Tue, 05 Feb 2008 21:15:39 GMT
Author: oheger
Date: Tue Feb  5 13:15:35 2008
New Revision: 618776

URL: http://svn.apache.org/viewvc?rev=618776&view=rev
Log:
Removed dependency to commons-collections from AbstractConfiguration by introducing a new
PrefixedKeysIterator class

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
  (with props)
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java
  (with props)
Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java?rev=618776&r1=618775&r2=618776&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
Tue Feb  5 13:15:35 2008
@@ -27,8 +27,6 @@
 import java.util.NoSuchElementException;
 import java.util.Properties;
 
-import org.apache.commons.collections.Predicate;
-import org.apache.commons.collections.iterators.FilterIterator;
 import org.apache.commons.configuration2.event.ConfigurationErrorEvent;
 import org.apache.commons.configuration2.event.ConfigurationErrorListener;
 import org.apache.commons.configuration2.event.EventSource;
@@ -557,16 +555,9 @@
         fireEvent(EVENT_CLEAR, null, null, false);
     }
 
-    public Iterator getKeys(final String prefix)
+    public Iterator<String> getKeys(final String prefix)
     {
-        return new FilterIterator(getKeys(), new Predicate()
-        {
-            public boolean evaluate(Object obj)
-            {
-                String key = (String) obj;
-                return key.startsWith(prefix + ".") || key.equals(prefix);
-            }
-        });
+        return new PrefixedKeysIterator(getKeys(), prefix);
     }
 
     public Properties getProperties(String key)

Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java?rev=618776&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
(added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
Tue Feb  5 13:15:35 2008
@@ -0,0 +1,141 @@
+/*
+ * 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.configuration2;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * <p>
+ * A specialized iterator implementation used by
+ * <code>{@link AbstractConfiguration}</code> to return an iteration over all
+ * keys starting with a specified prefix.
+ * </p>
+ * <p>
+ * This class is basically a stripped-down version of the
+ * <code>FilterIterator</code> class of commons-collections.
+ * </p>
+ *
+ * @author <a
+ *         href="http://commons.apache.org/configuration/team-list.html">Commons
+ *         Configuration team</a>
+ * @version $Id$
+ */
+class PrefixedKeysIterator implements Iterator<String>
+{
+    /** Stores the wrapped iterator. */
+    private final Iterator<String> iterator;
+
+    /** Stores the prefix. */
+    private final String prefix;
+
+    /** Stores the next element in the iteration. */
+    private String nextElement;
+
+    /** A flag whether the next element has been calculated. */
+    private boolean nextElementSet;
+
+    /**
+     * Creates a new instance of <code>PrefixedKeysIterator</code> and sets
+     * the wrapped iterator and the prefix for the accepted keys.
+     *
+     * @param wrappedIterator the wrapped iterator
+     * @param keyPrefix the prefix of the allowed keys
+     */
+    public PrefixedKeysIterator(Iterator<String> wrappedIterator,
+            String keyPrefix)
+    {
+        iterator = wrappedIterator;
+        prefix = keyPrefix;
+    }
+
+    /**
+     * Returns a flag whether there are more elements in the iteration.
+     *
+     * @return a flag if there is a next element
+     */
+    public boolean hasNext()
+    {
+        if (nextElementSet)
+        {
+            return true;
+        }
+        else
+        {
+            return setNextElement();
+        }
+    }
+
+    /**
+     * Returns the next element in the iteration. This is the next key that
+     * matches the specified prefix.
+     *
+     * @return the next element in the iteration
+     * @throws NoSuchElementException if there is no next element
+     */
+    public String next()
+    {
+        if (!nextElementSet)
+        {
+            if (!setNextElement())
+            {
+                throw new NoSuchElementException();
+            }
+        }
+        nextElementSet = false;
+        return nextElement;
+    }
+
+    /**
+     * Removes from the underlying collection of the base iterator the last
+     * element returned by this iterator. This method can only be called if
+     * <code>next()</code> was called, but not after <code>hasNext()</code>,
+     * because the <code>hasNext()</code> call changes the base iterator.
+     *
+     * @throws IllegalStateException if <code>hasNext()</code> has already
+     *         been called.
+     */
+    public void remove()
+    {
+        if (nextElementSet)
+        {
+            throw new IllegalStateException("remove() cannot be called");
+        }
+        iterator.remove();
+    }
+
+    /**
+     * Determines the next element in the iteration. The return value indicates
+     * whether such an element can be found.
+     *
+     * @return a flag whether a next element exists
+     */
+    private boolean setNextElement()
+    {
+        while (iterator.hasNext())
+        {
+            String key = iterator.next();
+            if (key.startsWith(prefix + ".") || key.equals(prefix))
+            {
+                nextElement = key;
+                nextElementSet = true;
+                return true;
+            }
+        }
+        return false;
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PrefixedKeysIterator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java?rev=618776&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java
(added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java
Tue Feb  5 13:15:35 2008
@@ -0,0 +1,220 @@
+/*
+ * 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.configuration2;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for PrefixedKeysIterator.
+ *
+ * @version $Id$
+ */
+public class TestPrefixedKeysIterator extends TestCase
+{
+    /** Constant for the used prefix. */
+    private static final String PREFIX = "test";
+
+    /** Constant for the prefix of the valid keys. */
+    private static final String KEY = ".key";
+
+    /** An array with test keys. */
+    private static final String[] TEST_KEYS = {
+            PREFIX + KEY + "1", PREFIX + "AnotherKey", PREFIX + KEY + "2",
+            "differentKey", PREFIX + KEY + "3", PREFIX + KEY + "4"
+    };
+
+    /** Constant for the number of keys in the iteration. */
+    private static final int KEY_COUNT = 4;
+
+    /**
+     * A collection that stores test keys. Used for constructing the wrapped
+     * iterator.
+     */
+    private List<String> keyList;
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        keyList = new ArrayList<String>(Arrays.asList(TEST_KEYS));
+    }
+
+    /**
+     * Creates a test iterator using the list with the test keys.
+     *
+     * @return the test iterator
+     */
+    private PrefixedKeysIterator setUpIterator()
+    {
+        return new PrefixedKeysIterator(keyList.iterator(), PREFIX);
+    }
+
+    /**
+     * Tests whether the iterator returns the expected element.
+     *
+     * @param it the iterator
+     * @param expected the index of the expected key
+     */
+    private static void checkNext(PrefixedKeysIterator it, int expected)
+    {
+        assertEquals("Wrong key", PREFIX + KEY + expected, it.next());
+    }
+
+    /**
+     * Tests obtaining the next element.
+     */
+    public void testNext()
+    {
+        checkNext(setUpIterator(), 1);
+    }
+
+    /**
+     * Tests the next() method after calling hasNext().
+     */
+    public void testNextAfterHasNext()
+    {
+        PrefixedKeysIterator it = setUpIterator();
+        assertTrue("Iteration is empty", it.hasNext());
+        checkNext(it, 1);
+    }
+
+    /**
+     * Tests calling hasNext() multiple times.
+     */
+    public void testHasNextCached()
+    {
+        PrefixedKeysIterator it = setUpIterator();
+        assertTrue("Iteration is empty", it.hasNext());
+        assertTrue("Iteration is empty (2)", it.hasNext());
+    }
+
+    /**
+     * Tests a default iteration.
+     */
+    public void testIteration()
+    {
+        PrefixedKeysIterator it = setUpIterator();
+        int count = 0;
+
+        while (it.hasNext())
+        {
+            checkNext(it, ++count);
+        }
+        assertEquals("Wrong number of elements", KEY_COUNT, count);
+    }
+
+    /**
+     * Tests hasNext() for an empty iteration.
+     */
+    public void testHasNextEmpty()
+    {
+        keyList.clear();
+        PrefixedKeysIterator it = setUpIterator();
+        assertFalse("Found elements", it.hasNext());
+    }
+
+    /**
+     * Tests calling next() after the end of the iteration.
+     */
+    public void testNextAfterEndOfIteration()
+    {
+        PrefixedKeysIterator it = setUpIterator();
+        int count = 0;
+
+        try
+        {
+            while (true)
+            {
+                it.next();
+                count++;
+            }
+        }
+        catch (NoSuchElementException nsex)
+        {
+            assertEquals("Wrong number of elements", KEY_COUNT, count);
+        }
+    }
+
+    /**
+     * Tests calling next() for an empty iteration.
+     */
+    public void testNextEmpty()
+    {
+        keyList.clear();
+        PrefixedKeysIterator it = setUpIterator();
+        try
+        {
+            it.next();
+            fail("Could obtain element from empty iteration!");
+        }
+        catch (NoSuchElementException nsex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests whether a key that matches exactly the prefix is allowed.
+     */
+    public void testNextPrefix()
+    {
+        keyList.add(PREFIX);
+        PrefixedKeysIterator it = setUpIterator();
+        for (int i = 1; i <= KEY_COUNT; i++)
+        {
+            checkNext(it, i);
+        }
+        assertTrue("No more elements found", it.hasNext());
+        assertEquals("Prefix key not found", PREFIX, it.next());
+        assertFalse("More elements found", it.hasNext());
+    }
+
+    /**
+     * Tests removing an element.
+     */
+    public void testRemove()
+    {
+        PrefixedKeysIterator it = setUpIterator();
+        checkNext(it, 1);
+        it.remove();
+        assertFalse("Element not removed", keyList.contains(PREFIX + KEY + "1"));
+    }
+
+    /**
+     * Tests calling remove() when this is not allowed.
+     */
+    public void testRemoveInvalid()
+    {
+        PrefixedKeysIterator it = setUpIterator();
+        it.next();
+        it.hasNext();
+        try
+        {
+            it.remove();
+            fail("Could call remove() without a current object!");
+        }
+        catch (IllegalStateException istex)
+        {
+            // ok
+        }
+    }
+}

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPrefixedKeysIterator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message