Skip to content

Commit

Permalink
Merge branch 'main' into 9727-jakarta-servlet-support
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Nov 14, 2023
2 parents d0ec92b + be37473 commit 29dc223
Show file tree
Hide file tree
Showing 26 changed files with 1,118 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ function installScript(filename) {
var docbody = doc.body;
var script = doc.createElement('script');
script.language='javascript';
script.crossOrigin='';
if (location.host) {
script.crossOrigin='';
}
script.src = code;
if (__MODULE_FUNC__.__errFn) {
script.onerror = function() {
Expand Down
13 changes: 13 additions & 0 deletions user/super/com/google/gwt/emul/java/util/Enumeration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,17 @@ public interface Enumeration<E> {
boolean hasMoreElements();

E nextElement();

default Iterator<E> asIterator() {
return new Iterator<E>() {
@Override
public boolean hasNext() {
return hasMoreElements();
}
@Override
public E next() {
return nextElement();
}
};
}
}
4 changes: 4 additions & 0 deletions user/super/com/google/gwt/emul/java/util/OptionalDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public boolean isPresent() {
return present;
}

public boolean isEmpty() {
return !present;
}

public double getAsDouble() {
checkCriticalElement(present);
return ref;
Expand Down
4 changes: 4 additions & 0 deletions user/super/com/google/gwt/emul/java/util/OptionalInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public boolean isPresent() {
return present;
}

public boolean isEmpty() {
return !present;
}

public int getAsInt() {
checkCriticalElement(present);
return ref;
Expand Down
4 changes: 4 additions & 0 deletions user/super/com/google/gwt/emul/java/util/OptionalLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public boolean isPresent() {
return present;
}

public boolean isEmpty() {
return !present;
}

public long getAsLong() {
checkCriticalElement(present);
return ref;
Expand Down
32 changes: 32 additions & 0 deletions user/super/com/google/gwt/emul/java/util/stream/Collectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,38 @@ public static Collector<CharSequence,?,String> joining(CharSequence delimiter) {
downstream.finisher());
}

public static <T, U, A, R> Collector<T, ?, R> flatMapping(Function<? super T,? extends Stream<?
extends U>> mapper, Collector<? super U, A, R> downstream) {
return new CollectorImpl<>(
downstream.supplier(),
(A a, T t) -> {
Stream<? extends U> stream = mapper.apply(t);
if (stream == null) {
return;
}
stream.forEach(u -> {
downstream.accumulator().accept(a, u);
});
},
downstream.combiner(),
downstream.finisher()
);
}

public static <T, A, R> Collector<T, ?, R> filtering(Predicate<? super T> predicate,
Collector<? super T, A, R> downstream) {
return new CollectorImpl<>(
downstream.supplier(),
(a, t) -> {
if (predicate.test(t)) {
downstream.accumulator().accept(a, t);
}
},
downstream.combiner(),
downstream.finisher()
);
}

public static <T> Collector<T,?,Optional<T>> maxBy(Comparator<? super T> comparator) {
return reducing(BinaryOperator.maxBy(comparator));
}
Expand Down
82 changes: 80 additions & 2 deletions user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,35 @@ public boolean tryAdvance(DoubleConsumer action) {
}

static DoubleStream iterate(double seed, DoubleUnaryOperator f) {
return iterate(seed, ignore -> true, f);
}

static DoubleStream iterate(double seed, DoublePredicate hasNext, DoubleUnaryOperator f) {
Spliterator.OfDouble spliterator =
new Spliterators.AbstractDoubleSpliterator(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private boolean first = true;
private double next = seed;
private boolean terminated = false;

@Override
public boolean tryAdvance(DoubleConsumer action) {
if (terminated) {
return false;
}
if (!first) {
next = f.applyAsDouble(next);
}
first = false;

if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsDouble(next);
return true;
}
};

return StreamSupport.doubleStream(spliterator, false);
}

Expand All @@ -185,6 +201,39 @@ static DoubleStream of(double t) {

DoubleStream distinct();

default DoubleStream dropWhile(DoublePredicate predicate) {
Spliterator.OfDouble prev = spliterator();
Spliterator.OfDouble spliterator =
new Spliterators.AbstractDoubleSpliterator(prev.estimateSize(),
prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) {
private boolean drop = true;
private boolean found;

@Override
public boolean tryAdvance(DoubleConsumer action) {
found = false;
if (drop) {
// drop items until we find one that matches
while (drop && prev.tryAdvance((double item) -> {
if (!predicate.test(item)) {
drop = false;
found = true;
action.accept(item);
}
})) {
// do nothing, work is done in tryAdvance
}
// only return true if we accepted at least one item
return found;
} else {
// accept one item, return result
return prev.tryAdvance(action);
}
}
};
return StreamSupport.doubleStream(spliterator, false);
}

DoubleStream filter(DoublePredicate predicate);

OptionalDouble findAny();
Expand Down Expand Up @@ -239,5 +288,34 @@ static DoubleStream of(double t) {

DoubleSummaryStatistics summaryStatistics();

default DoubleStream takeWhile(DoublePredicate predicate) {
Spliterator.OfDouble original = spliterator();
Spliterator.OfDouble spliterator =
new Spliterators.AbstractDoubleSpliterator(original.estimateSize(),
original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) {
private boolean take = true;
private boolean found;

@Override
public boolean tryAdvance(DoubleConsumer action) {
found = false;
if (!take) {
// already failed the check
return false;
}
original.tryAdvance((double item) -> {
if (predicate.test(item)) {
found = true;
action.accept(item);
} else {
take = false;
}
});
return found;
}
};
return StreamSupport.doubleStream(spliterator, false);
}

double[] toArray();
}
83 changes: 80 additions & 3 deletions user/super/com/google/gwt/emul/java/util/stream/IntStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,35 @@ public boolean tryAdvance(IntConsumer action) {
}

static IntStream iterate(int seed, IntUnaryOperator f) {
return iterate(seed, ignore -> true, f);
}

AbstractIntSpliterator spliterator =
static IntStream iterate(int seed, IntPredicate hasNext, IntUnaryOperator f) {
Spliterator.OfInt spliterator =
new Spliterators.AbstractIntSpliterator(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private boolean first = true;
private int next = seed;
private boolean terminated = false;

@Override
public boolean tryAdvance(IntConsumer action) {
if (terminated) {
return false;
}
if (!first) {
next = f.applyAsInt(next);
}
first = false;

if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsInt(next);
return true;
}
};

return StreamSupport.intStream(spliterator, false);
}

Expand Down Expand Up @@ -235,6 +250,39 @@ public boolean tryAdvance(IntConsumer action) {

IntStream distinct();

default IntStream dropWhile(IntPredicate predicate) {
Spliterator.OfInt prev = spliterator();
Spliterator.OfInt spliterator =
new Spliterators.AbstractIntSpliterator(prev.estimateSize(),
prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) {
private boolean drop = true;
private boolean found;

@Override
public boolean tryAdvance(IntConsumer action) {
found = false;
if (drop) {
// drop items until we find one that matches
while (drop && prev.tryAdvance((int item) -> {
if (!predicate.test(item)) {
drop = false;
found = true;
action.accept(item);
}
})) {
// do nothing, work is done in tryAdvance
}
// only return true if we accepted at least one item
return found;
} else {
// accept one item, return result
return prev.tryAdvance(action);
}
}
};
return StreamSupport.intStream(spliterator, false);
}

IntStream filter(IntPredicate predicate);

OptionalInt findAny();
Expand Down Expand Up @@ -289,5 +337,34 @@ public boolean tryAdvance(IntConsumer action) {

IntSummaryStatistics summaryStatistics();

default IntStream takeWhile(IntPredicate predicate) {
Spliterator.OfInt original = spliterator();
Spliterator.OfInt spliterator =
new Spliterators.AbstractIntSpliterator(original.estimateSize(),
original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) {
private boolean take = true;
private boolean found;

@Override
public boolean tryAdvance(IntConsumer action) {
found = false;
if (!take) {
// already failed the check
return false;
}
original.tryAdvance((int item) -> {
if (predicate.test(item)) {
found = true;
action.accept(item);
} else {
take = false;
}
});
return found;
}
};
return StreamSupport.intStream(spliterator, false);
}

int[] toArray();
}
Loading

0 comments on commit 29dc223

Please sign in to comment.