Java Code Examples for org.apache.commons.lang3.ClassUtils#wrapperToPrimitive()

The following examples show how to use org.apache.commons.lang3.ClassUtils#wrapperToPrimitive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemberUtils.java    From jdmn with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the number of steps required to promote a primitive number to another
 * type.
 *
 * @param srcClass  the (primitive) source class
 * @param destClass the (primitive) destination class
 * @return The cost of promoting the primitive
 */
private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) {
    float cost = 0.0f;
    Class<?> cls = srcClass;
    if (!cls.isPrimitive()) {
        // slight unwrapping penalty
        cost += 0.1f;
        cls = ClassUtils.wrapperToPrimitive(cls);
    }
    for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) {
        if (cls == ORDERED_PRIMITIVE_TYPES[i]) {
            cost += 0.1f;
            if (i < ORDERED_PRIMITIVE_TYPES.length - 1) {
                cls = ORDERED_PRIMITIVE_TYPES[i + 1];
            }
        }
    }
    return cost;
}
 
Example 2
Source File: MemberUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the number of steps required to promote a primitive number to another
 * type.
 * @param srcClass the (primitive) source class
 * @param destClass the (primitive) destination class
 * @return The cost of promoting the primitive
 */
private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) {
    float cost = 0.0f;
    Class<?> cls = srcClass;
    if (!cls.isPrimitive()) {
        // slight unwrapping penalty
        cost += 0.1f;
        cls = ClassUtils.wrapperToPrimitive(cls);
    }
    for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) {
        if (cls == ORDERED_PRIMITIVE_TYPES[i]) {
            cost += 0.1f;
            if (i < ORDERED_PRIMITIVE_TYPES.length - 1) {
                cls = ORDERED_PRIMITIVE_TYPES[i + 1];
            }
        }
    }
    return cost;
}
 
Example 3
Source File: MemberUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the number of steps required to promote a primitive number to another
 * type.
 * @param srcClass the (primitive) source class
 * @param destClass the (primitive) destination class
 * @return The cost of promoting the primitive
 */
private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) {
    float cost = 0.0f;
    Class<?> cls = srcClass;
    if (!cls.isPrimitive()) {
        // slight unwrapping penalty
        cost += 0.1f;
        cls = ClassUtils.wrapperToPrimitive(cls);
    }
    for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) {
        if (cls == ORDERED_PRIMITIVE_TYPES[i]) {
            cost += 0.1f;
            if (i < ORDERED_PRIMITIVE_TYPES.length - 1) {
                cls = ORDERED_PRIMITIVE_TYPES[i + 1];
            }
        }
    }
    return cost;
}
 
Example 4
Source File: MemberUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the number of steps required to promote a primitive number to another type.
 * @param srcClass the (primitive) source class
 * @param destClass the (primitive) destination class
 * @return The cost of promoting the primitive
 */
private static float getPrimitivePromotionCost(final Class<?> srcClass,
        final Class<?> destClass) {
    float cost = 0.0f;
    Class<?> cls = srcClass;
    if (!cls.isPrimitive()) {
        // slight unwrapping penalty
        cost += 0.1f;
        cls = ClassUtils.wrapperToPrimitive(cls);
    }
    for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) {
        if (cls == ORDERED_PRIMITIVE_TYPES[i]) {
            cost += 0.1f;
            if (i < ORDERED_PRIMITIVE_TYPES.length - 1) {
                cls = ORDERED_PRIMITIVE_TYPES[i + 1];
            }
        }
    }
    return cost;
}
 
Example 5
Source File: MemberUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the number of steps required to promote a primitive number to another
 * type.
 * @param srcClass the (primitive) source class
 * @param destClass the (primitive) destination class
 * @return The cost of promoting the primitive
 */
private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) {
    float cost = 0.0f;
    Class<?> cls = srcClass;
    if (!cls.isPrimitive()) {
        // slight unwrapping penalty
        cost += 0.1f;
        cls = ClassUtils.wrapperToPrimitive(cls);
    }
    for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) {
        if (cls == ORDERED_PRIMITIVE_TYPES[i]) {
            cost += 0.1f;
            if (i < ORDERED_PRIMITIVE_TYPES.length - 1) {
                cls = ORDERED_PRIMITIVE_TYPES[i + 1];
            }
        }
    }
    return cost;
}
 
Example 6
Source File: MemberUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the number of steps required to promote a primitive number to another
 * type.
 * @param srcClass the (primitive) source class
 * @param destClass the (primitive) destination class
 * @return The cost of promoting the primitive
 */
