diff --git a/src/main/java/com/cinchapi/common/collect/lazy/LazyTransformMap.java b/src/main/java/com/cinchapi/common/collect/lazy/LazyTransformMap.java new file mode 100644 index 0000000..34db7f5 --- /dev/null +++ b/src/main/java/com/cinchapi/common/collect/lazy/LazyTransformMap.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2013-2019 Cinchapi Inc. + * + * Licensed 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 com.cinchapi.common.collect.lazy; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * + * + * @author jeff + */ +public class LazyTransformMap extends AbstractMap { + + private final Map from; + private final Function keyTransformer; + private final Function valueTransformer; + + private LazyTransformMap(Map from, Function keyTransformer, + Function valueTransformer) { + this.from = from; + this.keyTransformer = keyTransformer; + this.valueTransformer = valueTransformer; + } + + @Override + public Set> entrySet() { + return LazyTransformSet.of(from.entrySet(), + entry -> new SimpleImmutableEntry<>( + keyTransformer.apply(entry.getKey()), + valueTransformer.apply(entry.getValue()))); + } + +} diff --git a/src/main/java/com/cinchapi/common/collect/lazy/LazyTransformSet.java b/src/main/java/com/cinchapi/common/collect/lazy/LazyTransformSet.java index 0715737..184972b 100644 --- a/src/main/java/com/cinchapi/common/collect/lazy/LazyTransformSet.java +++ b/src/main/java/com/cinchapi/common/collect/lazy/LazyTransformSet.java @@ -16,6 +16,7 @@ package com.cinchapi.common.collect.lazy; import java.util.AbstractSet; +import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.Optional; @@ -50,8 +51,7 @@ * This {@link Set} allows just-in-time transformation and should be used to * transform elements on the fly when said transformation is expensive. This is * especially useful in {@link #stream() stream} operations that feature - * intermediate - * operations like {@link Stream#skip(long) skipping}. + * intermediate operations like {@link Stream#skip(long) skipping}. *

* * @author Jeff Nelson @@ -66,7 +66,7 @@ public class LazyTransformSet extends AbstractSet { * @param transformer * @return the {@link LazyTransformSet} */ - public static LazyTransformSet of(Set from, + public static LazyTransformSet of(Collection from, Function transformer) { return new LazyTransformSet<>(from, transformer); } @@ -74,7 +74,7 @@ public static LazyTransformSet of(Set from, /** * The original {@link Set} whose items will be transformed. */ - private final Set from; + private final Collection from; /** * The transforming function. @@ -87,7 +87,7 @@ public static LazyTransformSet of(Set from, * @param from * @param transformer */ - private LazyTransformSet(Set from, Function transformer) { + private LazyTransformSet(Collection from, Function transformer) { this.from = from; this.transformer = transformer; } diff --git a/src/test/java/com/cinchapi/common/collect/lazy/LazyTransformSetTest.java b/src/test/java/com/cinchapi/common/collect/lazy/LazyTransformSetTest.java index 609103a..abaf214 100644 --- a/src/test/java/com/cinchapi/common/collect/lazy/LazyTransformSetTest.java +++ b/src/test/java/com/cinchapi/common/collect/lazy/LazyTransformSetTest.java @@ -15,12 +15,14 @@ */ package com.cinchapi.common.collect.lazy; +import java.util.Collection; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Assert; import org.junit.Test; +import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableSet; /** @@ -42,6 +44,19 @@ public void testLazyStreamSkip() { Assert.assertEquals(4, count.get()); } + @Test + public void testLazyStreamSkipVsGuava() { + // Tests to prove that the benefits of LazyTransformSet (e.g. skip tracking) cannot be achieved + Set original = ImmutableSet.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + AtomicInteger count = new AtomicInteger(0); + Collection transformed = Collections2.transform(original, o -> { + count.incrementAndGet(); + return o.toString(); + }); + transformed.stream().skip(3).limit(4).forEach(System.out::println); + Assert.assertNotEquals(4, count.get()); + } + @Test public void testLazyStreamDoubleSkip() { Set original = ImmutableSet.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);