Difference between revisions of "Collector Rosetta Stone"

From CSE231 Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
You will use {{AccumulatorCombinerReducerLink}} for our MapReduce assignments which is almost a one-to-one match with {{CollectorLink}} but de-ultra-uber-hyper-mega-super-lambdafied.   
 
You will use {{AccumulatorCombinerReducerLink}} for our MapReduce assignments which is almost a one-to-one match with {{CollectorLink}} but de-ultra-uber-hyper-mega-super-lambdafied.   
  
 +
= One To One =
 
{| class="wikitable" style="margin:auto"
 
{| class="wikitable" style="margin:auto"
 
|+ Caption text
 
|+ Caption text
 
|-
 
|-
! CSE 231s: AccumulatorCombinerReducer<V, A, R> !! Header text !! Header text
+
! CSE 231s: AccumulatorCombinerReducer<V, A, R> !! !! Java Streams: Collector<T, A, R>
 
|-
 
|-
 
| <syntaxhighlight lang="java">
 
| <syntaxhighlight lang="java">
Line 19: Line 20:
 
}
 
}
 
</syntaxhighlight> || <syntaxhighlight lang="java">
 
</syntaxhighlight> || <syntaxhighlight lang="java">
public interface Collector<T, A, R> {
 
    /**
 
    * A function that creates and returns a new mutable result container.
 
    *
 
    * @return a function which returns a new, mutable result container
 
    */
 
    Supplier<A> supplier();
 
  
    /**
+
==methods==
    * A function that folds a value into a mutable result container.
+
=== createMutableContainer a.k.a. supplier get===
    *
+
We use createMutableContainer() to create a new mutable container. For classic map reduce this would be a [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List<V>].
    * @return a function which folds a value into a mutable result container
+
 
    */
+
rosetta stone: <code>container = collector.supplier().get()</code> <math>\leftrightarrow</math> <code>container = reducer.createMutableContainer()</code>
    BiConsumer<A, T> accumulator();
+
 
 +
=== accumulate a.k.a. accumulator accept===
 +
We use accumulate(container,item) to accumulate a value.  For classic map reduce this would add an item to a list.
 +
 
 +
rosetta stone: <code>collector.accumulator().accept(container,item);</code> <math>\leftrightarrow</math> <code>reducer.accumulate(container,item)</code>
 +
 
 +
=== combine a.k.a. combiner apply===
 +
We use combine(containerA,containerB) to combine two accumulators.  You may combine containerB into containerA or containerA into containerB.  Just return whichever is the combined result.
  
    /**
+
rosetta stone: <code>collector.combiner().apply(containerA,containerB)</code> <math>\leftrightarrow</math> <code>reducer.combine(containerA,containerB)</code>
    * A function that accepts two partial results and merges them. The
 
    * combiner function may fold state from one argument into the other and
 
    * return that, or may return a new result container.
 
    *
 
    * @return a function which combines two partial results into a combined
 
    * result
 
    */
 
    BinaryOperator<A> combiner();
 
  
    /**
+
=== reduce a.k.a. finisher apply===
    * Perform the final transformation from the intermediate accumulation type
+
We use reduce(container) to reduce an accumulator.
    * {@code A} to the final result type {@code R}.
 
    *
 
    * <p>If the characteristic {@code IDENTITY_FINISH} is
 
    * set, this function may be presumed to be an identity transform with an
 
    * unchecked cast from {@code A} to {@code R}.
 
    *
 
    * @return a function which transforms the intermediate result to the final
 
    * result
 
    */
 
    Function<A, R> finisher();
 
  
    /**
+
rosetta stone: <code>collector.finisher().apply(container)</code> <math>\leftrightarrow</math> <code>r = reducer.reduce(container)</code>
    * Returns a {@code Set} of {@code Collector.Characteristics} indicating
 
    * the characteristics of this Collector. This set should be immutable.
 
    *
 
    * @return an immutable set of collector characteristics
 
    */
 
    Set<Characteristics> characteristics();
 
}
 
