|
@@ -6,7 +6,6 @@ import net.ranides.assira.collection.arrays.NativeArrayBuilder;
|
|
|
import net.ranides.assira.collection.maps.OpenMap;
|
|
import net.ranides.assira.collection.maps.OpenMap;
|
|
|
import net.ranides.assira.collection.query.CQuery;
|
|
import net.ranides.assira.collection.query.CQuery;
|
|
|
import net.ranides.assira.collection.sets.OpenSet;
|
|
import net.ranides.assira.collection.sets.OpenSet;
|
|
|
-import net.ranides.assira.generic.Wrapper;
|
|
|
|
|
import net.ranides.assira.text.StrBuilder;
|
|
import net.ranides.assira.text.StrBuilder;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
@@ -102,44 +101,69 @@ public class BaseEach {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static <T> boolean matchAny(CQuery<T> that, Predicate<? super T> predicate) {
|
|
public static <T> boolean matchAny(CQuery<T> that, Predicate<? super T> predicate) {
|
|
|
- Wrapper<Boolean> out = Wrapper.of(false);
|
|
|
|
|
|
|
+ BaseState.Terminator out = BaseState.newTerminator(that);
|
|
|
that.whileEach(v -> {
|
|
that.whileEach(v -> {
|
|
|
if(predicate.test(v)) {
|
|
if(predicate.test(v)) {
|
|
|
- out.set(true);
|
|
|
|
|
|
|
+ out.stop();
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
return true;
|
|
return true;
|
|
|
});
|
|
});
|
|
|
- return out.get();
|
|
|
|
|
|
|
+ return out.stopped();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static <T> T reduce(CQuery<T> that, T identity, BinaryOperator<T> accumulator) {
|
|
public static <T> T reduce(CQuery<T> that, T identity, BinaryOperator<T> accumulator) {
|
|
|
- Wrapper<T> out = Wrapper.of(identity);
|
|
|
|
|
- that.forEach(v -> { out.set(accumulator.apply(out.get(), v)); });
|
|
|
|
|
|
|
+ BaseState.Holder<T> out = BaseState.newHolder(that, identity);
|
|
|
|
|
+ that.forEach(v -> {
|
|
|
|
|
+ out.set(accumulator.apply(out.get(), v));
|
|
|
|
|
+ });
|
|
|
return out.get();
|
|
return out.get();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static <T> T reduce(CQuery<T> that, BinaryOperator<T> accumulator) {
|
|
public static <T> T reduce(CQuery<T> that, BinaryOperator<T> accumulator) {
|
|
|
- Wrapper<T> out = Wrapper.of(null);
|
|
|
|
|
- that.forEach(v -> { out.set(out.isEmpty() ? v : accumulator.apply(out.get(), v)); });
|
|
|
|
|
|
|
+ BaseState.Holder<T> out = BaseState.newHolder(that);
|
|
|
|
|
+ that.forEach(v -> {
|
|
|
|
|
+ out.set(out.isEmpty() ? v : accumulator.apply(out.get(), v));
|
|
|
|
|
+ });
|
|
|
return out.get();
|
|
return out.get();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static <T> Optional<T> first(CQuery<T> that) {
|
|
public static <T> Optional<T> first(CQuery<T> that) {
|
|
|
- Wrapper<Optional<T>> out = Wrapper.of(Optional.empty());
|
|
|
|
|
|
|
+ BaseState.Holder<T> out = BaseState.newHolder(that);
|
|
|
that.whileEach(v -> {
|
|
that.whileEach(v -> {
|
|
|
- out.set(Optional.of(v));
|
|
|
|
|
|
|
+ out.set(v);
|
|
|
return false;
|
|
return false;
|
|
|
});
|
|
});
|
|
|
- return out.get();
|
|
|
|
|
|
|
+ return Optional.ofNullable(out.get());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static <T> Optional<T> last(CQuery<T> that) {
|
|
public static <T> Optional<T> last(CQuery<T> that) {
|
|
|
- Wrapper<T> out = Wrapper.of(null);
|
|
|
|
|
|
|
+ BaseState.Holder<T> out = BaseState.newHolder(that);
|
|
|
that.forEach(v -> {
|
|
that.forEach(v -> {
|
|
|
out.set(v);
|
|
out.set(v);
|
|
|
});
|
|
});
|
|
|
return Optional.ofNullable(out.get());
|
|
return Optional.ofNullable(out.get());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static <T> int indexOf(CQuery<T> that, Predicate<? super T> predicate) {
|
|
|
|
|
+ BaseState.Holder<Integer> out = BaseState.newHolder(that, -1);
|
|
|
|
|
+ that.whileEach((i,v) -> {
|
|
|
|
|
+ if(predicate.test(v)) {
|
|
|
|
|
+ out.set(i);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ return out.get();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static <T> int lastIndexOf(CQuery<T> that, Predicate<? super T> predicate) {
|
|
|
|
|
+ BaseState.Holder<Integer> out = BaseState.newHolder(that, -1);
|
|
|
|
|
+ that.forEach((i,v) -> {
|
|
|
|
|
+ if(predicate.test(v)) {
|
|
|
|
|
+ out.set(i);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ return out.get();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|