private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) {
    float cost = 0.0f;
    Class<?> cls = srcClass;
    if (!cls.isPrimitive()) {
        // slight unwrapping penalty
        cost += 0.1f;
        cls = ClassUtils.wrapperToPrimitive(cls);
    }
    for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) {
        if (cls == ORDERED_PRIMITIVE_TYPES[i]) {
            cost += 0.1f;
            if (i < ORDERED_PRIMITIVE_TYPES.length - 1) {
                cls = ORDERED_PRIMITIVE_TYPES[i + 1];
            }
        }
    }
    return cost;
}
 
Example 7
Source File: RuleEvaluator.java    From proctor with Apache License 2.0 6 votes vote down vote up
/**
 * @throws IllegalArgumentException if type of expression is not boolean
 */
static void checkRuleIsBooleanType(final String rule, final ELContext elContext, final ValueExpression ve) {
    // apache-el is an expression language, not a rule language, and it is very lenient
    // sadly that means it will just evaluate to false when users make certain mistakes, e.g. by
    // coercing String value "xyz" to boolean false, instead of throwing an exception.
    // To support users writing rules, be more strict here in requiring the type of the
    // value to be expected before coercion
    Class<?> type = ve.getType(elContext);
    if (ClassUtils.isPrimitiveWrapper(type)) {
        type = ClassUtils.wrapperToPrimitive(type);
    }
    // allow null to be coerced for historic reasons
    if ((type != null) && (type != boolean.class)) {
        throw new IllegalArgumentException("Received non-boolean return value: " + type + " from rule " + rule);
    }
}
 
Example 8
Source File: AppDataPushAgent.java    From Bats with Apache License 2.0 5 votes vote down vote up
private JSONObject getMetricsSchemaData(LogicalPlan.OperatorMeta operatorMeta, Map<String, Object> aggregates)
{
  JSONObject result = new JSONObject();
  try {
    result.put("type", METRICS_SCHEMA);
    result.put("version", METRICS_SCHEMA_VERSION);
    result.put("appUser", appContext.getUser());
    result.put("appName", dnmgr.getApplicationAttributes().get(DAGContext.APPLICATION_NAME));
    result.put("logicalOperatorName", operatorMeta.getName());

    MetricAggregatorMeta metricAggregatorMeta = operatorMeta.getMetricAggregatorMeta();
    JSONArray valueSchemas = new JSONArray();
    for (Map.Entry<String, Object> entry : aggregates.entrySet()) {
      String metricName = entry.getKey();
      Object metricValue = entry.getValue();
      JSONObject valueSchema = new JSONObject();
      valueSchema.put("name", metricName);
      Class<?> type = ClassUtils.wrapperToPrimitive(metricValue.getClass());
      valueSchema.put("type", type == null ? metricValue.getClass().getCanonicalName() : type);
      String[] dimensionAggregators = metricAggregatorMeta.getDimensionAggregatorsFor(metricName);
      if (dimensionAggregators != null) {
        valueSchema.put("dimensionAggregators", Arrays.asList(dimensionAggregators));
      }
      valueSchemas.put(valueSchema);
    }
    result.put("values", valueSchemas);
    String[] timeBuckets = metricAggregatorMeta.getTimeBuckets();
    if (timeBuckets != null) {
      result.put("timeBuckets", Arrays.asList(timeBuckets));
    }

  } catch (JSONException ex) {
    throw new RuntimeException(ex);
  }
  return result;
}
 
Example 9
Source File: ReflectionUtil.java    From sling-org-apache-sling-models-impl with Apache License 2.0 5 votes vote down vote up
public static Type mapWrapperClasses(Type type) {
    if (type instanceof Class<?>) {
        return ClassUtils.wrapperToPrimitive((Class<?>) type);
    } else {
        return type;
    }
}
 
Example 10
Source File: AbstractDeepCopyableTest.java    From morf with Apache License 2.0 5 votes vote down vote up
private boolean typeIsOfInterest(Class<?> clazz) {
  if (clazz == String.class ||
      clazz.isEnum() ||
      clazz.isArray() ||
      clazz.isPrimitive() ||
      ClassUtils.wrapperToPrimitive(clazz) != null) {
    return false;
  }
  return true;
}
 