</syntaxhighlight>
 
|}
 
  
== CSE 231s: AccumulatorCombinerReducer<V, A, R> ==
+
<===>
<syntaxhighlight lang="java">
 
public interface AccumulatorCombinerReducer<V, A, R> {
 
A createMutableContainer();
 
  
void accumulate(A container, V item);
+
<===>
  
void combine(A containerA, A containerB);
+
<===>
  
R reduce(A container);
+
<===>
}
 
</syntaxhighlight>
 
  
== Java Streams: Collector<T, A, R> ==
+
</syntaxhighlight> || <syntaxhighlight lang="java">
<syntaxhighlight lang="java">
 
 
public interface Collector<T, A, R> {
 
public interface Collector<T, A, R> {
    /**
 
    * A function that creates and returns a new mutable result container.
 
    *
 
    * @return a function which returns a new, mutable result container
 
    */
 
 
     Supplier<A> supplier();
 
     Supplier<A> supplier();
  
    /**
 
    * A function that folds a value into a mutable result container.
 
    *
 
    * @return a function which folds a value into a mutable result container
 
    */
 
 
     BiConsumer<A, T> accumulator();
 
     BiConsumer<A, T> accumulator();
  
    /**
 
    * A function that accepts two partial results and merges them.  The
 
    * combiner function may fold state from one argument into the other and
 
    * return that, or may return a new result container.
 
    *
 
    * @return a function which combines two partial results into a combined
 
    * result
 
    */
 
 
     BinaryOperator<A> combiner();
 
     BinaryOperator<A> combiner();
  
    /**
 
    * Perform the final transformation from the intermediate accumulation type
 
    * {@code A} to the final result type {@code R}.
 
    *
 
    * <p>If the characteristic {@code IDENTITY_FINISH} is
 
    * set, this function may be presumed to be an identity transform with an
 
    * unchecked cast from {@code A} to {@code R}.
 
    *
 
    * @return a function which transforms the intermediate result to the final
 
    * result
 
    */
 
 
     Function<A, R> finisher();
 
     Function<A, R> finisher();
 
    /**
 
    * Returns a {@code Set} of {@code Collector.Characteristics} indicating
 
    * the characteristics of this Collector.  This set should be immutable.
 
    *
 
    * @return an immutable set of collector characteristics
 
    */
 
