ArrayLikeLambdaExecutor.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.ssssssss.expression.parsing;
  2. import java.lang.reflect.Method;
  3. import java.util.*;
  4. import java.util.function.Supplier;
  5. public class ArrayLikeLambdaExecutor {
  6. public static final Set<String> SUPPORT_METHOD = new HashSet<>();
  7. public static final Map<String, Method> METHODS;
  8. static {
  9. Map<String, Method> temp = new HashMap<>();
  10. addSupport(temp, "map");
  11. METHODS = Collections.unmodifiableMap(temp);
  12. }
  13. private static void addSupport(Map<String, Method> temp, String name) {
  14. SUPPORT_METHOD.add(name);
  15. init(temp, name);
  16. }
  17. private static void init(Map<String, Method> initialMap, String name) {
  18. try {
  19. initialMap.put(name, ArrayLikeLambdaExecutor.class.getMethod(name, Object.class, Object[].class));
  20. } catch (NoSuchMethodException e) {
  21. e.printStackTrace();
  22. throw new RuntimeException(e);
  23. }
  24. }
  25. @SuppressWarnings("unchecked")
  26. public static Object map(Object arrayLike, Object... arguments) {
  27. // System.err.println("ArrayLikeLambdaExecutor:11 " + arrayLike);
  28. List<Object> results = null;
  29. Object argument = arguments[0];
  30. List<Object> args = (List<Object>) argument;
  31. results = new ArrayList<>(args.size());
  32. for (int j = 0; j < args.size(); j++) {
  33. Object result = ((Supplier) args.get(j)).get();
  34. results.add(result);
  35. }
  36. if (arrayLike instanceof Collection) {
  37. return results;
  38. } else if (arrayLike.getClass().isArray()) {
  39. return results.toArray();
  40. } else if (arrayLike instanceof Iterator) {
  41. } else if (arrayLike instanceof Enumeration) {
  42. }
  43. throw new RuntimeException("未实现");
  44. }
  45. }