Example 11
Source File: AppDataPushAgent.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
private JSONObject getMetricsSchemaData(LogicalPlan.OperatorMeta operatorMeta, Map<String, Object> aggregates)
{
  JSONObject result = new JSONObject();
  try {
    result.put("type", METRICS_SCHEMA);
    result.put("version", METRICS_SCHEMA_VERSION);
    result.put("appUser", appContext.getUser());
    result.put("appName", dnmgr.getApplicationAttributes().get(DAGContext.APPLICATION_NAME));
    result.put("logicalOperatorName", operatorMeta.getName());

    MetricAggregatorMeta metricAggregatorMeta = operatorMeta.getMetricAggregatorMeta();
    JSONArray valueSchemas = new JSONArray();
    for (Map.Entry<String, Object> entry : aggregates.entrySet()) {
      String metricName = entry.getKey();
      Object metricValue = entry.getValue();
      JSONObject valueSchema = new JSONObject();
      valueSchema.put("name", metricName);
      Class<?> type = ClassUtils.wrapperToPrimitive(metricValue.getClass());
      valueSchema.put("type", type == null ? metricValue.getClass().getCanonicalName() : type);
      String[] dimensionAggregators = metricAggregatorMeta.getDimensionAggregatorsFor(metricName);
      if (dimensionAggregators != null) {
        valueSchema.put("dimensionAggregators", Arrays.asList(dimensionAggregators));
      }
      valueSchemas.put(valueSchema);
    }
    result.put("values", valueSchemas);
    String[] timeBuckets = metricAggregatorMeta.getTimeBuckets();
    if (timeBuckets != null) {
      result.put("timeBuckets", Arrays.asList(timeBuckets));
    }

  } catch (JSONException ex) {
    throw new RuntimeException(ex);
  }
  return result;
}
 
Example 12
Source File: ValueMapInjector.java    From sling-org-apache-sling-models-impl with Apache License 2.0 4 votes vote down vote up
@Override
public Object getValue(@NotNull Object adaptable, String name, @NotNull Type type, @NotNull AnnotatedElement element,
        @NotNull DisposalCallbackRegistry callbackRegistry) {
    if (adaptable == ObjectUtils.NULL) {
        return null;
    }
    ValueMap map = getValueMap(adaptable);
    if (map == null) {
        return null;
    } else if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        try {
            return map.get(name, clazz);
        } catch (ClassCastException e) {
            // handle case of primitive/wrapper arrays
            if (clazz.isArray()) {
                Class<?> componentType = clazz.getComponentType();
                if (componentType.isPrimitive()) {
                    Class<?> wrapper = ClassUtils.primitiveToWrapper(componentType);
                    if (wrapper != componentType) {
                        Object wrapperArray = map.get(name, Array.newInstance(wrapper, 0).getClass());
                        if (wrapperArray != null) {
                            return unwrapArray(wrapperArray, componentType);
                        }
                    }
                } else {
                    Class<?> primitiveType = ClassUtils.wrapperToPrimitive(componentType);
                    if (primitiveType != componentType) {
                        Object primitiveArray = map.get(name, Array.newInstance(primitiveType, 0).getClass());
                        if (primitiveArray != null) {
                            return wrapArray(primitiveArray, componentType);
                        }
                    }
                }
            }
            return null;
        }
    } else if (type instanceof ParameterizedType) {
        // list support
        ParameterizedType pType = (ParameterizedType) type;
        if (pType.getActualTypeArguments().length != 1) {
            return null;
        }
        Class<?> collectionType = (Class<?>) pType.getRawType();
        if (!(collectionType.equals(Collection.class) || collectionType.equals(List.class))) {
            return null;
        }

        Class<?> itemType = (Class<?>) pType.getActualTypeArguments()[0];
        Object array = map.get(name, Array.newInstance(itemType, 0).getClass());
        if (array == null) {
            return null;

        }

        return Arrays.asList((Object[]) array);
    } else {
        log.debug("ValueMapInjector doesn't support non-class types {}", type);
        return null;
    }
}
 
Example 13
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether the specified object is a scalar value. This method is
 * called by {@code getList()} and {@code getStringArray()} if the
 * property requested is not a string, a list, or an array. If it returns
 * <b>true</b>, the calling method transforms the value to a string and
 * returns a list or an array with this single element. This implementation
 * returns <b>true</b> if the value is of a wrapper type for a primitive
 * type.
 *
 * @param value the value to be checked
 * @return a flag whether the value is a scalar
 * @since 1.7
 */
protected boolean isScalarValue(final Object value)
{
    return ClassUtils.wrapperToPrimitive(value.getClass()) != null;
}