## collections/src/main/java/org/apache/commons/collections4/collection/TransformedCollection.java:50-102
50:      * If there are any elements already in the collection being decorated, they
51:      * will be transformed by this method.
52:      * Contrast this with {@link #transformingCollection(Collection, Transformer)}.
53:      * </p>
54:      *
55:      * @param <E> The type of the elements in the collection.
56:      * @param collection  The collection to decorate, must not be null.
57:      * @param transformer  The transformer to use for conversion, must not be null.
58:      * @return A new transformed Collection.
59:      * @throws NullPointerException if collection or transformer is null.
60:      * @since 4.0
61:      */
62:     public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection,
63:             final Transformer<? super E, ? extends E> transformer) {
64: 
65:         final TransformedCollection<E> decorated = new TransformedCollection<>(collection, transformer);
66:         // null collection & transformer are disallowed by the constructor call above
67:         if (!collection.isEmpty()) {
68:             @SuppressWarnings("unchecked") // collection is of type E
69:             final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics
70:             collection.clear();
71:             for (final E value : values) {
72:                 decorated.decorated().add(transformer.apply(value));
73:             }
74:         }
75:         return decorated;
76:     }
77: 
78:     /**
79:      * Creates a transforming collection.
80:      * <p>
81:      * If there are any elements already in the collection being decorated, they
82:      * are NOT transformed.
83:      * Contrast this with {@link #transformedCollection(Collection, Transformer)}.
84:      * </p>
85:      *
86:      * @param <E> The type of the elements in the collection.
87:      * @param coll  The collection to decorate, must not be null.
88:      * @param transformer  The transformer to use for conversion, must not be null.
89:      * @return A new transformed collection.
90:      * @throws NullPointerException if collection or transformer is null.
91:      * @since 4.0
92:      */
93:     public static <E> TransformedCollection<E> transformingCollection(final Collection<E> coll,
94:             final Transformer<? super E, ? extends E> transformer) {
95:         return new TransformedCollection<>(coll, transformer);
96:     }
97: 
98:     /** The transformer to use */
99:     protected final Transformer<? super E, ? extends E> transformer;
100: 
101:     /**
102:      * Constructs and wraps (not copies).

## collections/src/main/java/org/apache/commons/collections4/collection/SynchronizedCollection.java:68-100
68:     protected final Object lock;
69: 
70:     /**
71:      * Constructs and wraps (not copies).
72:      *
73:      * @param collection The collection to decorate, must not be null.
74:      * @throws NullPointerException if the collection is null.
75:      */
76:     protected SynchronizedCollection(final Collection<E> collection) {
77:         this.collection = Objects.requireNonNull(collection, "collection");
78:         this.lock = this;
79:     }
80: 
81:     /**
82:      * Constructs and wraps (not copies).
83:      *
84:      * @param collection The collection to decorate, must not be null.
85:      * @param lock       The lock object to use, must not be null.
86:      * @throws NullPointerException if the collection or lock is null.
87:      */
88:     protected SynchronizedCollection(final Collection<E> collection, final Object lock) {
89:         this.collection = Objects.requireNonNull(collection, "collection");
90:         this.lock = Objects.requireNonNull(lock, "lock");
91:     }
92: 
93:     @Override
94:     public boolean add(final E object) {
95:         synchronized (lock) {
96:             return decorated().add(object);
97:         }
98:     }
99: 
100:     @Override

