/** * Factory method to create an unmodifiable bounded collection. * <p> * This method is capable of drilling down through up to 1000 other decorators * to find a suitable BoundedCollection. * * @param <E> the type of the elements in the collection * @param coll the <code>BoundedCollection</code> to decorate, must not be null * @return a new unmodifiable bounded collection * @throws NullPointerException if coll is null * @throws IllegalArgumentException if coll is not a {@code BoundedCollection} * @since 4.0 */ @SuppressWarnings("unchecked") public static <E> BoundedCollection<E> unmodifiableBoundedCollection(Collection<? extends E> coll) { if (coll == null) { throw new NullPointerException("Collection must not be null."); } // handle decorators for (int i = 0; i < 1000; i++) { // counter to prevent infinite looping if (coll instanceof BoundedCollection) { break; // normal loop exit } if (coll instanceof AbstractCollectionDecorator) { coll = ((AbstractCollectionDecorator<E>) coll).decorated(); } else if (coll instanceof SynchronizedCollection) { coll = ((SynchronizedCollection<E>) coll).decorated(); } } if (coll instanceof BoundedCollection == false) { throw new IllegalArgumentException("Collection is not a bounded collection."); } return new UnmodifiableBoundedCollection<E>((BoundedCollection<E>) coll); }
/** * increments a value in the map. * * @param key * the key to use. * @param amount * the amount to use. */ private static void addToPerfDataSumMap(final String key, final long amount) { synchronized (PERF_DATA_SUM_MAP) { if (!PERF_DATA_SUM_MAP.containsKey(key)) { PERF_DATA_SUM_MAP.put(key, new CircularFifoQueue<>(MAX_PERF_DATA_HISTORY)); } final BoundedCollection<Long> values = PERF_DATA_SUM_MAP.get(key); values.add(amount); } }
/** * increments a value in the map. * * @param key * the key to use. * @return the sum. */ private static long getSum(final String key) { synchronized (PERF_DATA_SUM_MAP) { final BoundedCollection<Long> values = PERF_DATA_SUM_MAP.get(key); long sum = 0; for (final long value : values) { sum += value; } return sum; } }
public void testDecorateFactory() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testDecorateFactory"); final BoundedCollection<E> coll = makeFullCollection(); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),4807,coll); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),4809,null,4808,org.apache.commons.collections4.collection.UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll)); try { org.apache.commons.collections4.collection.UnmodifiableBoundedCollection.unmodifiableBoundedCollection(null); } catch (final IllegalArgumentException ex) { } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testDecorateFactory_add1552() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testDecorateFactory_add1552"); final BoundedCollection<E> coll = makeFullCollection(); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),4807,coll); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),4809,null,4808,org.apache.commons.collections4.collection.UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll)); try { org.apache.commons.collections4.collection.UnmodifiableBoundedCollection.unmodifiableBoundedCollection(null); org.apache.commons.collections4.collection.UnmodifiableBoundedCollection.unmodifiableBoundedCollection(null); } catch (final IllegalArgumentException ex) { } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Override protected BoundedCollection<E> decorated() { return (BoundedCollection<E>) super.decorated(); }
@Override public Collection<E> makeObject() { final BoundedCollection<E> coll = org.apache.commons.collections4.list.FixedSizeList.<E>fixedSizeList(new ArrayList<E>()); return org.apache.commons.collections4.collection.UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll); }
@Override public BoundedCollection<E> makeFullCollection() { final E[] allElements = getFullElements(); final BoundedCollection<E> coll = org.apache.commons.collections4.list.FixedSizeList.<E>fixedSizeList(new ArrayList<E>(java.util.Arrays.asList(allElements))); return org.apache.commons.collections4.collection.UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll); }
/** * Factory method to create an unmodifiable bounded collection. * * @param <E> the type of the elements in the collection * @param coll the <code>BoundedCollection</code> to decorate, must not be null * @return a new unmodifiable bounded collection * @throws NullPointerException if {@code coll} is {@code null} * @since 4.0 */ public static <E> BoundedCollection<E> unmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) { if (coll instanceof Unmodifiable) { @SuppressWarnings("unchecked") // safe to upcast final BoundedCollection<E> tmpColl = (BoundedCollection<E>) coll; return tmpColl; } return new UnmodifiableBoundedCollection<E>(coll); }
/** * Constructor that wraps (not copies). * * @param coll the collection to decorate, must not be null * @throws NullPointerException if coll is null */ @SuppressWarnings("unchecked") // safe to upcast private UnmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) { super((BoundedCollection<E>) coll); }