ArrayLikeLambdaOneArgumentExecutor.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package org.ssssssss.expression.parsing;
  2. import java.lang.reflect.Method;
  3. import java.util.*;
  4. import java.util.function.Supplier;
  5. public class ArrayLikeLambdaOneArgumentExecutor {
  6. public static final Set<String> SUPPORT_METHOD;
  7. public static final Map<String, Method> METHODS;
  8. static {
  9. Map<String, Method> temp = new HashMap<>();
  10. Set<String> set = new HashSet<>();
  11. addSupport(temp, set, "map");
  12. addSupport(temp, set, "filter");
  13. SUPPORT_METHOD = Collections.unmodifiableSet(set);
  14. METHODS = Collections.unmodifiableMap(temp);
  15. }
  16. private static void addSupport(Map<String, Method> temp, Set<String> set, String name) {
  17. set.add(name);
  18. addMethod(temp, name);
  19. }
  20. private static void addMethod(Map<String, Method> initialMap, String name) {
  21. try {
  22. initialMap.put(name, ArrayLikeLambdaOneArgumentExecutor.class.getMethod(name, Object.class, Object[].class));
  23. } catch (NoSuchMethodException e) {
  24. e.printStackTrace();
  25. throw new RuntimeException(e);
  26. }
  27. }
  28. @SuppressWarnings("unchecked")
  29. private static Object eachParse(Object arrayLike, Object argument, SPConsumer spConsumer) {
  30. List<Object> results = null;
  31. List<Object> args = (List<Object>) argument;
  32. results = new ArrayList<>(args.size());
  33. for (int j = 0; j < args.size(); j++) {
  34. SourceAndParsed<Object, Object> result = (SourceAndParsed<Object, Object>) ((Supplier) args.get(j)).get();
  35. spConsumer.accept(results, result);
  36. }
  37. if (arrayLike instanceof Collection) {
  38. return results;
  39. } else if (arrayLike.getClass().isArray()) {
  40. return results.toArray();
  41. } else if (arrayLike instanceof Iterator) {
  42. return results;
  43. } else if (arrayLike instanceof Enumeration) {
  44. return results;
  45. }
  46. throw new RuntimeException("未实现");
  47. }
  48. @SuppressWarnings("unchecked")
  49. public static Object map(Object arrayLike, Object... arguments) {
  50. return eachParse(arrayLike, arguments[0], (list, sp) -> list.add(sp.getParsed()));
  51. }
  52. @SuppressWarnings("unchecked")
  53. public static Object filter(Object arrayLike, Object... arguments) {
  54. return eachParse(arrayLike, arguments[0], (list, sp) -> {
  55. if (sp.getParsed() instanceof Boolean) {
  56. if ((Boolean)sp.getParsed()) {
  57. list.add(sp.getSource());
  58. }
  59. } else {
  60. throw new RuntimeException("lambda函数filter的结果非布尔类型");
  61. }
  62. });
  63. }
  64. public interface SPConsumer {
  65. void accept(List<Object> list, SourceAndParsed<Object, Object> sp);
  66. }
  67. public static class SourceAndParsed<S, P> {
  68. private S source;
  69. private P parsed;
  70. public SourceAndParsed(S source, P parsed) {
  71. this.source = source;
  72. this.parsed = parsed;
  73. }
  74. public P getParsed() {
  75. return parsed;
  76. }
  77. public void setParsed(P parsed) {
  78. this.parsed = parsed;
  79. }
  80. public S getSource() {
  81. return source;
  82. }
  83. public void setSource(S source) {
  84. this.source = source;
  85. }
  86. }
  87. }