From 6a787c476f46fffe5c449d5cdcd6f4aab769470c Mon Sep 17 00:00:00 2001 From: Keval Dharmeshbhai Gandevia Date: Fri, 1 Mar 2024 20:59:42 -0400 Subject: [PATCH 1/3] Three new test cases added --- .../commons/collections4/ArrayStackTest.java | 18 ++++++++ .../collections4/EnumerationUtilsTest.java | 35 ++++++++++++---- .../bidimap/UnmodifiableBidiMapTest.java | 41 +++++++++++++++++-- .../iterators/ListIteratorWrapperTest.java | 28 +++++++++++-- 4 files changed, 107 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/ArrayStackTest.java b/src/test/java/org/apache/commons/collections4/ArrayStackTest.java index 3c34cce7ea..318af66fed 100644 --- a/src/test/java/org/apache/commons/collections4/ArrayStackTest.java +++ b/src/test/java/org/apache/commons/collections4/ArrayStackTest.java @@ -101,6 +101,24 @@ public void testSearch() { "Cannot find 'Missing Item'"); } + @Test + public void testPeekForItemsDownInStack() { + // arrange + final ArrayStack stack = makeObject(); + final ArrayStack emptyStack = makeObject(); + stack.push((E) "First"); + stack.push((E) "Second"); + stack.push((E) "Third"); + + // act and assert + // check for valid index + assertEquals("Second", stack.peek(1)); + // check for invalid index + assertThrows(EmptyStackException.class, () -> {stack.peek(5);}); + // check if stack is empty. + assertThrows(EmptyStackException.class, () -> {emptyStack.peek(1);}); + } + // public void testCreate() throws Exception { // resetEmpty(); // writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/ArrayStack.emptyCollection.version4.obj"); diff --git a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java index 00d28520f7..1daee8d695 100644 --- a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java @@ -21,14 +21,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.List; -import java.util.StringTokenizer; -import java.util.Vector; +import java.util.*; +import org.apache.commons.collections4.iterators.EnumerationIterator; import org.junit.jupiter.api.Test; /** @@ -124,4 +119,30 @@ public void testToListWithStringTokenizer() { assertEquals(expectedList2, actualList); } + @Test + public void testRemove() { + // arrange + List list = new ArrayList<>(Arrays.asList("First", "Second", "Third")); + List emptyList = new ArrayList<>(); + Enumeration enumeration1 = Collections.enumeration(list); + Enumeration enumeration2 = Collections.enumeration(list); + Enumeration enumeration3 = Collections.enumeration(emptyList); + + Iterator enumerationIterator1 = new EnumerationIterator<>(enumeration1, list); + Iterator enumerationIterator2 = new EnumerationIterator<>(enumeration2, list); + Iterator enumerationIterator3 = new EnumerationIterator<>(enumeration2); + Iterator enumerationIterator4 = new EnumerationIterator<>(enumeration3, emptyList); + enumerationIterator1.next(); + enumerationIterator1.remove(); + + // act and assert + // test to check for valid remove + assertEquals(Arrays.asList("Second", "Third"), list); + // test to remove without iterator next + assertThrows(IllegalStateException.class, enumerationIterator2::remove); + // test to check if collection is null + assertThrows(UnsupportedOperationException.class, enumerationIterator3::remove); + // test to remove from empty collection + assertThrows(IllegalStateException.class, enumerationIterator4::remove); + } } diff --git a/src/test/java/org/apache/commons/collections4/bidimap/UnmodifiableBidiMapTest.java b/src/test/java/org/apache/commons/collections4/bidimap/UnmodifiableBidiMapTest.java index 774e6b0726..b2133583a9 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/UnmodifiableBidiMapTest.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/UnmodifiableBidiMapTest.java @@ -16,18 +16,18 @@ */ package org.apache.commons.collections4.bidimap; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.apache.commons.collections4.BidiMap; import org.apache.commons.collections4.Unmodifiable; import org.apache.commons.collections4.collection.AbstractCollectionTest; +import org.apache.commons.collections4.map.UnmodifiableEntrySet; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + /** * JUnit tests. */ @@ -82,6 +82,12 @@ public BidiMap makeObject() { return UnmodifiableBidiMap.unmodifiableBidiMap(new DualHashBidiMap<>()); } + public Set> makeCustomFullMap() { + final BidiMap bidi = new DualHashBidiMap<>(); + addSampleMappings(bidi); + return UnmodifiableBidiMap.unmodifiableBidiMap(bidi).entrySet(); + } + @Test public void testDecorateFactory() { final BidiMap map = makeFullMap(); @@ -96,4 +102,31 @@ public void testUnmodifiable() { assertTrue(makeFullMap() instanceof Unmodifiable); } + @Test + public void testToArray() { + // arrange + Set> x = makeCustomFullMap(); + + // act + Object[] array1 = x.toArray(); + Object[] array2 = x.toArray(new Map.Entry[x.size()]); + Object[] array3 = x.toArray(new Map.Entry[0]); + + // assert + // check for same length + assertEquals(18, array1.length); + assertEquals(18, array2.length); + assertEquals(18, array3.length); + + // verify the type for each entry + for (Object obj : array1) { + assertTrue(obj instanceof Map.Entry); + } + for (Object obj : array2) { + assertTrue(obj instanceof Map.Entry); + } + for (Object obj : array3) { + assertTrue(obj instanceof Map.Entry); + } + } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java index c52bef0ffb..4754120486 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java @@ -16,10 +16,6 @@ */ package org.apache.commons.collections4.iterators; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; - import java.util.ArrayList; import java.util.List; import java.util.ListIterator; @@ -29,6 +25,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + /** * Tests the ListIteratorWrapper to ensure that it simulates * a ListIterator correctly. @@ -56,6 +54,11 @@ public ResettableListIterator makeObject() { return new ListIteratorWrapper<>(list1.iterator()); } + public ResettableListIterator makeCustomObject() { + ListIterator listIterator= list1.listIterator(); + return new ListIteratorWrapper<>(listIterator); + } + @BeforeEach @SuppressWarnings("unchecked") public void setUp() { @@ -223,4 +226,21 @@ public void testReset() { } } + @Test + public void testAdd() { + + // arrange part starts + final ResettableListIterator iter1 = makeCustomObject(); + final ResettableListIterator iter2 = makeObject(); + int[] x = new int[]{1}; + // arrange part ends + + // act and assert starts + // check for no exception + assertDoesNotThrow(()->{iter1.add((E) x);}); + + // check for exception + assertThrows(UnsupportedOperationException.class,()-> {iter2.add((E)"e");}); + // act and assert ends + } } From 76b60ca61c60a725bbced4c68662c78cb5408dc6 Mon Sep 17 00:00:00 2001 From: Keval Dharmeshbhai Gandevia Date: Sat, 30 Mar 2024 02:16:24 -0300 Subject: [PATCH 2/3] refactor implementation smells --- .../commons/collections4/CollectionUtils.java | 41 +++++++++++-------- .../commons/collections4/ListUtils.java | 14 ++++++- .../collections4/list/SetUniqueList.java | 6 +-- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index 5a62e92697..df751c3734 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -1058,36 +1058,45 @@ public static Object get(final Object object, final int index) { if (i < 0) { throw new IndexOutOfBoundsException("Index cannot be negative: " + i); } + if (object == null) { + throw new IllegalArgumentException("Unsupported object type: null"); + } + return retrieveElement(object, index); + } + + /** + * Retrieves the element at index 'i' from various types of objects. + * @param object - The object from which to retrieve the element. + * @param i - The index of the element to retrieve. + * @return - The element at index 'i' from the provided object. + * @throws - IllegalArgumentException if the provided object type is unsupported. + */ + private static Object retrieveElement(final Object object, final int i) { if (object instanceof Map) { final Map map = (Map) object; final Iterator iterator = map.entrySet().iterator(); return IteratorUtils.get(iterator, i); - } - if (object instanceof Object[]) { + } else if (object instanceof Object[]) { return ((Object[]) object)[i]; - } - if (object instanceof Iterator) { + } else if (object instanceof Iterator) { final Iterator it = (Iterator) object; return IteratorUtils.get(it, i); - } - if (object instanceof Iterable) { + } else if (object instanceof Iterable) { final Iterable iterable = (Iterable) object; return IterableUtils.get(iterable, i); - } - if (object instanceof Enumeration) { + } else if (object instanceof Enumeration) { final Enumeration it = (Enumeration) object; return EnumerationUtils.get(it, i); - } - if (object == null) { - throw new IllegalArgumentException("Unsupported object type: null"); - } - try { - return Array.get(object, i); - } catch (final IllegalArgumentException ex) { - throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); + } else { + try { + return Array.get(object, i); + } catch (final IllegalArgumentException ex) { + throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); + } } } + /** * Returns a {@link Map} mapping each unique element in the given * {@link Collection} to an {@link Integer} representing the number diff --git a/src/main/java/org/apache/commons/collections4/ListUtils.java b/src/main/java/org/apache/commons/collections4/ListUtils.java index f7eb95c5eb..e6eeb0d29f 100644 --- a/src/main/java/org/apache/commons/collections4/ListUtils.java +++ b/src/main/java/org/apache/commons/collections4/ListUtils.java @@ -314,7 +314,8 @@ public static boolean isEqualList(final Collection list1, final Collection if (list1 == list2) { return true; } - if (list1 == null || list2 == null || list1.size() != list2.size()) { + + if(checkCollectionSize(list1, list2)) { return false; } @@ -333,6 +334,17 @@ public static boolean isEqualList(final Collection list1, final Collection return !(it1.hasNext() || it2.hasNext()); } + /** + * Checks if two collections are not equal in size or if either of them is null. + * + * @param list1 The first collection to compare. + * @param list2 The second collection to compare. + * @return True if the collections are not equal in size or if either is null, false otherwise. + */ + private static boolean checkCollectionSize(final Collection list1, final Collection list2) { + return (list1 == null || list2 == null || list1.size() != list2.size()); + } + /** * Returns a "lazy" list whose elements will be created on demand. *

diff --git a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java index ff9a9f68c0..edb6b20783 100644 --- a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java +++ b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java @@ -398,13 +398,13 @@ public boolean retainAll(final Collection coll) { */ @Override public E set(final int index, final E object) { - final int pos = indexOf(object); + final int position = indexOf(object); final E removed = super.set(index, object); - if (pos != -1 && pos != index) { + if (position != -1 && position != index) { // the object is already in the unique list // (and it hasn't been swapped with itself) - super.remove(pos); // remove the duplicate by index + super.remove(position); // remove the duplicate by index } set.remove(removed); // remove the item deleted by the set From 5187835c31277e2a74e8301071c6cbd2cef09177 Mon Sep 17 00:00:00 2001 From: Keval Dharmeshbhai Gandevia Date: Sun, 31 Mar 2024 21:57:23 -0300 Subject: [PATCH 3/3] refactor design smells --- .../collections4/CardinalityHelper.java | 98 +++++++++++ .../commons/collections4/CollectionUtils.java | 157 ------------------ .../commons/collections4/EquatorWrapper.java | 56 +++++++ .../SetOperationCardinalityHelper.java | 73 ++++++++ .../bloomfilter/IndexProducer.java | 19 ++- .../collections4/bloomfilter/IndexUtils.java | 47 ------ .../collections4/list/AbstractLinkedList.java | 24 +++ .../list/CursorableLinkedList.java | 22 --- .../list/NodeCachingLinkedList.java | 22 --- 9 files changed, 269 insertions(+), 249 deletions(-) create mode 100644 src/main/java/org/apache/commons/collections4/CardinalityHelper.java create mode 100644 src/main/java/org/apache/commons/collections4/EquatorWrapper.java create mode 100644 src/main/java/org/apache/commons/collections4/SetOperationCardinalityHelper.java delete mode 100644 src/main/java/org/apache/commons/collections4/bloomfilter/IndexUtils.java diff --git a/src/main/java/org/apache/commons/collections4/CardinalityHelper.java b/src/main/java/org/apache/commons/collections4/CardinalityHelper.java new file mode 100644 index 0000000000..3fb2aa01ba --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/CardinalityHelper.java @@ -0,0 +1,98 @@ +/* + * 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.collections4; + + +import java.util.Map; + +/** + * Helper class to easily access cardinality properties of two collections. + * + * @param the element type + */ + +public class CardinalityHelper { + /** + * Contains the cardinality for each object in collection A. + */ + final Map cardinalityA; + + /** + * Contains the cardinality for each object in collection B. + */ + final Map cardinalityB; + + /** + * Create a new CardinalityHelper for two collections. + * + * @param a the first collection + * @param b the second collection + */ + CardinalityHelper(final Iterable a, final Iterable b) { + cardinalityA = CollectionUtils.getCardinalityMap(a); + cardinalityB = CollectionUtils.getCardinalityMap(b); + } + + /** + * Returns the frequency of this object in collection A. + * + * @param obj the object + * @return the frequency of the object in collection A + */ + public int freqA(final Object obj) { + return getFreq(obj, cardinalityA); + } + + /** + * Returns the frequency of this object in collection B. + * + * @param obj the object + * @return the frequency of the object in collection B + */ + public int freqB(final Object obj) { + return getFreq(obj, cardinalityB); + } + + private int getFreq(final Object obj, final Map freqMap) { + final Integer count = freqMap.get(obj); + if (count != null) { + return count.intValue(); + } + return 0; + } + + /** + * Returns the maximum frequency of an object. + * + * @param obj the object + * @return the maximum frequency of the object + */ + public final int max(final Object obj) { + return Math.max(freqA(obj), freqB(obj)); + } + + /** + * Returns the minimum frequency of an object. + * + * @param obj the object + * @return the minimum frequency of the object + */ + public final int min(final Object obj) { + return Math.min(freqA(obj), freqB(obj)); + } +} diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index df751c3734..7faf7d8ebb 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -56,163 +56,6 @@ */ public class CollectionUtils { - /** - * Helper class to easily access cardinality properties of two collections. - * @param the element type - */ - private static class CardinalityHelper { - - /** Contains the cardinality for each object in collection A. */ - final Map cardinalityA; - - /** Contains the cardinality for each object in collection B. */ - final Map cardinalityB; - - /** - * Create a new CardinalityHelper for two collections. - * @param a the first collection - * @param b the second collection - */ - CardinalityHelper(final Iterable a, final Iterable b) { - cardinalityA = CollectionUtils.getCardinalityMap(a); - cardinalityB = CollectionUtils.getCardinalityMap(b); - } - - /** - * Returns the frequency of this object in collection A. - * @param obj the object - * @return the frequency of the object in collection A - */ - public int freqA(final Object obj) { - return getFreq(obj, cardinalityA); - } - - /** - * Returns the frequency of this object in collection B. - * @param obj the object - * @return the frequency of the object in collection B - */ - public int freqB(final Object obj) { - return getFreq(obj, cardinalityB); - } - - private int getFreq(final Object obj, final Map freqMap) { - final Integer count = freqMap.get(obj); - if (count != null) { - return count.intValue(); - } - return 0; - } - - /** - * Returns the maximum frequency of an object. - * @param obj the object - * @return the maximum frequency of the object - */ - public final int max(final Object obj) { - return Math.max(freqA(obj), freqB(obj)); - } - - /** - * Returns the minimum frequency of an object. - * @param obj the object - * @return the minimum frequency of the object - */ - public final int min(final Object obj) { - return Math.min(freqA(obj), freqB(obj)); - } - } - - /** - * Wraps another object and uses the provided Equator to implement - * {@link #equals(Object)} and {@link #hashCode()}. - *

- * This class can be used to store objects into a Map. - *

- * - * @param the element type - * @since 4.0 - */ - private static final class EquatorWrapper { - private final Equator equator; - private final O object; - - EquatorWrapper(final Equator equator, final O object) { - this.equator = equator; - this.object = object; - } - - @Override - public boolean equals(final Object obj) { - if (!(obj instanceof EquatorWrapper)) { - return false; - } - @SuppressWarnings("unchecked") - final EquatorWrapper otherObj = (EquatorWrapper) obj; - return equator.equate(object, otherObj.getObject()); - } - - public O getObject() { - return object; - } - - @Override - public int hashCode() { - return equator.hash(object); - } - } - - /** - * Helper class for set-related operations, e.g. union, subtract, intersection. - * @param the element type - */ - private static final class SetOperationCardinalityHelper extends CardinalityHelper implements Iterable { - - /** Contains the unique elements of the two collections. */ - private final Set elements; - - /** Output collection. */ - private final List newList; - - /** - * Create a new set operation helper from the two collections. - * @param a the first collection - * @param b the second collection - */ - SetOperationCardinalityHelper(final Iterable a, final Iterable b) { - super(a, b); - elements = new HashSet<>(); - addAll(elements, a); - addAll(elements, b); - // the resulting list must contain at least each unique element, but may grow - newList = new ArrayList<>(elements.size()); - } - - @Override - public Iterator iterator() { - return elements.iterator(); - } - - /** - * Returns the resulting collection. - * @return the result - */ - public Collection list() { - return newList; - } - - /** - * Add the object {@code count} times to the result collection. - * @param obj the object to add - * @param count the count - */ - public void setCardinality(final O obj, final int count) { - for (int i = 0; i < count; i++) { - newList.add(obj); - } - } - - } /** * The index value when an element is not found in a collection or array: {@code -1}. diff --git a/src/main/java/org/apache/commons/collections4/EquatorWrapper.java b/src/main/java/org/apache/commons/collections4/EquatorWrapper.java new file mode 100644 index 0000000000..c60bcd78a8 --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/EquatorWrapper.java @@ -0,0 +1,56 @@ +/* + * 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.collections4; + +/** + * Wraps another object and uses the provided Equator to implement + * {@link #equals(Object)} and {@link #hashCode()}. + *

+ * This class can be used to store objects into a Map. + *

+ * + * @param the element type + * @since 4.0 + */ +public class EquatorWrapper { + private final Equator equator; + private final O object; + + EquatorWrapper(final Equator equator, final O object) { + this.equator = equator; + this.object = object; + } + + @Override + public boolean equals(final Object obj) { + if (!(obj instanceof EquatorWrapper)) { + return false; + } + @SuppressWarnings("unchecked") final EquatorWrapper otherObj = (EquatorWrapper) obj; + return equator.equate(object, otherObj.getObject()); + } + + public O getObject() { + return object; + } + + @Override + public int hashCode() { + return equator.hash(object); + } +} diff --git a/src/main/java/org/apache/commons/collections4/SetOperationCardinalityHelper.java b/src/main/java/org/apache/commons/collections4/SetOperationCardinalityHelper.java new file mode 100644 index 0000000000..923e455e31 --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/SetOperationCardinalityHelper.java @@ -0,0 +1,73 @@ +/* + * 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.collections4; + +import java.util.*; + +public class SetOperationCardinalityHelper extends CardinalityHelper implements Iterable { + /** + * Contains the unique elements of the two collections. + */ + private final Set elements; + + /** + * Output collection. + */ + private final List newList; + + /** + * Create a new set operation helper from the two collections. + * + * @param a the first collection + * @param b the second collection + */ + SetOperationCardinalityHelper(final Iterable a, final Iterable b) { + super(a, b); + elements = new HashSet<>(); + CollectionUtils.addAll(elements, a); + CollectionUtils.addAll(elements, b); + // the resulting list must contain at least each unique element, but may grow + newList = new ArrayList<>(elements.size()); + } + + @Override + public Iterator iterator() { + return elements.iterator(); + } + + /** + * Returns the resulting collection. + * + * @return the result + */ + public Collection list() { + return newList; + } + + /** + * Add the object {@code count} times to the result collection. + * + * @param obj the object to add + * @param count the count + */ + public void setCardinality(final O obj, final int count) { + for (int i = 0; i < count; i++) { + newList.add(obj); + } + } +} diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java b/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java index 7923288765..989deeb49f 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/IndexProducer.java @@ -33,6 +33,7 @@ @FunctionalInterface public interface IndexProducer { + /** * Creates an IndexProducer from a {@code BitMapProducer}. * @param producer the {@code BitMapProducer} @@ -106,7 +107,7 @@ class Indices { private int size; boolean add(final int index) { - data = IndexUtils.ensureCapacityForAdd(data, size); + data = ensureCapacityForAdd(data, size); data[size++] = index; return true; } @@ -173,4 +174,20 @@ public IndexProducer uniqueIndices() { } }; } + + /** + * Ensure the array can add an element at the specified index. + * @param array the array to check. + * @param index the index to add at. + * @return the array or a newly allocated copy of the array. + */ + static int[] ensureCapacityForAdd(final int[] array, final int index) { + + final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + + if (index >= array.length) { + return Arrays.copyOf(array, (int) Math.min(MAX_ARRAY_SIZE, Math.max(array.length * 2L, index + 1))); + } + return array; + } } diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/IndexUtils.java b/src/main/java/org/apache/commons/collections4/bloomfilter/IndexUtils.java deleted file mode 100644 index bea31d733b..0000000000 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/IndexUtils.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.collections4.bloomfilter; - -import java.util.Arrays; - -/** - * Provides functions to assist in IndexProducer creation and manipulation. - * @see IndexProducer - */ -final class IndexUtils { - - /** - * The maximum array size for the methods in this class. - */ - static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; - - /** - * Ensure the array can add an element at the specified index. - * @param array the array to check. - * @param index the index to add at. - * @return the array or a newly allocated copy of the array. - */ - static int[] ensureCapacityForAdd(final int[] array, final int index) { - if (index >= array.length) { - return Arrays.copyOf(array, (int) Math.min(IndexUtils.MAX_ARRAY_SIZE, Math.max(array.length * 2L, index + 1))); - } - return array; - } - - // do not instantiate - private IndexUtils() {} -} diff --git a/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java b/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java index aecb2e9482..8f2251c082 100644 --- a/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java +++ b/src/main/java/org/apache/commons/collections4/list/AbstractLinkedList.java @@ -1076,4 +1076,28 @@ protected void updateNode(final Node node, final E value) { node.setValue(value); } + + /** + * Deserializes the data held in this object to the stream specified. + * + * @param in the input stream + * @throws IOException if an error occurs while reading from the stream + * @throws ClassNotFoundException if an object read from the stream can not be loaded + */ + private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + doReadObject(in); + } + + /** + * Serializes the data held in this object to the stream specified. + * + * @param out the output stream + * @throws IOException if an error occurs while writing to the stream + */ + private void writeObject(final ObjectOutputStream out) throws IOException { + out.defaultWriteObject(); + doWriteObject(out); + } + } diff --git a/src/main/java/org/apache/commons/collections4/list/CursorableLinkedList.java b/src/main/java/org/apache/commons/collections4/list/CursorableLinkedList.java index 12bce5c704..d9205d65e6 100644 --- a/src/main/java/org/apache/commons/collections4/list/CursorableLinkedList.java +++ b/src/main/java/org/apache/commons/collections4/list/CursorableLinkedList.java @@ -515,18 +515,6 @@ public ListIterator listIterator(final int fromIndex) { return cursor(fromIndex); } - /** - * Deserializes the data held in this object to the stream specified. - * - * @param in the input stream - * @throws IOException if an error occurs while reading from the stream - * @throws ClassNotFoundException if an object read from the stream can not be loaded - */ - private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - doReadObject(in); - } - /** * Registers a cursor to be notified of changes to this list. * @@ -602,15 +590,5 @@ protected void updateNode(final Node node, final E value) { broadcastNodeChanged(node); } - /** - * Serializes the data held in this object to the stream specified. - * - * @param out the output stream - * @throws IOException if an error occurs while writing to the stream - */ - private void writeObject(final ObjectOutputStream out) throws IOException { - out.defaultWriteObject(); - doWriteObject(out); - } } diff --git a/src/main/java/org/apache/commons/collections4/list/NodeCachingLinkedList.java b/src/main/java/org/apache/commons/collections4/list/NodeCachingLinkedList.java index 1b379dd4db..c2d2d6e10d 100644 --- a/src/main/java/org/apache/commons/collections4/list/NodeCachingLinkedList.java +++ b/src/main/java/org/apache/commons/collections4/list/NodeCachingLinkedList.java @@ -169,18 +169,6 @@ protected boolean isCacheFull() { return cacheSize >= maximumCacheSize; } - /** - * Deserializes the data held in this object to the stream specified. - * - * @param in the input stream - * @throws IOException if an error occurs while reading from the stream - * @throws ClassNotFoundException if an object read from the stream can not be loaded - */ - private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - doReadObject(in); - } - /** * Removes all the nodes from the list, storing as many as required in the * cache for reuse. @@ -233,15 +221,5 @@ protected void shrinkCacheToMaximumSize() { } } - /** - * Serializes the data held in this object to the stream specified. - * - * @param out the output stream - * @throws IOException if an error occurs while writing to the stream - */ - private void writeObject(final ObjectOutputStream out) throws IOException { - out.defaultWriteObject(); - doWriteObject(out); - } }