## collections/src/main/java/org/apache/commons/collections4/collection/SynchronizedCollection.java:170-192
170: 
171:     /**
172:      * Iterators must be manually synchronized.
173:      *
174:      * <pre>
175:      * synchronized (coll) {
176:      *     Iterator it = coll.iterator();
177:      *     // do stuff with iterator
178:      * }
179:      * </pre>
180:      *
181:      * @return An iterator that must be manually synchronized on the collection.
182:      */
183:     @Override
184:     public Iterator<E> iterator() {
185:         return decorated().iterator();
186:     }
187: 
188:     @Override
189:     public boolean remove(final Object object) {
190:         synchronized (lock) {
191:             return decorated().remove(object);
192:         }

## collections/src/test/java/org/apache/commons/collections4/collection/PredicatedCollectionTest.java:84-119
84:     @Test
85:     void testDeserializeRejectsElementFailingPredicate() throws Exception {
86:         final PredicatedCollection<E> coll = (PredicatedCollection<E>) decorateCollection(new ArrayList<>(), NotNullPredicate.<E>notNullPredicate());
87:         // a crafted stream can carry an element that never passed add(); mimic it by
88:         // writing one straight into the decorated collection
89:         coll.decorated().add(null);
90:         assertThrows(InvalidObjectException.class, () -> serializeDeserialize(coll));
91:     }
92: 
93:     @Test
94:     @SuppressWarnings("unchecked")
95:     void testIllegalAdd() {
96:         final Collection<E> c = makeTestCollection();
97:         final Integer i = 3;
98:         assertThrows(IllegalArgumentException.class, () -> c.add((E) i), "Integer should fail string predicate.");
99:         assertFalse(c.contains(i), "Collection shouldn't contain illegal element");
100:     }
101: 
102:     @Test
103:     @SuppressWarnings("unchecked")
104:     void testIllegalAddAll() {
105:         final Collection<E> c = makeTestCollection();
106:         final List<E> elements = new ArrayList<>();
107:         elements.add((E) "one");
108:         elements.add((E) "two");
109:         elements.add((E) Integer.valueOf(3));
110:         elements.add((E) "four");
111:         assertThrows(IllegalArgumentException.class, () -> c.addAll(elements), "Integer should fail string predicate.");
112:         assertFalse(c.contains("one"), "Collection shouldn't contain illegal element");
113:         assertFalse(c.contains("two"), "Collection shouldn't contain illegal element");
114:         assertFalse(c.contains(3), "Collection shouldn't contain illegal element");
115:         assertFalse(c.contains("four"), "Collection shouldn't contain illegal element");
116:     }
117: 
118: //    void testCreate() throws Exception {
119: //        resetEmpty();

## collections/src/test/java/org/apache/commons/collections4/collection/TransformedCollectionTest.java:75-130
75:     }
76: 
77:     @Override
78:     public Collection<Object> makeConfirmedFullCollection() {
79:         return new ArrayList<>(Arrays.asList(getFullElements()));
80:     }
81: 
82:     @Override
83:     public Collection<Object> makeFullCollection() {
84:         final List<Object> list = new ArrayList<>(Arrays.asList(getFullElements()));
85:         return TransformedCollection.transformingCollection(list, NOOP_TRANSFORMER);
86:     }
87: 
88:     @Override
89:     public Collection<Object> makeObject() {
90:         return TransformedCollection.transformingCollection(new ArrayList<>(), NOOP_TRANSFORMER);
91:     }
92: 
93:     @Test
94:     void testTransformedCollection() {
95:         final Collection<Object> coll = TransformedCollection.transformingCollection(new ArrayList<>(), STRING_TO_INTEGER_TRANSFORMER);
96:         assertEquals(0, coll.size());
97:         final Object[] elements = getFullElements();
98:         for (int i = 0; i < elements.length; i++) {
99:             coll.add(elements[i]);
100:             assertEquals(i + 1, coll.size());
101:             assertTrue(coll.contains(Integer.valueOf((String) elements[i])));
102:             assertFalse(coll.contains(elements[i]));
103:         }
104: 
105:         assertTrue(coll.remove(Integer.valueOf((String) elements[0])));
106:     }
107: 
108:     @Test
109:     void testTransformedCollection_decorateTransform() {
110:         final Collection<Object> originalCollection = new ArrayList<>();
111:         final Object[] elements = getFullElements();
112:         Collections.addAll(originalCollection, elements);
113:         final Collection<Object> collection = TransformedCollection.transformedCollection(originalCollection, TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
114:         assertEquals(elements.length, collection.size());
115:         for (final Object element : elements) {
116:             assertTrue(collection.contains(Integer.valueOf((String) element)));
117:             assertFalse(collection.contains(element));
118:         }
119: 
120:         assertFalse(collection.remove(elements[0]));
121:         assertTrue(collection.remove(Integer.valueOf((String) elements[0])));
122:     }
123: 
124: //    void testCreate() throws Exception {
125: //        resetEmpty();
126: //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedCollection.emptyCollection.version4.obj");
127: //        resetFull();
128: //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedCollection.fullCollection.version4.obj");
129: //    }
130: 
