cn.keayuan.util.Agent Maven / Gradle / Ivy
The newest version!
package cn.keayuan.util;
import cn.keayuan.util.function.Consumer;
import cn.keayuan.util.function.Function;
import cn.keayuan.util.function.ObjIntConsumer;
import cn.keayuan.util.function.ObjIntFunction;
import cn.keayuan.util.function.ObjIntPredicate;
import cn.keayuan.util.function.Predicate;
/**
* Created by keayuan on 2024.10.14.
*
* @author keayuan
*/
public interface Agent {
public boolean isNull();
public boolean isEmpty();
public int size();
public boolean isNotEmpty();
public E first();
public E getLast();
default boolean anyMatch(Predicate predicate) {
ObjectUtils.requireNonNull(predicate);
if (isEmpty()) return false;
final Wrapper wrapper = new Wrapper<>(false);
forEachInterruptibly(e -> {
if (predicate.test(e)) {
wrapper.setValue(true);
return true;
}
return false;
});
return wrapper.getValue();
}
default boolean allMatch(Predicate predicate) {
ObjectUtils.requireNonNull(predicate);
if (isEmpty()) return true;
final Wrapper wrapper = new Wrapper<>(true);
forEachInterruptibly(e -> {
if (!predicate.test(e)) {
wrapper.setValue(false);
return true;
}
return false;
});
return wrapper.getValue();
}
default Agent filter(Predicate predicate) {
ObjectUtils.requireNonNull(predicate);
return filter((e, i) -> predicate.test(e));
}
Agent filter(ObjIntPredicate predicate);
default int findIndex(Predicate predicate, boolean reverse) {
ObjectUtils.requireNonNull(predicate);
if (isEmpty()) return -1;
final Wrapper wrapper = new Wrapper<>(-1);
forEachInterruptibly(reverse, (e, i) -> {
if (predicate.test(e)) {
wrapper.setValue(i);
return true;
}
return false;
});
return wrapper.getValue();
}
default int findIndex(Predicate predicate) {
return findIndex(predicate, false);
}
default E find(Predicate predicate) {return find(predicate, false);}
default E find(Predicate predicate, boolean reverse) {
ObjectUtils.requireNonNull(predicate);
if (isEmpty()) return null;
final Wrapper wrapper = new Wrapper<>(null);
forEachInterruptibly(reverse, (e, i) -> {
if (predicate.test(e)) {
wrapper.setValue(e);
return true;
}
return false;
});
return wrapper.getValue();
}
default int lastIndex(Predicate predicate) {
return findIndex(predicate, true);
}
default E last(Predicate predicate) {
return find(predicate, true);
}
default void forEach(Consumer consumer) {
ObjectUtils.requireNonNull(consumer);
forEach(false, (e, i) -> consumer.accept(e));
}
default void forEach(ObjIntConsumer consumer) {
forEach(false, consumer);
}
void forEach(boolean reverse, ObjIntConsumer consumer);
default void forEachInterruptibly(Predicate function) {
ObjectUtils.requireNonNull(function);
forEachInterruptibly((e, i) -> function.test(e));
}
default void forEachInterruptibly(ObjIntPredicate function) {
forEachInterruptibly(false, function);
}
void forEachInterruptibly(boolean reverse, ObjIntPredicate function);
public Agent map(Function function);
public Agent map(ObjIntFunction function);
public C get();
}