Last active
January 11, 2018 14:38
-
-
Save jarrodhroberson/66dd80d2e847ea07ecc8949ec01c53df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.svd; | |
import com.google.auto.value.AutoValue; | |
import com.google.common.base.Function; | |
import com.google.common.collect.ImmutableList; | |
import javax.annotation.Nonnull; | |
import java.util.Iterator; | |
import java.util.Random; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import static com.google.common.collect.Iterables.*; | |
public class StreamVsDecoratorAutoValued | |
{ | |
@AutoValue | |
public static abstract class IndexedDouble | |
{ | |
static IndexedDouble create(@Nonnull final Integer index, @Nonnull final Double value) { | |
return new AutoValue_StreamVsDecorator_IndexedDouble(index,value); | |
} | |
abstract Integer index(); | |
abstract Double value(); | |
} | |
public static void main(String[] args) | |
{ | |
final Double ZERO = 0.0d; | |
final Double ONE = 1.0d; | |
final Iterable<Double> probes = ImmutableList.copyOf(new Random().doubles(1000, 0.0d, 1.0d).iterator()); | |
transform(limit(filter(probes, probe -> ZERO.compareTo(probe) < 0 || ONE.compareTo(probe) > 0), 10), new Function<Double, IndexedDouble>() | |
{ | |
final AtomicInteger index = new AtomicInteger(0); | |
@Nonnull | |
@Override | |
public IndexedDouble apply(@Nonnull final Double probe) | |
{ | |
return IndexedDouble.create(index.incrementAndGet(), probe); | |
} | |
}).forEach(idp -> System.out.printf("Probe #%d: %f\n", idp.index(), idp.value() * 100.0d)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.base.Function; | |
import com.google.common.collect.ImmutableList; | |
import javafx.util.Pair; | |
import javax.annotation.Nonnull; | |
import java.util.Random; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import static com.google.common.collect.Iterables.*; | |
public class StreamVsDecorator | |
{ | |
public static void main(String[] args) | |
{ | |
final Double ZERO = 0.0d; | |
final Double ONE = 1.0d; | |
final Iterable<Double> probes = ImmutableList.copyOf(new Random().doubles(1000, 0.0d, 1.0d).iterator()); | |
transform(limit(filter(probes, probe -> ZERO.compareTo(probe) < 0 || ONE.compareTo(probe) > 0), 10), new Function<Double, Pair<Integer, Double>>() | |
{ | |
final AtomicInteger index = new AtomicInteger(0); | |
@Nonnull | |
@Override | |
public Pair<Integer, Double> apply(@Nonnull final Double probe) | |
{ | |
return new Pair<>(index.incrementAndGet(), probe); | |
} | |
}).forEach(idp -> System.out.printf("Probe #%d: %f\n", idp.getKey(), idp.getValue() * 100.0d)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment