com.tukeof.common.legacy.MatchWhen Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-core Show documentation
Show all versions of common-core Show documentation
a common and useful pure java library
The newest version!
package com.tukeof.common.legacy;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
class MatchWhen {
private T value;
private Object result;
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
private boolean matched;
private MatchWhen(T value) {
this.value = value;
this.result = null;
this.matched = false;
}
public static MatchWhen match(T value) {
return new MatchWhen<>(value);
}
public Object result() {
return result;
}
public MatchWhen when(T other, Function function) {
if (!isMatched()) {
if (value.equals(other)) {
result = function.apply(value);
setMatched();
}
}
return this;
}
public MatchWhen when(Iterable others, Function function) {
if (!isMatched()) {
for (T other : others) {
if (value.equals(other)) {
result = function.apply(value);
setMatched();
break;
}
}
}
return this;
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
public MatchWhen when(T[] others, Function function) {
if (!isMatched()) {
for (T other : others) {
if (value.equals(other)) {
result = function.apply(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenPrimitive(int[] others, Function function) {
if (!isMatched()) {
for (int other : others) {
if (value.equals(other)) {
result = function.apply(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenPrimitive(long[] others, Function function) {
if (!isMatched()) {
for (long other : others) {
if (value.equals(other)) {
result = function.apply(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenPrimitive(double[] others, Function function) {
if (!isMatched()) {
for (double other : others) {
if (value.equals(other)) {
result = function.apply(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenContain(String[] contexts, Function function) {
return whenContain(Arrays.asList(contexts), function);
}
public MatchWhen whenContain(Iterable contexts, Function function) {
if (!isMatched()) {
for (String context : contexts) {
if (value.toString().contains(context)) {
result = function.apply(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenContained(String[] contexts, Function function) {
return whenContained(Arrays.asList(contexts), function);
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
public MatchWhen whenContained(Iterable contexts, Function function) {
if (!isMatched()) {
for (String context : contexts) {
if (context.contains(value.toString())) {
result = function.apply(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenMatcher(String[] regexs, BiFunction function) {
List patterns = Arrays.stream(regexs)
.map(Pattern::compile)
.collect(Collectors.toList());
return whenMatcher(patterns, function);
}
public MatchWhen whenMatcher(Pattern[] patterns, BiFunction function) {
return whenMatcher(Arrays.asList(patterns), function);
}
public MatchWhen whenMatcher(Iterable patterns, BiFunction function) {
if (!isMatched()) {
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(value.toString());
if (matcher.matches()) {
result = function.apply(value, matcher);
setMatched();
break;
}
}
}
return this;
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
public MatchWhen when(Class> clazz, BiFunction, Object> function) {
if (!isMatched()) {
if (clazz.isInstance(value)) {
result = function.apply(value, clazz);
setMatched();
}
}
return this;
}
public MatchWhen when(Iterable> clazzes, BiFunction, Object> function) {
if (!isMatched()) {
for (Class> clazz : clazzes) {
if (clazz.isInstance(value)) {
result = function.apply(value, clazz);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen when(Class>[] clazzes, BiFunction, Object> function) {
if (!isMatched()) {
for (Class> clazz : clazzes) {
if (clazz.isInstance(value)) {
result = function.apply(value, clazz);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen orElse(Function function) {
if (!isMatched()) {
result = function.apply(value);
setMatched();
}
return this;
}
public MatchWhen when(T other, Consumer consumer) {
if (!isMatched()) {
if (value.equals(other)) {
consumer.accept(value);
setMatched();
}
}
return this;
}
public MatchWhen when(Iterable others, Consumer consumer) {
if (!isMatched()) {
for (T other : others) {
if (value.equals(other)) {
consumer.accept(value);
setMatched();
break;
}
}
}
return this;
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
public MatchWhen when(T[] others, Consumer consumer) {
if (!isMatched()) {
for (T other : others) {
if (value.equals(other)) {
consumer.accept(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenPrimitive(int[] others, Consumer consumer) {
if (!isMatched()) {
for (int other : others) {
if (value.equals(other)) {
consumer.accept(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenPrimitive(long[] others, Consumer consumer) {
if (!isMatched()) {
for (long other : others) {
if (value.equals(other)) {
consumer.accept(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenPrimitive(double[] others, Consumer consumer) {
if (!isMatched()) {
for (double other : others) {
if (value.equals(other)) {
consumer.accept(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenContain(String[] contexts, Consumer consumer) {
return whenContain(Arrays.asList(contexts), consumer);
}
public MatchWhen whenContain(Iterable contexts, Consumer consumer) {
if (!isMatched()) {
for (String context : contexts) {
if (value.toString().contains(context)) {
consumer.accept(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenContained(String[] contexts, Consumer consumer) {
return whenContained(Arrays.asList(contexts), consumer);
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
public MatchWhen whenContained(Iterable contexts, Consumer consumer) {
if (!isMatched()) {
for (String context : contexts) {
if (context.contains(value.toString())) {
consumer.accept(value);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen whenMatcher(String[] regexs, BiConsumer consumer) {
List patterns = Arrays.stream(regexs)
.map(Pattern::compile)
.collect(Collectors.toList());
return whenMatcher(patterns, consumer);
}
public MatchWhen whenMatcher(Pattern[] patterns, BiConsumer consumer) {
return whenMatcher(Arrays.asList(patterns), consumer);
}
public MatchWhen whenMatcher(Iterable patterns, BiConsumer consumer) {
if (!isMatched()) {
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(value.toString());
if (matcher.matches()) {
consumer.accept(value, matcher);
setMatched();
break;
}
}
}
return this;
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
public MatchWhen when(Class> clazz, BiConsumer> consumer) {
if (!isMatched()) {
if (clazz.isInstance(value)) {
consumer.accept(value, clazz);
setMatched();
}
}
return this;
}
public MatchWhen when(Iterable> clazzes, BiConsumer> consumer) {
if (!isMatched()) {
for (Class> clazz : clazzes) {
if (clazz.isInstance(value)) {
consumer.accept(value, clazz);
setMatched();
break;
}
}
}
return this;
}
public MatchWhen when(Class>[] clazzes, BiConsumer> consumer) {
if (!isMatched()) {
for (Class> clazz : clazzes) {
if (clazz.isInstance(value)) {
consumer.accept(value, clazz);
setMatched();
break;
}
}
}
return this;
}
public boolean isMatched() {
return matched;
}
private void setMatched() {
this.matched = true;
}
public MatchWhen orElse(Consumer consumer) {
if (!isMatched()) {
consumer.accept(value);
setMatched();
}
return this;
}
}