com.github.strangepleasures.match.Match Maven / Gradle / Ivy
The newest version!
package com.github.strangepleasures.match;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
public final class Match {
private Match() {
}
@SafeVarargs
@SuppressWarnings("unchecked")
public static R match(T value, MatchingFunction extends T, ? extends R>... functions) {
for (MatchingFunction function : functions) {
try {
if (function.matches(value)) {
return (R) function.apply(value);
}
} catch (ClassCastException ignore) {
}
}
return null;
}
@SafeVarargs
@SuppressWarnings("unchecked")
public static void match(Object value, MatchingConsumer extends T>... consumers) {
for (MatchingConsumer consumer : consumers) {
try {
if (consumer.matches(value)) {
consumer.accept(value);
return;
}
} catch (ClassCastException ignore) {
}
}
}
@SuppressWarnings("unchecked")
public static MatchingConsumer on(MatchingFunction filter, MatchingConsumer super T> consumer) {
return new MatchingConsumer() {
@Override
public boolean matches(Object value) {
return filter.matches(value) && filter.apply((T) value);
}
@Override
public void accept(T value) {
consumer.accept(value);
}
};
}
@SuppressWarnings("unchecked")
public static MatchingFunction on(MatchingFunction filter, MatchingFunction super T, R> function) {
return new MatchingFunction() {
@Override
public boolean matches(Object value) {
return filter.matches(value) && filter.apply((T) value);
}
@Override
public R apply(T value) {
return function.apply(value);
}
};
}
public static MatchingConsumer on(T expected, Runnable runnable) {
return new MatchingConsumer() {
@Override
public boolean matches(Object value) {
return Objects.equals(value, expected);
}
@Override
public void accept(T value) {
runnable.run();
}
};
}
public static MatchingFunction on(T expected, RunnableSupplier supplier) {
return new MatchingFunction() {
@Override
public boolean matches(Object value) {
return Objects.equals(value, expected);
}
@Override
public R apply(T t) {
return supplier.get();
}
};
}
public static MatchingConsumer otherwise(Consumer super T> consumer) {
return new MatchingConsumer() {
@Override
public boolean matches(Object value) {
return true;
}
@Override
public void accept(T value) {
consumer.accept(value);
}
};
}
public static MatchingFunction otherwise(Function function) {
return new MatchingFunction() {
@Override
public boolean matches(Object value) {
return true;
}
@Override
public R apply(T value) {
return function.apply(value);
}
};
}
public static T badArgument(Object arg) throws IllegalArgumentException {
throw new IllegalArgumentException(String.valueOf(arg));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy