From 5051dbf0b5f773943e9e640937ee6eef6d28c6b3 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 12 May 2024 08:38:43 -0400 Subject: [PATCH] Sort members --- .../bloomfilter/LayerManager.java | 64 +++---- .../bloomfilter/LayeredBloomFilter.java | 20 +-- .../bloomfilter/WrappedBloomFilter.java | 8 +- .../bloomfilter/LayeredBloomFilterTest.java | 166 +++++++++--------- ...efaultAbstractLinkedListForJava21Test.java | 72 ++++---- 5 files changed, 165 insertions(+), 165 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java b/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java index 0e984b0aa..926afde2f 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java @@ -299,6 +299,18 @@ public class LayerManager implements BloomFilterProducer filters.add(bf); } + /** + * Forces execution the configured cleanup without creating a new filter except in cases + * where the cleanup removes all the layers. + * @see LayerManager.Builder#setCleanup(Consumer) + */ + void cleanup() { + this.filterCleanup.accept(filters); + if (filters.isEmpty()) { + addFilter(); + } + } + /** * Removes all the filters from the layer manager, and sets up a new one as the * target. @@ -325,6 +337,16 @@ public class LayerManager implements BloomFilterProducer return newMgr; } + /** + * Gets the Bloom filter from the first layer. + * No extension check is performed during this call. + * @return The Bloom filter from the first layer. + * @see #getTarget() + */ + public final T first() { + return filters.getFirst(); + } + /** * Executes a Bloom filter Predicate on each Bloom filter in the manager in * depth order. Oldest filter first. @@ -369,26 +391,6 @@ public class LayerManager implements BloomFilterProducer return filters.size(); } - /** - * Gets the Bloom filter from the first layer. - * No extension check is performed during this call. - * @return The Bloom filter from the first layer. - * @see #getTarget() - */ - public final T first() { - return filters.getFirst(); - } - - /** - * Gets the Bloom filter from the last layer. - * No extension check is performed during this call. - * @return The Bloom filter from the last layer. - * @see #getTarget() - */ - public final T last() { - return filters.getLast(); - } - /** * Returns the current target filter. If a new filter should be created based on * {@code extendCheck} it will be created before this method returns. @@ -402,6 +404,16 @@ public class LayerManager implements BloomFilterProducer return last(); } + /** + * Gets the Bloom filter from the last layer. + * No extension check is performed during this call. + * @return The Bloom filter from the last layer. + * @see #getTarget() + */ + public final T last() { + return filters.getLast(); + } + /** * Forces an advance to the next depth. This method will clean-up the current * layers and generate a new filter layer. In most cases is it unnecessary to @@ -417,16 +429,4 @@ public class LayerManager implements BloomFilterProducer this.filterCleanup.accept(filters); addFilter(); } - - /** - * Forces execution the configured cleanup without creating a new filter except in cases - * where the cleanup removes all the layers. - * @see LayerManager.Builder#setCleanup(Consumer) - */ - void cleanup() { - this.filterCleanup.accept(filters); - if (filters.isEmpty()) { - addFilter(); - } - } } diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilter.java b/src/main/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilter.java index 0a79537dc..cb83ab4a0 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilter.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilter.java @@ -115,6 +115,16 @@ public class LayeredBloomFilter implements BloomFilter, B return 0; } + /** + * Forces the execution of the cleanup Consumer that was provided when the associated LayerManager + * was built. + * + * @see LayerManager.Builder#setCleanup(java.util.function.Consumer) + */ + public void cleanup() { + layerManager.cleanup(); + } + @Override public final void clear() { layerManager.clear(); @@ -366,14 +376,4 @@ public class LayeredBloomFilter implements BloomFilter, B public void next() { layerManager.next(); } - - /** - * Forces the execution of the cleanup Consumer that was provided when the associated LayerManager - * was built. - * - * @see LayerManager.Builder#setCleanup(java.util.function.Consumer) - */ - public void cleanup() { - layerManager.cleanup(); - } } diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java b/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java index 18aa6f5d8..60f734f87 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java @@ -36,10 +36,6 @@ public abstract class WrappedBloomFilter implements BloomFilter { this.wrapped = bf; } - protected BloomFilter getWrapped() { - return wrapped; - } - @Override public long[] asBitMapArray() { return wrapped.asBitMapArray(); @@ -120,6 +116,10 @@ public abstract class WrappedBloomFilter implements BloomFilter { return wrapped.getShape(); } + protected BloomFilter getWrapped() { + return wrapped; + } + @Override public boolean isFull() { return wrapped.isFull(); diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java index 25f807a80..d98385861 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java @@ -36,36 +36,6 @@ import org.junit.jupiter.api.Test; public class LayeredBloomFilterTest extends AbstractBloomFilterTest> { - /** - * Creates a fixed size layered bloom filter that adds new filters to the list, - * but never merges them. List will never exceed maxDepth. As additional filters - * are added earlier filters are removed. Uses SimpleBloomFilters. - * - * @param shape The shape for the enclosed Bloom filters. - * @param maxDepth The maximum depth of layers. - * @return An empty layered Bloom filter of the specified shape and depth. - */ - public static LayeredBloomFilter fixed(final Shape shape, int maxDepth) { - return fixed(shape, maxDepth, () -> new SimpleBloomFilter(shape)); - } - - /** - * Creates a fixed size layered bloom filter that adds new filters to the list, - * but never merges them. List will never exceed maxDepth. As additional filters - * are added earlier filters are removed. - * - * @param shape The shape for the enclosed Bloom filters. - * @param maxDepth The maximum depth of layers. - * @param supplier A supplier of the Bloom filters to create layers with. - * @return An empty layered Bloom filter of the specified shape and depth. - */ - public static LayeredBloomFilter fixed(final Shape shape, int maxDepth, Supplier supplier) { - LayerManager.Builder builder = LayerManager.builder(); - builder.setExtendCheck(LayerManager.ExtendCheck.advanceOnPopulated()) - .setCleanup(LayerManager.Cleanup.onMaxSize(maxDepth)).setSupplier(supplier); - return new LayeredBloomFilter<>(shape, builder.build()); - } - /** * A Predicate that advances after a quantum of time. */ @@ -111,6 +81,21 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest(shape, layerManager); } + /** + * Creates a fixed size layered bloom filter that adds new filters to the list, + * but never merges them. List will never exceed maxDepth. As additional filters + * are added earlier filters are removed. Uses SimpleBloomFilters. + * + * @param shape The shape for the enclosed Bloom filters. + * @param maxDepth The maximum depth of layers. + * @return An empty layered Bloom filter of the specified shape and depth. + */ + public static LayeredBloomFilter fixed(final Shape shape, int maxDepth) { + return fixed(shape, maxDepth, () -> new SimpleBloomFilter(shape)); + } + + /** + * Creates a fixed size layered bloom filter that adds new filters to the list, + * but never merges them. List will never exceed maxDepth. As additional filters + * are added earlier filters are removed. + * + * @param shape The shape for the enclosed Bloom filters. + * @param maxDepth The maximum depth of layers. + * @param supplier A supplier of the Bloom filters to create layers with. + * @return An empty layered Bloom filter of the specified shape and depth. + */ + public static LayeredBloomFilter fixed(final Shape shape, int maxDepth, Supplier supplier) { + LayerManager.Builder builder = LayerManager.builder(); + builder.setExtendCheck(LayerManager.ExtendCheck.advanceOnPopulated()) + .setCleanup(LayerManager.Cleanup.onMaxSize(maxDepth)).setSupplier(supplier); + return new LayeredBloomFilter<>(shape, builder.build()); + } + // instrumentation to record timestamps in dbgInstrument list private final Predicate dbg = (bf) -> { TimestampedBloomFilter tbf = (TimestampedBloomFilter) bf; @@ -214,6 +229,39 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest(getTestShape(), layerManager)); } + // ***** TESTS THAT CHECK LAYERED PROCESSING ****** + + @Test + public void testCleanup() { + int[] sequence = {1}; + LayerManager layerManager = LayerManager.builder() + .setSupplier(() -> new NumberedBloomFilter(getTestShape(), 3, sequence[0]++)) + .setExtendCheck(ExtendCheck.neverAdvance()) + .setCleanup(ll -> ll.removeIf( f -> (((NumberedBloomFilter) f).value-- == 0))).build(); + LayeredBloomFilter underTest = new LayeredBloomFilter(getTestShape(), layerManager ); + assertEquals(1, underTest.getDepth()); + underTest.merge(TestingHashers.randomHasher()); + underTest.cleanup(); // first count == 2 + assertEquals(1, underTest.getDepth()); + underTest.next(); // first count == 1 + assertEquals(2, underTest.getDepth()); + underTest.merge(TestingHashers.randomHasher()); + underTest.cleanup(); // first count == 0 + NumberedBloomFilter f = (NumberedBloomFilter) underTest.get(0); + assertEquals(1, f.sequence); + + assertEquals(2, underTest.getDepth()); + underTest.cleanup(); // should be removed ; second is now 1st with value 1 + assertEquals(1, underTest.getDepth()); + f = (NumberedBloomFilter) underTest.get(0); + assertEquals(2, f.sequence); + + underTest.cleanup(); // first count == 0 + underTest.cleanup(); // should be removed. But there is always at least one + assertEquals(1, underTest.getDepth()); + f = (NumberedBloomFilter) underTest.get(0); + assertEquals(3, f.sequence); // it is a new one. + } /** * Tests that the estimated union calculations are correct. */ @@ -227,8 +275,6 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest !lst.contains(((TimestampedBloomFilter) x).timestamp))), "Found filter that should have been deleted: " + dbgInstrument.get(dbgInstrument.size() - 1)); } + @Test public void testFindBitMapProducer() { LayeredBloomFilter filter = setupFindTest(); @@ -359,51 +406,4 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest new NumberedBloomFilter(getTestShape(), 3, sequence[0]++)) - .setExtendCheck(ExtendCheck.neverAdvance()) - .setCleanup(ll -> ll.removeIf( f -> (((NumberedBloomFilter) f).value-- == 0))).build(); - LayeredBloomFilter underTest = new LayeredBloomFilter(getTestShape(), layerManager ); - assertEquals(1, underTest.getDepth()); - underTest.merge(TestingHashers.randomHasher()); - underTest.cleanup(); // first count == 2 - assertEquals(1, underTest.getDepth()); - underTest.next(); // first count == 1 - assertEquals(2, underTest.getDepth()); - underTest.merge(TestingHashers.randomHasher()); - underTest.cleanup(); // first count == 0 - NumberedBloomFilter f = (NumberedBloomFilter) underTest.get(0); - assertEquals(1, f.sequence); - - assertEquals(2, underTest.getDepth()); - underTest.cleanup(); // should be removed ; second is now 1st with value 1 - assertEquals(1, underTest.getDepth()); - f = (NumberedBloomFilter) underTest.get(0); - assertEquals(2, f.sequence); - - underTest.cleanup(); // first count == 0 - underTest.cleanup(); // should be removed. But there is always at least one - assertEquals(1, underTest.getDepth()); - f = (NumberedBloomFilter) underTest.get(0); - assertEquals(3, f.sequence); // it is a new one. - } - - static class NumberedBloomFilter extends WrappedBloomFilter { - int value; - int sequence; - NumberedBloomFilter(Shape shape, int value, int sequence) { - super(new SimpleBloomFilter(shape)); - this.value = value; - this.sequence = sequence; - } - - @Override - public BloomFilter copy() { - return new NumberedBloomFilter(getShape(), value, sequence); - } - } } diff --git a/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java b/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java index 79f4c97af..c8569d883 100644 --- a/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java +++ b/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java @@ -32,6 +32,22 @@ import org.junit.jupiter.api.Test; */ public class DefaultAbstractLinkedListForJava21Test extends AbstractListTest { + private static class DefaultAbstractLinkedListForJava21 extends AbstractLinkedListForJava21 { + DefaultAbstractLinkedListForJava21() { + init(); + } + + private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + doReadObject(in); + } + + private void writeObject(final ObjectOutputStream out) throws IOException { + out.defaultWriteObject(); + doWriteObject(out); + } + } + public DefaultAbstractLinkedListForJava21Test() { super(DefaultAbstractLinkedListForJava21Test.class.getSimpleName()); } @@ -47,6 +63,26 @@ public class DefaultAbstractLinkedListForJava21Test extends AbstractListTest< } } + @Override + public AbstractLinkedListForJava21 getCollection() { + return (AbstractLinkedListForJava21) super.getCollection(); + } + + @Override + public String getCompatibilityVersion() { + return null; + } + + @Override + public List makeObject() { + return new DefaultAbstractLinkedListForJava21<>(); + } + + @Override + protected boolean skipSerializedCanonicalTests() { + return true; + } + @Test @SuppressWarnings("unchecked") public void testAddNodeAfter() { @@ -178,26 +214,6 @@ public class DefaultAbstractLinkedListForJava21Test extends AbstractListTest< checkNodes(); } - @Override - public String getCompatibilityVersion() { - return null; - } - - @Override - protected boolean skipSerializedCanonicalTests() { - return true; - } - - @Override - public AbstractLinkedListForJava21 getCollection() { - return (AbstractLinkedListForJava21) super.getCollection(); - } - - @Override - public List makeObject() { - return new DefaultAbstractLinkedListForJava21<>(); - } - @Test @SuppressWarnings("unchecked") public void testSubList() { @@ -297,20 +313,4 @@ public class DefaultAbstractLinkedListForJava21Test extends AbstractListTest< assertEquals("[]", sublist.toString()); assertEquals("[A, E]", list.toString()); } - - private static class DefaultAbstractLinkedListForJava21 extends AbstractLinkedListForJava21 { - DefaultAbstractLinkedListForJava21() { - init(); - } - - private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - doReadObject(in); - } - - private void writeObject(final ObjectOutputStream out) throws IOException { - out.defaultWriteObject(); - doWriteObject(out); - } - } }