Commits

Sergei Vavinov  committed 5335340

Container#tee for explicit side effects

  • Participants
  • Parent commits e64d3fc

Comments (0)

Files changed (8)

File src/main/java/ru/yandex/bolts/collection/CollectionF.java

 
     E maxW(@FunctionParameter int comparator);
 
+    CollectionF<E> tee(Function1V<? super CollectionF<E>> f);
 } //~

File src/main/java/ru/yandex/bolts/collection/MapF.java

 import ru.yandex.bolts.function.Function;
 import ru.yandex.bolts.function.Function0;
 import ru.yandex.bolts.function.Function1B;
+import ru.yandex.bolts.function.Function1V;
 import ru.yandex.bolts.function.Function2;
 import ru.yandex.bolts.function.Function2B;
 import ru.yandex.bolts.function.Function2V;
     MapF<K, V> unmodifiable();
 
     <L, W> MapF<L, W> uncheckedCast();
+
+    MapF<K, V> tee(Function1V<? super MapF<K, V>> f);
 } //~

File src/main/java/ru/yandex/bolts/collection/impl/AbstractCollectionF.java

         throw new RuntimeException("weaving must be enabled");
     }
 
+    @Override
+    public CollectionF<E> tee(Function1V<? super CollectionF<E>> f) {
+        f.apply(this);
+        return this;
+    }
 } //~

File src/main/java/ru/yandex/bolts/collection/impl/AbstractCollectionFTest.java

 import static ru.yandex.bolts.collection.CollectionsF.set;
 
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicReference;
 
 import junit.framework.TestCase;
 
         assertEquals("d", coll.max());
         assertEquals("a", coll.max(Comparator.naturalComparator().<String, String>uncheckedCast().invert()));
     }
+
+    public void testTee() {
+        CollectionF<Integer> m1 = Cf.list(1, 2);
+        final AtomicReference<Integer> counter = new AtomicReference<Integer>(0);
+        CollectionF<Integer> m2 = m1.tee(new Function1V<Collection<Integer>>() {
+            public void apply(Collection<Integer> l) {
+                counter.set(l.size());
+            }
+        });
+        assertEquals(m1, m2);
+        assertEquals(Integer.valueOf(2), counter.get());
+    }
+
 } //~

File src/main/java/ru/yandex/bolts/collection/impl/AbstractListF.java

 import java.util.RandomAccess;
 
 import ru.yandex.bolts.collection.Cf;
+import ru.yandex.bolts.collection.CollectionF;
 import ru.yandex.bolts.collection.CollectionsF;
 import ru.yandex.bolts.collection.IteratorF;
 import ru.yandex.bolts.collection.ListF;
 import ru.yandex.bolts.collection.Tuple2List;
 import ru.yandex.bolts.function.Function;
 import ru.yandex.bolts.function.Function1B;
+import ru.yandex.bolts.function.Function1V;
 
 /**
  * Implementation of {@link ListF} algorithms.
         throw new IndexOutOfBoundsException();
     }
 
+    @Override
+    public ListF<E> tee(Function1V<? super CollectionF<E>> f) {
+        return (ListF<E>) super.tee(f);
+    }
 } //~

File src/main/java/ru/yandex/bolts/collection/impl/AbstractMapF.java

 import ru.yandex.bolts.function.Function;
 import ru.yandex.bolts.function.Function0;
 import ru.yandex.bolts.function.Function1B;
+import ru.yandex.bolts.function.Function1V;
 import ru.yandex.bolts.function.Function2;
 import ru.yandex.bolts.function.Function2B;
 import ru.yandex.bolts.function.Function2V;
         }
     }
 
+    @Override
+    public MapF<K, V> tee(Function1V<? super MapF<K, V>> f) {
+        f.apply(this);
+        return this;
+    }
+    
 } //~

File src/main/java/ru/yandex/bolts/collection/impl/AbstractMapFTest.java

 import static ru.yandex.bolts.collection.CollectionsF.list;
 import static ru.yandex.bolts.collection.CollectionsF.set;
 
+import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicReference;
 
 import junit.framework.TestCase;
 
 import ru.yandex.bolts.collection.Option;
 import ru.yandex.bolts.function.Function;
 import ru.yandex.bolts.function.Function1BTest;
+import ru.yandex.bolts.function.Function1V;
 
 /**
  * @author Stepan Koltsov
         MapF<Integer, String> m = map123().plus1(11, "11");
         assertEquals(m, Cf.list(1, 2, 3, 11).toMapMappingToValue(Function.toStringF()));
     }
+
+    public void testTee() {
+        MapF<Integer, String> m1 = Cf.map(1, "qwer", 2, "asdf");
+        final AtomicReference<Integer> counter = new AtomicReference<Integer>(0);
+        MapF<Integer, String> m2 = m1.tee(new Function1V<Map<Integer, String>>() {
+            public void apply(Map<Integer, String> map) {
+                counter.set(map.size());
+            }
+        });
+        assertEquals(m1, m2);
+        assertEquals(Integer.valueOf(2), counter.get());
+    }
+    
 } //~

File src/main/java/ru/yandex/bolts/collection/impl/AbstractSetF.java

 import ru.yandex.bolts.collection.SetF;
 import ru.yandex.bolts.collection.Tuple2;
 import ru.yandex.bolts.function.Function1B;
+import ru.yandex.bolts.function.Function1V;
 
 /**
  * @author Stepan Koltsov
         return result;
     }
 
+    @Override
+    public SetF<E> tee(Function1V<? super CollectionF<E>> f) {
+        return (SetF<E>) super.tee(f);
+    }
+
 } //~