From cce4634e79fd3d74d6086ae09672fc716509c4c9 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Mon, 18 Mar 2013 17:16:42 +0000 Subject: [PATCH] Use final for read-only local variables. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1457876 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/CollectionUtils.java | 1 + .../bidimap/DualLinkedHashBidiMap.java | 1 + .../collection/IndexedCollection.java | 1 + .../commons/collections/list/TreeList.java | 4 ++-- .../commons/collections/map/MultiKeyMap.java | 2 +- .../collections/CollectionUtilsTest.java | 8 ++++---- .../commons/collections/IteratorUtilsTest.java | 18 +++++++++--------- .../collection/IndexedCollectionTest.java | 10 +++++----- .../iterators/NodeListIteratorTest.java | 17 +++++++++-------- 9 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/apache/commons/collections/CollectionUtils.java b/src/main/java/org/apache/commons/collections/CollectionUtils.java index a849f0a82..44900beb1 100644 --- a/src/main/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections/CollectionUtils.java @@ -584,6 +584,7 @@ public class CollectionUtils { return false; } @SuppressWarnings("unchecked") + final EquatorWrapper otherObj = (EquatorWrapper) obj; return equator.equate(object, otherObj.getObject()); } diff --git a/src/main/java/org/apache/commons/collections/bidimap/DualLinkedHashBidiMap.java b/src/main/java/org/apache/commons/collections/bidimap/DualLinkedHashBidiMap.java index 9f0a9bacb..647a02147 100644 --- a/src/main/java/org/apache/commons/collections/bidimap/DualLinkedHashBidiMap.java +++ b/src/main/java/org/apache/commons/collections/bidimap/DualLinkedHashBidiMap.java @@ -94,6 +94,7 @@ public class DualLinkedHashBidiMap extends AbstractDualBidiMap imple normalMap = new LinkedHashMap(); reverseMap = new LinkedHashMap(); @SuppressWarnings("unchecked") // will fail at runtime if stream is incorrect + final Map map = (Map) in.readObject(); putAll(map); } diff --git a/src/main/java/org/apache/commons/collections/collection/IndexedCollection.java b/src/main/java/org/apache/commons/collections/collection/IndexedCollection.java index 7f98140ea..a74f9a4c9 100644 --- a/src/main/java/org/apache/commons/collections/collection/IndexedCollection.java +++ b/src/main/java/org/apache/commons/collections/collection/IndexedCollection.java @@ -176,6 +176,7 @@ public class IndexedCollection extends AbstractCollectionDecorator { */ public C get(final K key) { @SuppressWarnings("unchecked") // index is a MultiMap which returns a Collection + final Collection coll = (Collection) index.get(key); return coll == null ? null : coll.iterator().next(); } diff --git a/src/main/java/org/apache/commons/collections/list/TreeList.java b/src/main/java/org/apache/commons/collections/list/TreeList.java index 390ed338d..9e0f89143 100644 --- a/src/main/java/org/apache/commons/collections/list/TreeList.java +++ b/src/main/java/org/apache/commons/collections/list/TreeList.java @@ -429,7 +429,7 @@ public class TreeList extends AbstractList { if (relativePosition >= 0) { relativePosition++; } - AVLNode ret = balance(); + final AVLNode ret = balance(); recalcHeight(); return ret; } @@ -443,7 +443,7 @@ public class TreeList extends AbstractList { if (relativePosition < 0) { relativePosition--; } - AVLNode ret = balance(); + final AVLNode ret = balance(); recalcHeight(); return ret; } diff --git a/src/main/java/org/apache/commons/collections/map/MultiKeyMap.java b/src/main/java/org/apache/commons/collections/map/MultiKeyMap.java index 87491fc9e..8f558233d 100644 --- a/src/main/java/org/apache/commons/collections/map/MultiKeyMap.java +++ b/src/main/java/org/apache/commons/collections/map/MultiKeyMap.java @@ -823,7 +823,7 @@ public class MultiKeyMap extends AbstractMapDecorator clone() { try { return (MultiKeyMap) super.clone(); - } catch (CloneNotSupportedException e) { + } catch (final CloneNotSupportedException e) { throw new InternalError(); } } diff --git a/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java index 5f8bdfbb5..abb1633c9 100644 --- a/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java +++ b/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java @@ -510,7 +510,7 @@ public class CollectionUtilsTest extends MockTestCase { @Test public void testIsEqualCollectionEquator() { - Collection collB = CollectionUtils.collect(collectionB, TRANSFORM_TO_INTEGER); + final Collection collB = CollectionUtils.collect(collectionB, TRANSFORM_TO_INTEGER); // odd / even equator final Equator e = new Equator() { @@ -993,12 +993,12 @@ public class CollectionUtilsTest extends MockTestCase { @Test public void filterInverse() { - List ints = new ArrayList(); + final List ints = new ArrayList(); ints.add(1); ints.add(2); ints.add(3); ints.add(3); - Iterable iterable = ints; + final Iterable iterable = ints; assertTrue(CollectionUtils.filterInverse(iterable, EQUALS_TWO)); assertEquals(3, ints.size()); assertEquals(1, (int) ints.get(0)); @@ -1008,7 +1008,7 @@ public class CollectionUtilsTest extends MockTestCase { @Test public void filterInverseNullParameters() throws Exception { - List longs = Collections.nCopies(4, 10L); + final List longs = Collections.nCopies(4, 10L); assertFalse(CollectionUtils.filterInverse(longs, null)); assertEquals(4, longs.size()); assertFalse(CollectionUtils.filterInverse(null, EQUALS_TWO)); diff --git a/src/test/java/org/apache/commons/collections/IteratorUtilsTest.java b/src/test/java/org/apache/commons/collections/IteratorUtilsTest.java index 1f7190678..3ec297e54 100644 --- a/src/test/java/org/apache/commons/collections/IteratorUtilsTest.java +++ b/src/test/java/org/apache/commons/collections/IteratorUtilsTest.java @@ -795,8 +795,8 @@ public class IteratorUtilsTest extends BulkTest { * Tests method nodeListIterator(NodeList) */ public void testNodeListIterator() { - Node[] nodes = createNodes(); - NodeList nodeList = createNodeList(nodes); + final Node[] nodes = createNodes(); + final NodeList nodeList = createNodeList(nodes); final Iterator iterator = IteratorUtils.nodeListIterator(nodeList); int expectedNodeIndex = 0; @@ -817,9 +817,9 @@ public class IteratorUtilsTest extends BulkTest { * Tests method nodeListIterator(Node) */ public void testNodeIterator() { - Node[] nodes = createNodes(); - NodeList nodeList = createNodeList(nodes); - Node parentNode = createMock(Node.class); + final Node[] nodes = createNodes(); + final NodeList nodeList = createNodeList(nodes); + final Node parentNode = createMock(Node.class); expect(parentNode.getChildNodes()).andStubReturn(nodeList); replay(parentNode); @@ -844,10 +844,10 @@ public class IteratorUtilsTest extends BulkTest { * @return */ private Node[] createNodes() { - Node node1 = createMock(Node.class); - Node node2 = createMock(Node.class); - Node node3 = createMock(Node.class); - Node node4 = createMock(Node.class); + final Node node1 = createMock(Node.class); + final Node node2 = createMock(Node.class); + final Node node3 = createMock(Node.class); + final Node node4 = createMock(Node.class); replay(node1); replay(node2); replay(node3); diff --git a/src/test/java/org/apache/commons/collections/collection/IndexedCollectionTest.java b/src/test/java/org/apache/commons/collections/collection/IndexedCollectionTest.java index 10c071842..4e71f636b 100644 --- a/src/test/java/org/apache/commons/collections/collection/IndexedCollectionTest.java +++ b/src/test/java/org/apache/commons/collections/collection/IndexedCollectionTest.java @@ -133,14 +133,14 @@ public class IndexedCollectionTest extends AbstractCollectionTest { try { coll.add("1"); fail(); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { // expected } } public void testDecoratedCollectionIsIndexedOnCreation() throws Exception { - Collection original = makeFullCollection(); - IndexedCollection indexed = decorateUniqueCollection(original); + final Collection original = makeFullCollection(); + final IndexedCollection indexed = decorateUniqueCollection(original); assertEquals("1", indexed.get(1)); assertEquals("2", indexed.get(2)); @@ -148,8 +148,8 @@ public class IndexedCollectionTest extends AbstractCollectionTest { } public void testReindexUpdatesIndexWhenDecoratedCollectionIsModifiedSeparately() throws Exception { - Collection original = new ArrayList(); - IndexedCollection indexed = decorateUniqueCollection(original); + final Collection original = new ArrayList(); + final IndexedCollection indexed = decorateUniqueCollection(original); original.add("1"); original.add("2"); diff --git a/src/test/java/org/apache/commons/collections/iterators/NodeListIteratorTest.java b/src/test/java/org/apache/commons/collections/iterators/NodeListIteratorTest.java index 13c32e116..273717843 100644 --- a/src/test/java/org/apache/commons/collections/iterators/NodeListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections/iterators/NodeListIteratorTest.java @@ -54,10 +54,10 @@ public class NodeListIteratorTest extends AbstractIteratorTest { // create mocked Node Instances and fill Node[] to be used by test cases - Node node1 = createMock(Element.class); - Node node2 = createMock(Element.class); - Node node3 = createMock(Text.class); - Node node4 = createMock(Element.class); + final Node node1 = createMock(Element.class); + final Node node2 = createMock(Element.class); + final Node node3 = createMock(Text.class); + final Node node4 = createMock(Element.class); nodes = new Node[] {node1, node2, node3, node4}; replay(node1); @@ -68,7 +68,7 @@ public class NodeListIteratorTest extends AbstractIteratorTest { @Override public Iterator makeEmptyIterator() { - NodeList emptyNodeList = new NodeList() { + final NodeList emptyNodeList = new NodeList() { public Node item(final int index) { throw new IndexOutOfBoundsException(); } @@ -80,7 +80,7 @@ public class NodeListIteratorTest extends AbstractIteratorTest { if (createIteratorWithStandardConstr) { return new NodeListIterator(emptyNodeList); } else { - Node parentNode = createMock(Node.class); + final Node parentNode = createMock(Node.class); expect(parentNode.getChildNodes()).andStubReturn(emptyNodeList); replay(parentNode); @@ -90,7 +90,7 @@ public class NodeListIteratorTest extends AbstractIteratorTest { @Override public Iterator makeObject() { - NodeList nodeList = new NodeList() { + final NodeList nodeList = new NodeList() { public Node item(final int index) { return nodes[index]; } @@ -111,9 +111,10 @@ public class NodeListIteratorTest extends AbstractIteratorTest { public void testNullConstructor(){ try{ @SuppressWarnings("unused") + final NodeListIterator iter = new NodeListIterator((Node) null); fail("IllegalArgumentException expected!"); - }catch(IllegalArgumentException e){ + }catch(final IllegalArgumentException e){ // expected. } }