Esempio n. 1
0
  /**
   * 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);
    }
  }
Esempio n. 2
0
  /**
   * 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);
    }
  }