/** {@inheritDoc} */
  @Override
  @SuppressWarnings("unchecked")
  public <N extends Number> Expression<N> sum(N x, Expression<? extends N> y) {
    final TypeImpl<N> type = (TypeImpl<N>) this.metamodel.type(x.getClass());

    return new ArithmeticExression<N>(
        ArithmeticOperation.ADD, new EntityConstantExpression<N>(type, x), y);
  }
  @Override
  @SuppressWarnings({"unchecked"})
  public <N extends Number> Expression<N> diff(N n, Expression<? extends N> expression) {
    if (n == null || expression == null) {
      throw new IllegalArgumentException("arguments to diff() cannot be null");
    }

    final Class resultType =
        BinaryArithmeticOperation.determineResultType(n.getClass(), expression.getJavaType());

    return new BinaryArithmeticOperation<N>(
        this, resultType, BinaryArithmeticOperation.Operation.SUBTRACT, n, expression);
  }
  @Override
  @SuppressWarnings({"unchecked"})
  public <N extends Number> Expression<N> prod(Expression<? extends N> expression, N n) {
    if (expression == null || n == null) {
      throw new IllegalArgumentException("arguments to prod() cannot be null");
    }

    final Class resultType =
        BinaryArithmeticOperation.determineResultType(expression.getJavaType(), n.getClass());

    return new BinaryArithmeticOperation<N>(
        this, resultType, BinaryArithmeticOperation.Operation.MULTIPLY, expression, n);
  }
 @SuppressWarnings("unchecked")
 public static <N extends Number> N zeroLike(N val) {
   if (val instanceof Double) {
     return (N) (Double) 0d;
   }
   if (val instanceof Float) {
     return (N) (Float) 0f;
   }
   if (val instanceof Long) {
     return (N) (Long) 0l;
   }
   if (val instanceof Integer) {
     return (N) (Integer) 0;
   }
   if (val instanceof Short) {
     return (N) (Short) (short) 0;
   }
   throw new IllegalArgumentException("Cannot add to " + val.getClass().getName());
 }
 @SuppressWarnings("unchecked")
 public static <N extends Number> N addTo(N val, double more) {
   if (val instanceof Double) {
     return (N) new Double(((Double) val).doubleValue() + more);
   }
   if (val instanceof Float) {
     return (N) new Float(((Float) val).floatValue() + more);
   }
   if (val instanceof Long) {
     return (N) new Long((long) (((Long) val).longValue() + more));
   }
   if (val instanceof Integer) {
     return (N) new Integer((int) (((Integer) val).intValue() + more));
   }
   if (val instanceof Short) {
     return (N) new Short((short) (((Short) val).shortValue() + more));
   }
   throw new IllegalArgumentException("Cannot add to " + val.getClass().getName());
 }
Beispiel #6
0
    /**
     * Gets BufferType given a Number. User should check for null return.
     *
     * @param <N> the getType of value, a Number
     * @param value the Number to match against enum values
     * @return BufferType if match is found, null if not
     * @throws NullPointerException
     */
    public static <N extends Number> BufferType getType(N value) {
      if (value == null) {
        throw new NullPointerException();
      }
      BufferType type = null;
      String instanceClassName = value.getClass().getName();

      for (BufferType t : BufferType.values()) {
        if (instanceClassName.contains(t.VALUE_CLASS.getSimpleName())) {
          type = t;
          break;
        }
      }

      if (type == null) {
        throw new NullPointerException();
      }

      return type;
    }