Skip to content

Instantly share code, notes, and snippets.

@YSMull
Last active June 9, 2018 04:36
Show Gist options
  • Save YSMull/af8d55aa9489394c6c00d8d3fb53a9ab to your computer and use it in GitHub Desktop.
Save YSMull/af8d55aa9489394c6c00d8d3fb53a9ab to your computer and use it in GitHub Desktop.
package chkr;
import java.util.function.Function;
@FunctionalInterface
interface CheckedFunction<T> {
T apply(T t) throws Exception;
}
public class Chkr {
private String chkrName = "";
private Chkr prevChkr;
private CheckedFunction checkFn;
public Chkr() {
}
private Chkr(CheckedFunction checkFn, Chkr chkr) {
this.checkFn = checkFn;
this.prevChkr = chkr;
}
@SuppressWarnings("unchecked")
public Chkr match(CheckedFunction check) {
return new Chkr(value -> {
try {
if (prevChkr != null) prevChkr.check(value);
return check.apply(value);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}, this);
}
@SuppressWarnings("unchecked")
public Chkr judge(Function<Object, Boolean> cond, String message) {
CheckedFunction check = value -> {
if (cond.apply(value)) {
return value;
} else {
throw new Exception("{" + value + "} " + message);
}
};
return match(check);
}
@SuppressWarnings("unchecked")
public Object check(Object value) throws Exception {
if (checkFn != null) {
return checkFn.apply(value);
} else {
// empty chkr
return value;
}
}
public Chkr called(String chkrName) {
this.chkrName = chkrName;
return this;
}
}
package chkr;
import java.util.Map;
import java.util.Objects;
public class T {
public static Chkr Null = new Chkr().judge(Objects::isNull, "is not Null").called("Null");
public static Chkr Any = new Chkr().judge(Objects::nonNull, "is Null").called("nonNull");
public static Chkr Num = Any.judge(v -> {
try {
long b = (long) v;
return true;
} catch (Exception e) {
return false;
}
}, "is not Num").called("Num");
public static Chkr Str = Any.judge(v -> {
try {
if (v == null) return false;
String b = (String) v;
return true;
} catch (Exception e) {
return false;
}
}, "is not Str").called("Str");
public static Chkr Or(Chkr... chkrs) {
return new Chkr().match(value -> {
boolean matchOne = false;
for (Chkr chkr : chkrs) {
try {
chkr.check(value);
matchOne = true;
break;
} catch (Exception exp) {
}
}
if (matchOne) {
return value;
} else {
// todo: print all failed ChkrNames that have been checked
throw new Exception("{" + value + "}" + " dose not match Or()");
}
});
}
public static void main(String[] args) {
try {
System.out.println(Num.check(null)); // throw "{null} is not Num"
System.out.println(Num.check(123)); // 123
System.out.println(Num.check("abc")); // throw "{abc} is not Num"
System.out.println(Str.check(null)); // throw "{null} is not Str"
System.out.println(Str.check("abc")); // abc
System.out.println(Str.check(123)); // throw "{123} is not Str"
System.out.println(Or(Str, Num).check(123)); // 123
System.out.println(Or(Str, Num).check("abc")); // abc
System.out.println(Or(Str, Num).check(true)); // throw "{true} dose not match Or()"
System.out.println(new Chkr().check(1)); // empty chkr just return the value passed in
var typeCheck = Map.of(
"userId", Num,
"userName", Str,
"phone", Str,
"IdCard", Or(Num, Str)
);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment