Skip to content

Commit

Permalink
[#212] Add Collectable.bitAnd(), bitOr() including mapping overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Mar 13, 2016
1 parent 1ab37a9 commit 09bd058
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 19 deletions.
132 changes: 129 additions & 3 deletions src/main/java/org/jooq/lambda/Agg.java
Expand Up @@ -24,8 +24,11 @@
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;

import org.jooq.lambda.tuple.Tuple2;
import static org.jooq.lambda.tuple.Tuple.tuple;

/**
* A set of additional {@link Collector} implementations.
Expand Down Expand Up @@ -81,8 +84,7 @@ public class Agg {
* type of {@link Number}.
*/
public static <T, U> Collector<T, ?, Optional<U>> sum(Function<? super T, ? extends U> function) {
return Collector.of(
() -> (Sum<U>[]) new Sum[1],
return Collector.of(() -> (Sum<U>[]) new Sum[1],
(s, v) -> {
if (s[0] == null)
s[0] = Sum.create(function.apply(v));
Expand All @@ -93,7 +95,7 @@ public class Agg {
s1[0].add(s2[0]);
return s1;
},
s -> s[0] == null ? Optional.empty() : Optional.of(s[0].sum())
s -> s[0] == null ? Optional.empty() : Optional.of(s[0].result())
);
}

Expand Down Expand Up @@ -274,6 +276,130 @@ public class Agg {
return allMatch(predicate.negate());
}

/**
* Get a {@link Collector} that calculates <code>BIT_AND()</code> for any
* type of {@link Number}.
*/
public static <T> Collector<T, ?, Optional<T>> bitAnd() {
return bitAnd(t -> t);
}

/**
* Get a {@link Collector} that calculates the <code>BIT_AND()</code> for any
* type of {@link Number}.
*/
public static <T, U> Collector<T, ?, Optional<U>> bitAnd(Function<? super T, ? extends U> function) {
return Collector.of(() -> (Sum<U>[]) new Sum[1],
(s, v) -> {
if (s[0] == null)
s[0] = Sum.create(function.apply(v));
else
s[0].and(function.apply(v));
},
(s1, s2) -> {
s1[0].and(s2[0]);
return s1;
},
s -> s[0] == null ? Optional.empty() : Optional.of(s[0].result())
);
}

/**
* Get a {@link Collector} that calculates the <code>BIT_AND()</code> for any
* type of {@link Number}.
*/
public static <T, U> Collector<T, ?, Integer> bitAndInt(ToIntFunction<? super T> function) {
return Collector.of(() -> new int[] { Integer.MAX_VALUE },
(s, v) -> {
s[0] = s[0] & function.applyAsInt(v);
},
(s1, s2) -> {
s1[0] = s1[0] & s2[0];
return s1;
},
s -> s[0]
);
}

/**
* Get a {@link Collector} that calculates the <code>BIT_AND()</code> for any
* type of {@link Number}.
*/
public static <T, U> Collector<T, ?, Long> bitAndLong(ToLongFunction<? super T> function) {
return Collector.of(() -> new long[] { Long.MAX_VALUE },
(s, v) -> {
s[0] = s[0] & function.applyAsLong(v);
},
(s1, s2) -> {
s1[0] = s1[0] & s2[0];
return s1;
},
s -> s[0]
);
}

/**
* Get a {@link Collector} that calculates <code>BIT_OR()</code> for any
* type of {@link Number}.
*/
public static <T> Collector<T, ?, Optional<T>> bitOr() {
return bitOr(t -> t);
}

/**
* Get a {@link Collector} that calculates the <code>BIT_OR()</code> for any
* type of {@link Number}.
*/
public static <T, U> Collector<T, ?, Optional<U>> bitOr(Function<? super T, ? extends U> function) {
return Collector.of(() -> (Sum<U>[]) new Sum[1],
(s, v) -> {
if (s[0] == null)
s[0] = Sum.create(function.apply(v));
else
s[0].or(function.apply(v));
},
(s1, s2) -> {
s1[0].or(s2[0]);
return s1;
},
s -> s[0] == null ? Optional.empty() : Optional.of(s[0].result())
);
}

/**
* Get a {@link Collector} that calculates the <code>BIT_OR()</code> for any
* type of {@link Number}.
*/
public static <T, U> Collector<T, ?, Integer> bitOrInt(ToIntFunction<? super T> function) {
return Collector.of(() -> new int[1],
(s, v) -> {
s[0] = s[0] | function.applyAsInt(v);
},
(s1, s2) -> {
s1[0] = s1[0] | s2[0];
return s1;
},
s -> s[0]
);
}

/**
* Get a {@link Collector} that calculates the <code>BIT_OR()</code> for any
* type of {@link Number}.
*/
public static <T, U> Collector<T, ?, Long> bitOrLong(ToLongFunction<? super T> function) {
return Collector.of(() -> new long[1],
(s, v) -> {
s[0] = s[0] | function.applyAsLong(v);
},
(s1, s2) -> {
s1[0] = s1[0] | s2[0];
return s1;
},
s -> s[0]
);
}

/**
* Get a {@link Collector} that calculates the <code>MODE()</code> function.
*/
Expand Down
46 changes: 45 additions & 1 deletion src/main/java/org/jooq/lambda/Collectable.java
Expand Up @@ -15,13 +15,16 @@
*/
package org.jooq.lambda;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
Expand All @@ -31,6 +34,7 @@
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Generated;
import org.jooq.lambda.tuple.Tuple;
import org.jooq.lambda.tuple.Tuple10;
Expand Down Expand Up @@ -554,6 +558,46 @@ default <R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16,
*/
boolean noneMatch(Predicate<? super T> predicate);

/**
* Collect all bits in this stream into a single value by applying bitwise and.
*/
Optional<T> bitAnd();

/**
* Collect all bits in this stream into a single value by applying bitwise and.
*/
<U> Optional<U> bitAnd(Function<? super T, ? extends U> function);

/**
* Collect all bits in this stream into a single value by applying bitwise and.
*/
int bitAndInt(ToIntFunction<? super T> function);

/**
* Collect all bits in this stream into a single value by applying bitwise and.
*/
long bitAndLong(ToLongFunction<? super T> function);

/**
* Collect all bits in this stream into a single value by applying bitwise or.
*/
Optional<T> bitOr();

/**
* Collect all bits in this stream into a single value by applying bitwise or.
*/
<U> Optional<U> bitOr(Function<? super T, ? extends U> function);

/**
* Collect all bits in this stream into a single value by applying bitwise or.
*/
int bitOrInt(ToIntFunction<? super T> function);

/**
* Collect all bits in this stream into a single value by applying bitwise or.
*/
long bitOrLong(ToLongFunction<? super T> function);

/**
* Collect the collectable into an {@link ArrayList}.
*/
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/jooq/lambda/SeqImpl.java
Expand Up @@ -26,6 +26,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -393,6 +396,46 @@ public boolean noneMatch(Predicate<? super T> predicate) {
return stream().noneMatch(predicate);
}

@Override
public Optional<T> bitAnd() {
return collect(Agg.bitAnd());
}

@Override
public <U> Optional<U> bitAnd(Function<? super T, ? extends U> function) {
return collect(Agg.bitAnd(function));
}

@Override
public int bitAndInt(ToIntFunction<? super T> function) {
return collect(Agg.bitAndInt(function));
}

@Override
public long bitAndLong(ToLongFunction<? super T> function) {
return collect(Agg.bitAndLong(function));
}

@Override
public Optional<T> bitOr() {
return collect(Agg.bitOr());
}

@Override
public <U> Optional<U> bitOr(Function<? super T, ? extends U> function) {
return collect(Agg.bitOr(function));
}

@Override
public int bitOrInt(ToIntFunction<? super T> function) {
return collect(Agg.bitOrInt(function));
}

@Override
public long bitOrLong(ToLongFunction<? super T> function) {
return collect(Agg.bitOrLong(function));
}

@Override
public Optional<T> findFirst() {
return stream().findFirst();
Expand Down

0 comments on commit 09bd058

Please sign in to comment.