List of usage examples for org.apache.commons.lang3 ClassUtils isPrimitiveOrWrapper
public static boolean isPrimitiveOrWrapper(final Class<?> type)
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
/** * Checks if classA or classB can be auto boxed by the JVM * * @return {@code true}, if both classes are either primitive or wrapper classes and * autoboxing is possible between {@code classA} and {@code classB} *///from w w w.ja va 2s . co m private static boolean canBeAutoboxed(Class<?> classA, Class<?> classB) { return ClassUtils.isPrimitiveOrWrapper(classA) && ClassUtils.isPrimitiveOrWrapper(classB) && (classB.equals(classA) // Same types || ClassUtils.primitiveToWrapper(classB).equals(classA) // Matching primitive-wrapper pair, e.g. double - Double || ClassUtils.primitiveToWrapper(classA).equals(classB)); // Matching wrapper-primitive pair, e.g. Long - long }
From source file:com.intuit.karate.Script.java
public static AssertionResult matchNestedObject(char delimiter, String path, MatchType matchType, Object actRoot, Object actObject, Object expObject, ScriptContext context) { logger.trace("path: {}, actual: '{}', expected: '{}'", path, actObject, expObject); if (expObject == null) { if (actObject != null) { return matchFailed(path, actObject, expObject, "actual value is not null"); }// www . j av a 2 s . co m return AssertionResult.PASS; // both are null } if (expObject instanceof String) { ScriptValue actValue = new ScriptValue(actObject); return matchStringOrPattern(delimiter, path, matchType, actRoot, actValue, expObject.toString(), context); } else if (expObject instanceof Map) { if (!(actObject instanceof Map)) { return matchFailed(path, actObject, expObject, "actual value is not of type 'map'"); } Map<String, Object> expMap = (Map) expObject; Map<String, Object> actMap = (Map) actObject; if (matchType != MatchType.CONTAINS && actMap.size() > expMap.size()) { // > is because of the chance of #ignore return matchFailed(path, actObject, expObject, "actual value has more keys than expected - " + actMap.size() + ":" + expMap.size()); } for (Map.Entry<String, Object> expEntry : expMap.entrySet()) { // TDDO should we assert order, maybe XML needs this ? String key = expEntry.getKey(); String childPath = path + delimiter + key; AssertionResult ar = matchNestedObject(delimiter, childPath, MatchType.EQUALS, actRoot, actMap.get(key), expEntry.getValue(), context); if (!ar.pass) { return ar; } } return AssertionResult.PASS; // map compare done } else if (expObject instanceof List) { List expList = (List) expObject; List actList = (List) actObject; int actCount = actList.size(); int expCount = expList.size(); if (matchType != MatchType.CONTAINS && actCount != expCount) { return matchFailed(path, actObject, expObject, "actual and expected arrays are not the same size - " + actCount + ":" + expCount); } if (matchType == MatchType.CONTAINS || matchType == MatchType.CONTAINS_ONLY) { // just checks for existence for (Object expListObject : expList) { // for each expected item in the list boolean found = false; for (int i = 0; i < actCount; i++) { Object actListObject = actList.get(i); String listPath = buildListPath(delimiter, path, i); AssertionResult ar = matchNestedObject(delimiter, listPath, MatchType.EQUALS, actRoot, actListObject, expListObject, context); if (ar.pass) { // exact match, we found it found = true; break; } } if (!found) { return matchFailed(path + "[*]", actObject, expListObject, "actual value does not contain expected"); } } return AssertionResult.PASS; // all items were found } else { // exact compare of list elements and order for (int i = 0; i < expCount; i++) { Object expListObject = expList.get(i); Object actListObject = actList.get(i); String listPath = buildListPath(delimiter, path, i); AssertionResult ar = matchNestedObject(delimiter, listPath, MatchType.EQUALS, actRoot, actListObject, expListObject, context); if (!ar.pass) { return matchFailed(listPath, actListObject, expListObject, "[" + ar.message + "]"); } } return AssertionResult.PASS; // lists (and order) are identical } } else if (ClassUtils.isPrimitiveOrWrapper(expObject.getClass())) { return matchPrimitive(path, actObject, expObject); } else if (expObject instanceof BigDecimal) { BigDecimal expNumber = (BigDecimal) expObject; if (actObject instanceof BigDecimal) { BigDecimal actNumber = (BigDecimal) actObject; if (actNumber.compareTo(expNumber) != 0) { return matchFailed(path, actObject, expObject, "not equal (big decimal)"); } } else { BigDecimal actNumber = convertToBigDecimal(actObject); if (actNumber == null || actNumber.compareTo(expNumber) != 0) { return matchFailed(path, actObject, expObject, "not equal (primitive : big decimal)"); } } return AssertionResult.PASS; } else { // this should never happen throw new RuntimeException("unexpected type: " + expObject.getClass()); } }
From source file:com.feilong.tools.jsonlib.JsonUtil.java
/** * ??json format.// ww w. j a v a 2 s . co m * * @param <V> * the value type * @param value * the value * @param allowClassTypes * the allow class types * @return true, if checks if is allow format type * @since 1.4.0 */ private static <V> boolean isAllowFormatType(V value, Class<?>... allowClassTypes) { if (null == value) {//null ? format return true; } Class<?> klassClass = value.getClass(); return ClassUtils.isPrimitiveOrWrapper(klassClass) // || String.class == klassClass // || ObjectUtil.isArray(value)//XXX ?? || ClassUtil.isInstanceAnyClass(value, allowClassTypes)// ; }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#multiply(java.lang.Object, java.lang.Number, java.lang.Class)}. *//*from w w w . j a va2 s. c om*/ @SuppressWarnings("unchecked") @Test public void testMultiplyObjectNumberClassOfT() { assertEquals("null", null, multiply(null, null, null)); assertEquals("null", null, multiply(1, 23, null)); assertEquals("null", null, multiply(1, 2, null)); try { for (Class<?> type : PRIMITIVES) { Object expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class) .invoke(null, "0"); assertEquals("fallback: " + type.getSimpleName(), expected, multiply(null, null, (Class<? extends Number>) type)); } for (Class<?> type : NUMBERS) { Object expected = valueOf(new BigDecimal("12").multiply(new BigDecimal("3.456")), (Class<? extends Number>) type); assertEquals("12 * 3.456:" + type.getSimpleName(), expected, multiply(12, 3.456, (Class<? extends Number>) type)); if (ClassUtils.isPrimitiveOrWrapper(type)) { expected = valueOf(valueOf("-Infinity"), (Class<? extends Number>) type); } else { expected = INFINITY_DOUBLE.pow(2).negate(); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("Infinity * -Infinity: " + type.getSimpleName(), expected, multiply("Infinity", Double.NEGATIVE_INFINITY, (Class<? extends Number>) type)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#remainder(java.lang.Object, java.lang.Number, java.lang.Class)}. */// w ww . j av a 2 s.c om @SuppressWarnings("unchecked") @Test public void testRemainderObjectNumberClassOfT() { assertEquals("null", null, remainder(null, null, null)); assertEquals("null", null, remainder(10, 3, null)); assertEquals("10 / 3", (Object) 1, remainder(10, 3, int.class)); assertEquals("10 / 3", (Object) 1, remainder(10d, 3, int.class)); assertEquals("10 / 3", (Object) 1f, remainder(10d, 3, float.class)); try { for (Class<?> type : NUMBERS) { Class<? extends Number> typeOfN = (Class<? extends Number>) type; Object expected = null; if (ClassUtils.isPrimitiveOrWrapper(type)) { expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class).invoke(null, "1"); } else { expected = type.getField("ONE").get(null); } for (Class<?> valueType : NUMBERS) { Object o = null; if (ClassUtils.isPrimitiveOrWrapper(valueType)) { o = ClassUtils.primitiveToWrapper(valueType).getMethod("valueOf", String.class).invoke(null, "10"); } else { o = valueType.getField("TEN").get(null); } assertEquals(o + "(" + o.getClass() + ") / 3: " + type.getSimpleName(), expected, remainder(o, 3, typeOfN)); } } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#round(Object, Class)}. */// w w w . ja va 2s .co m @SuppressWarnings("unchecked") @Test public void testRoundObjectClassOfT() { try { for (Class<?> type : NUMBERS) { Object expected = null; if (ClassUtils.isPrimitiveOrWrapper(type)) { expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class).invoke(null, "3"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); expected = c.newInstance("3"); } assertEquals("PI: " + type.getSimpleName(), expected, round(Math.PI, (Class<? extends Number>) type)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#round(Object, Number, RoundingMode, Class)}. *//*ww w .j ava2 s. c om*/ @SuppressWarnings("unchecked") @Test public void testRoundObjectNumberRoundingModeClassOfT() { assertEquals("null", null, round(null, null, null, null)); try { for (Class<?> type : NUMBERS) { Object expected = null; if (ObjectUtils.isAny(ClassUtils.primitiveToWrapper(type), Float.class, Double.class)) { expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class).invoke(null, "3.14"); } else if (ClassUtils.isPrimitiveOrWrapper(type)) { expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class).invoke(null, "3"); } else { expected = new BigDecimal("3.14"); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("PI: " + type.getSimpleName(), expected, round(Math.PI, 2, RoundingMode.HALF_UP, (Class<? extends Number>) type)); if (ObjectUtils.isAny(ClassUtils.primitiveToWrapper(type), Float.class, Double.class)) { expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class).invoke(null, "3.15"); } else if (ClassUtils.isPrimitiveOrWrapper(type)) { expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class).invoke(null, "3"); } else { expected = new BigDecimal("3.15"); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("PI: " + type.getSimpleName(), expected, round(Math.PI, 2, RoundingMode.CEILING, (Class<? extends Number>) type)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:at.gridtec.lambda4j.generator.util.LambdaUtils.java
/** * Checks if the given string represents a primitive type. * * @param type The type in string representation * @return {@code true} if, and only if, given string represents a primitive type, {@code false} otherwise. *//* w w w . java2s .com*/ public static boolean isPrimitiveType(@Nullable final TypeEntity type) { return type != null && ClassUtils.isPrimitiveOrWrapper(type.getTypeClass()); }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#subtract(java.lang.Object, java.lang.Number, java.lang.Class)}. *//*w w w .jav a 2 s .co m*/ @SuppressWarnings("unchecked") @Test public void testSubtractObjectNumberClassOfT() { assertEquals("null", null, subtract(null, null, null)); assertEquals("null", null, subtract("123.456", null, null)); assertEquals("null", null, subtract("123.456", 1, null)); assertEquals("null", null, subtract("", 10, null)); assertEquals("null", null, subtract(null, 10, null)); assertEquals("null", null, subtract(123.456, 10, null)); assertEquals("123.456 - .456", (Object) 123, subtract(123.456, .456, int.class)); assertEquals("123.456 - .456: Float", (Object) 123f, subtract(123.456, .456, Float.class)); assertEquals("123.912 - .456: Float", (Object) 123.456d, subtract(123.912, .456, Double.class)); assertEquals("123.912 - .456: Float", (Object) 123, subtract(123.912, .456, Integer.class)); assertEquals("123.456 - .456: Float", (Object) Float.class, subtract(123.912d, .456, Float.class).getClass()); assertEquals("123.456 - .456: Float", (Object) Float.class, subtract(123.456, .456d, Float.class).getClass()); for (Class<?> type : NUMBERS) { try { Object expected = null; Class<? extends Number> typeOfN = (Class<? extends Number>) type; Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); assertEquals("null: default: " + type.getSimpleName(), valueOf(null, typeOfN), subtract(null, null, typeOfN)); assertEquals("123.456: " + type.getSimpleName(), valueOf(123.912 - .456, typeOfN), subtract("123.912", .456, typeOfN)); assertEquals("123.456 - NaN: " + type.getSimpleName(), valueOf(123.456, typeOfN), subtract(123.456, Float.NaN, typeOfN)); assertEquals("NaN - 123.456: " + type.getSimpleName(), valueOf(123.456, typeOfN), subtract("NaN", 123.456, typeOfN)); assertEquals("invalid - 123.456: " + type.getSimpleName(), valueOf(123.456, typeOfN), subtract("not a number", 123.456, typeOfN)); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = valueOf(-Double.MAX_VALUE, typeOfN); } else if (ClassUtils.isPrimitiveOrWrapper(type)) { expected = wrapper.getField("MIN_VALUE").get(null); } else { expected = new BigDecimal("123.456").subtract(INFINITY_DOUBLE); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("123.456 - Infinity: " + type.getSimpleName(), expected, subtract(123.456, Double.POSITIVE_INFINITY, typeOfN)); if (ObjectUtils.isAny(wrapper, Float.class)) { expected = valueOf("Infinity", typeOfN); } else if (ClassUtils.isPrimitiveOrWrapper(type)) { expected = wrapper.getField("MAX_VALUE").get(null); } else { expected = INFINITY_DOUBLE.subtract(new BigDecimal("123.456")); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("Infinity - 123.456: " + type.getSimpleName(), expected, subtract("Infinity", 123.456, typeOfN)); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } } }
From source file:nl.knaw.huygens.timbuctoo.storage.graph.tinkerpop.conversion.property.PropertyConverterFactory.java
private boolean isSimpleValueType(Class<?> type) { return ClassUtils.isPrimitiveOrWrapper(type) || type == String.class; }