@Override public Object of(Object o) { InvokeDynamic dyn = new InvokeDynamic(); return dyn.execute(Arrays.asList("of","singleton","some","right","success"),type,o); }
/** * @return New Stream */ default Stream<T> stream(){ Object streamable = getStreamable(); if(streamable instanceof Stream) return (Stream)streamable; if(streamable instanceof Iterable) return StreamSupport.stream(((Iterable)streamable).spliterator(), false); return new InvokeDynamic().stream(streamable).orElseGet( ()-> (Stream)StreamSupport.stream(asDecomposable(streamable) .unapply() .spliterator(), false)); }
default T get(){ Object gettable = unwrap(); if(gettable instanceof Supplier) return ((Supplier<T>)gettable).get(); if(gettable instanceof Callable){ try { return ((Callable<T>)gettable).call(); } catch (Exception e) { ExceptionSoftener.singleton.factory.getInstance().throwSoftenedException(e); } } return new InvokeDynamic().<T>supplier(gettable,getSupplierMethodNames()).get(); } }
public A zero(){ return (A)invokeDynamic.execute("zero",o).get(); } public BiFunction<A,A,A> combiner(){
public <T> Optional<T> supplier(Object t,List<String> methodNames) { Class clazz = t.getClass(); Optional<Method> om = streamMethod.computeIfAbsent( clazz, c -> Stream.of(c.getMethods()) .filter(method -> methodNames.contains(method.getName())) .filter(method -> method.getParameterCount() == 0) .findFirst().map(m2 -> { m2.setAccessible(true); return m2; })); if (!om.isPresent()) return Optional.empty(); Method m = om.get(); return Optional.of( (T)executeMethod( m,t)); }
public BiFunction<A,A,A> combiner(){ return (a,b) -> (A) invokeDynamic.execute(Arrays.asList("sum","combine"),o,a,b).get(); }
public Optional<Stream> stream(Object t) { Class clazz = t.getClass(); Optional<Method> om = streamMethod.computeIfAbsent( clazz, c -> Stream.of(c.getMethods()) .filter(method -> "stream".equals(method.getName()) || "toStream".equals(method.getName())) .filter(method -> method.getParameterCount() == 0) .findFirst().map(m2 -> { m2.setAccessible(true); return m2; })); if (!om.isPresent()) return Optional.empty(); Method m = om.get(); return Optional.of((Stream) executeMethod( m,t)); } public <T> Optional<T> execute(List<String> methodNames, Object obj, Object... args){
@Override public Object empty() { InvokeDynamic dyn = new InvokeDynamic(); return dyn.execute(Arrays.asList("of","empty","cons","none","nil","left","failure"),type); }
public <T> Optional<T> execute(List<String> methodNames, Object obj, Object... args){ return (Optional)methodNames.stream().map(s -> execute(s,obj,args)).filter(Optional::isPresent).findFirst().get(); } public <T> Optional<T> execute(String methodName,Object obj,Object... args) {
public <T> Optional<T> execute(String methodName,Object obj,Object... args) { Class clazz = obj.getClass(); Map<Class, List<Method>> methods = generalMethods.computeIfAbsent(methodName, k->new ConcurrentHashMap<>()); List<Method> om = methods.computeIfAbsent( clazz, c -> Stream.of(c.getMethods()) .filter(method -> methodName.equals(method.getName())) .filter(method -> method.getParameterCount() == args.length) .map(m2 -> { m2.setAccessible(true); return m2; }).collect(Collectors.toList())); if(om.size()>0){ return Optional.of((T)executeMethod( om.get(0),obj,args)); } return Optional.empty(); } public <T> Optional<T> supplier(Object t,List<String> methodNames) {