Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LazyTransformMap #13

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<FK, FV, TK, TV> extends AbstractMap<TK, TV> {

private final Map<FK, FV> from;
private final Function<FK, TK> keyTransformer;
private final Function<FV, TV> valueTransformer;

private LazyTransformMap(Map<FK, FV> from, Function<FK, TK> keyTransformer,
Function<FV, TV> valueTransformer) {
this.from = from;
this.keyTransformer = keyTransformer;
this.valueTransformer = valueTransformer;
}

@Override
public Set<Entry<TK, TV>> entrySet() {
return LazyTransformSet.of(from.entrySet(),
entry -> new SimpleImmutableEntry<>(
keyTransformer.apply(entry.getKey()),
valueTransformer.apply(entry.getValue())));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}.
* </p>
*
* @author Jeff Nelson
Expand All @@ -66,15 +66,15 @@ public class LazyTransformSet<F, T> extends AbstractSet<T> {
* @param transformer
* @return the {@link LazyTransformSet}
*/
public static <F, T> LazyTransformSet<F, T> of(Set<F> from,
public static <F, T> LazyTransformSet<F, T> of(Collection<F> from,
Function<F, T> transformer) {
return new LazyTransformSet<>(from, transformer);
}

/**
* The original {@link Set} whose items will be transformed.
*/
private final Set<F> from;
private final Collection<F> from;

/**
* The transforming function.
Expand All @@ -87,7 +87,7 @@ public static <F, T> LazyTransformSet<F, T> of(Set<F> from,
* @param from
* @param transformer
*/
private LazyTransformSet(Set<F> from, Function<F, T> transformer) {
private LazyTransformSet(Collection<F> from, Function<F, T> transformer) {
this.from = from;
this.transformer = transformer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<Integer> original = ImmutableSet.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
AtomicInteger count = new AtomicInteger(0);
Collection<String> 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<Integer> original = ImmutableSet.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Expand Down