    Set<Characteristics> characteristics();
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
|}
== Java Streams Collector ==
 
<nowiki>public interface Collector<T, A, R> {
 
 
 
// invoke supplier().get() to create a new mutable container
 
Supplier<A> supplier();
 
 
 
// invoke accumulator().accept(container, item) to add item to a container
 
BiConsumer<A, T> accumulator();
 
 
 
// invoke combiner().apply(containerA, containerB) to combine one container into the other
 
BinaryOperator<A> combiner();
 
 
 
// invoke finisher().apply(container) to reduce a container to its final form
 
Function<A, R> finisher();
 
}</nowiki>
 
  
 
[https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html interface Collector<T,A,R>]
 
[https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html interface Collector<T,A,R>]
Line 153: Line 69:
 
: [https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html interface Function<T,R>]
 
: [https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html interface Function<T,R>]
  
== Rosetta Stone ==
+
== Converting Back And Forth ==
 
+
<syntaxhighlight lang="java">
<nowiki> public static <V, A, R> Collector<V, A, R> toCollector(Reducer<V, A, R> reducer) {
+
public class StreamUtils {
 +
public static <V, A, R> Collector<V, A, R> toCollector(
 +
AccumulatorCombinerReducer<V, A, R> accumulatorCombinerReducer) {
 
return new Collector<V, A, R>() {
 
return new Collector<V, A, R>() {
 
@Override
 
@Override
 
public Supplier<A> supplier() {
 
public Supplier<A> supplier() {
return () -> reducer.createMutableContainer();
+
return () -> accumulatorCombinerReducer.createMutableContainer();
 
}
 
}
  
 
@Override
 
@Override
 
public BiConsumer<A, V> accumulator() {
 
public BiConsumer<A, V> accumulator() {
return (container, item) -> reducer.accumulate(container, item);
+
return (container, item) -> accumulatorCombinerReducer.accumulate(container, item);
 
}
 
}
  
 
@Override
 
@Override
 
public BinaryOperator<A> combiner() {
 
public BinaryOperator<A> combiner() {
return (a, b) -> reducer.combine(a, b);
+
return (a, b) -> {
 +
accumulatorCombinerReducer.combine(a, b);
 +
return a;
 +
};
 
}
 
}
  
 
@Override
 
@Override
 
public Function<A, R> finisher() {
 
public Function<A, R> finisher() {
return (container) -> reducer.reduce(container);
+
return (container) -> accumulatorCombinerReducer.reduce(container);
 
}
 
}
  
 
@Override
 
@Override
 
public Set<Characteristics> characteristics() {
 
public Set<Characteristics> characteristics() {
return reducer.collectorCharacteristics();
+
return accumulatorCombinerReducer.collectorCharacteristics();
 
}
 
}
 
};
 
};
 
}
 
}
  
public static <V, A, R> Reducer<V, A, R> toReducer(Collector<V, A, R> collector) {
+
public static <V, A, R> AccumulatorCombinerReducer<V, A, R> toAccumulatorCombinerReducer(
return new Reducer<V, A, R>() {
+
Collector<V, A, R> collector) {
 +
return new AccumulatorCombinerReducer<V, A, R>() {
 
@Override
 
@Override
 
public A createMutableContainer() {
 
public A createMutableContainer() {
Line 197: Line 119:
  
 
@Override
 
@Override
public A combine(A containerA, A containerB) {
+
public void combine(A containerA, A containerB) {
return collector.combiner().apply(containerA, containerB);
+
A result = collector.combiner().apply(containerA, containerB);
 +
if (result != containerA) {
 +
throw new RuntimeException("collector must combine b into a and return a.");
 +
}
 
}
 
}
  
Line 212: Line 137:
 
};
 
};
 
}
 
}
</nowiki>
 
  
==methods==
+
public static AccumulatorCombinerReducer<Integer, ?, Integer> summingIntAccumulatorCombinerReducer() {
=== createMutableContainer a.k.a. supplier get===
+
return toAccumulatorCombinerReducer(Collectors.summingInt(Integer::intValue));
We use createMutableContainer() to create a new mutable container.  For classic map reduce this would be a [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List<V>].
+
}
 
+
}
rosetta stone: <code>container = collector.supplier().get()</code> <math>\leftrightarrow</math> <code>container = reducer.createMutableContainer()</code>
+
</syntaxhighlight>
 
 
=== accumulate a.k.a. accumulator accept===
 
We use accumulate(container,item) to accumulate a value.  For classic map reduce this would add an item to a list.
 
 
 
rosetta stone: <code>collector.accumulator().accept(container,item);</code> <math>\leftrightarrow</math> <code>reducer.accumulate(container,item)</code>
 
 
 
=== combine a.k.a. combiner apply===
 
We use combine(containerA,containerB) to combine two accumulators.  You may combine containerB into containerA or containerA into containerB.  Just return whichever is the combined result.
 
 
 
rosetta stone: <code>collector.combiner().apply(containerA,containerB)</code> <math>\leftrightarrow</math> <code>reducer.combine(containerA,containerB)</code>
 
 
 
=== reduce a.k.a. finisher apply===
 
We use reduce(container) to reduce an accumulator.
 
 
 
rosetta stone: <code>collector.finisher().apply(container)</code> <math>\leftrightarrow</math> <code>r = reducer.reduce(container)</code>
 

Revision as of 16:02, 23 February 2023

The interface Collector<T,A,R> serves the standard Java streams framework for MapReduce-like tasks with added in-memory processing capability a la Apache Spark.

You will use interface AccumulatorCombinerReducer<V,A,R> for our MapReduce assignments which is almost a one-to-one match with interface Collector<T,A,R> but de-ultra-uber-hyper-mega-super-lambdafied.

One To One

Caption text
CSE 231s: AccumulatorCombinerReducer<V, A, R> Java Streams: Collector<T, A, R>
public interface AccumulatorCombinerReducer<V, A, R> {
	A createMutableContainer();

	void accumulate(A container, V item);

	void combine(A containerA, A containerB);

	R reduce(A container);
}
==methods==
=== createMutableContainer a.k.a. supplier get===
We use createMutableContainer() to create a new mutable container.  For classic map reduce this would be a [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List<V>].

rosetta stone: <code>container = collector.supplier().get()</code> <math>\leftrightarrow</math> <code>container = reducer.createMutableContainer()</code>

=== accumulate a.k.a. accumulator accept===
We use accumulate(container,item) to accumulate a value.  For classic map reduce this would add an item to a list.

rosetta stone: <code>collector.accumulator().accept(container,item);</code> <math>\leftrightarrow</math> <code>reducer.accumulate(container,item)</code>

=== combine a.k.a. combiner apply===
We use combine(containerA,containerB) to combine two accumulators.  You may combine containerB into containerA or containerA into containerB.  Just return whichever is the combined result. 

rosetta stone: <code>collector.combiner().apply(containerA,containerB)</code> <math>\leftrightarrow</math> <code>reducer.combine(containerA,containerB)</code>

=== reduce a.k.a. finisher apply===
We use reduce(container) to reduce an accumulator.

rosetta stone: <code>collector.finisher().apply(container)</code> <math>\leftrightarrow</math> <code>r = reducer.reduce(container)</code>

<===>

<===>

<===>

<===>
public interface Collector<T, A, R> {
    Supplier<A> supplier();

    BiConsumer<A, T> accumulator();

    BinaryOperator<A> combiner();

    Function<A, R> finisher();
}

interface Collector<T,A,R>

interface Supplier<T>
interface BiConsumer<T,U>
interface BinaryOperator<T>
interface Function<T,R>

Converting Back And Forth

public class StreamUtils {
	public static <V, A, R> Collector<V, A, R> toCollector(
			AccumulatorCombinerReducer<V, A, R> accumulatorCombinerReducer) {
		return new Collector<V, A, R>() {
			@Override
			public Supplier<A> supplier() {
				return () -> accumulatorCombinerReducer.createMutableContainer();
			}

			@Override
			public BiConsumer<A, V> accumulator() {
				return (container, item) -> accumulatorCombinerReducer.accumulate(container, item);
			}

			@Override
			public BinaryOperator<A> combiner() {
				return (a, b) -> {
					accumulatorCombinerReducer.combine(a, b);
					return a;
				};
			}

			@Override
			public Function<A, R> finisher() {
				return (container) -> accumulatorCombinerReducer.reduce(container);
			}

			@Override
			public Set<Characteristics> characteristics() {
				return accumulatorCombinerReducer.collectorCharacteristics();
			}
		};
	}

	public static <V, A, R> AccumulatorCombinerReducer<V, A, R> toAccumulatorCombinerReducer(
			Collector<V, A, R> collector) {
		return new AccumulatorCombinerReducer<V, A, R>() {
			@Override
			public A createMutableContainer() {
				return collector.supplier().get();
			}

			@Override
			public void accumulate(A container, V item) {
				collector.accumulator().accept(container, item);
			}

			@Override
			public void combine(A containerA, A containerB) {
				A result = collector.combiner().apply(containerA, containerB);
				if (result != containerA) {
					throw new RuntimeException("collector must combine b into a and return a.");
				}
			}

			@Override
			public R reduce(A container) {
				return collector.finisher().apply(container);
			}

			@Override
			public Set<Characteristics> collectorCharacteristics() {
				return collector.characteristics();
			}
		};
	}

	public static AccumulatorCombinerReducer<Integer, ?, Integer> summingIntAccumulatorCombinerReducer() {
		return toAccumulatorCombinerReducer(Collectors.summingInt(Integer::intValue));
	}
}