/** * Reduce By * * @param array array of items to reduce by * @param length where to end in the array * @param function function * @param function functionName * @return reduction */ public static long reduceBy(final int[] array, int length, Object function, String functionName) { if (function.getClass().isAnonymousClass()) { return reduceByR(array, length, function, functionName); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(function, functionName); MethodHandle methodHandle = callSite.dynamicInvoker(); try { long sum = 0; for (int index = 0; index < length; index++) { int v = array[index]; sum = (long) methodHandle.invokeExact(sum, v); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, length, function, functionName); } }
/** * Reflection based reduce by. * * @param array array of items to reduce by * @param object function * @param methodName name of method * @param <T> type of function * @return reduction */ private static <T> long reduceByR(final int[] array, T object, String methodName) { try { Method method = Invoker.invokeReducerLongIntReturnLongMethod(object, methodName); long sum = 0; for (int v : array) { sum = (long) method.invoke(object, sum, v); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } }
/** * Reflection based reduce by. * * @param array array of items to reduce by * @param object function * @param <T> type of function * @return reduction */ private static <T> long reduceByR(final int[] array, int length, T object) { try { Method method = Invoker.invokeReducerLongIntReturnLongMethod(object); long sum = 0; for (int index = 0; index < length; index++) { int v = array[index]; sum = (long) method.invoke(object, sum, v); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } }
public Group(Grouping grouping, Class<?> cls, List<?> list) { this.grouping = grouping; ArrayList<Criteria> criteriaArrayList = new ArrayList(); List<List<?>> lists = (List<List<?>>) list; for (List args : lists) { args = new ArrayList(args); args.add(1, cls); Object o = atIndex(args, -1); if (!(o instanceof List)) { atIndex(args, -1, Collections.singletonList(o)); } Criteria criteria = (Criteria) Invoker.invokeFromObject(ObjectFilter.class, "createCriteriaFromClass", args); criteriaArrayList.add(criteria); } this.expressions = criteriaArrayList; }
/** * Reduce by functional support for int arrays. * * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> long reduceBy(final int[] array, T object) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object); MethodHandle methodHandle = callSite.dynamicInvoker(); try { long sum = 0; for (int v : array) { sum = (long) methodHandle.invokeExact(sum, v); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object); } }