📄 objects.java
字号:
return Math.max(t1, t2); } } /** * Returns a constant from the NumericTypes interface that represents the numeric type of the * given object. * * @param value * an object that needs to be interpreted as a number * @return the appropriate constant from the NumericTypes interface */ public static int getNumericType(Object value) { if (value != null) { Class c = value.getClass(); if (c == Integer.class) { return INT; } if (c == Double.class) { return DOUBLE; } if (c == Boolean.class) { return BOOL; } if (c == Byte.class) { return BYTE; } if (c == Character.class) { return CHAR; } if (c == Short.class) { return SHORT; } if (c == Long.class) { return LONG; } if (c == Float.class) { return FLOAT; } if (c == BigInteger.class) { return BIGINT; } if (c == BigDecimal.class) { return BIGDEC; } } return NONNUMERIC; } /** * Returns the constant from the NumericTypes interface that best expresses the type of a * numeric operation on the two given objects. * * @param v1 * one argument to a numeric operator * @param v2 * the other argument * @return the appropriate constant from the NumericTypes interface */ public static int getNumericType(Object v1, Object v2) { return getNumericType(v1, v2, false); } /** * Returns the constant from the NumericTypes interface that best expresses the type of an * operation, which can be either numeric or not, on the two given objects. * * @param v1 * one argument to an operator * @param v2 * the other argument * @param canBeNonNumeric * whether the operator can be interpreted as non-numeric * @return the appropriate constant from the NumericTypes interface */ public static int getNumericType(Object v1, Object v2, boolean canBeNonNumeric) { return getNumericType(getNumericType(v1), getNumericType(v2), canBeNonNumeric); } /** * Returns true if object1 is equal to object2 in either the sense that they are the same object * or, if both are non-null if they are equal in the <CODE>equals()</CODE> sense. * * @param object1 * First object to compare * @param object2 * Second object to compare * * @return true if v1 == v2 */ public static boolean isEqual(Object object1, Object object2) { boolean result = false; if (object1 == object2) { result = true; } else { if ((object1 != null) && object1.getClass().isArray()) { if ((object2 != null) && object2.getClass().isArray() && (object2.getClass() == object1.getClass())) { result = (Array.getLength(object1) == Array.getLength(object2)); if (result) { for (int i = 0, icount = Array.getLength(object1); result && (i < icount); i++) { result = isEqual(Array.get(object1, i), Array.get(object2, i)); } } } } else { // Check for converted equivalence first, then equals() // equivalence result = (object1 != null) && (object2 != null) && ((compareWithConversion(object1, object2) == 0) || object1.equals(object2)); } } return result; } /** * Evaluates the given object as a long integer. * * @param value * an object to interpret as a long integer * @return the long integer value implied by the given object * @throws NumberFormatException * if the given object can't be understood as a long integer */ public static long longValue(Object value) throws NumberFormatException { if (value == null) { return 0L; } Class c = value.getClass(); if (c.getSuperclass() == Number.class) { return ((Number)value).longValue(); } if (c == Boolean.class) { return ((Boolean)value).booleanValue() ? 1 : 0; } if (c == Character.class) { return ((Character)value).charValue(); } return Long.parseLong(stringValue(value, true)); } /** * Creates a new instance using the current application's class resolver. Returns null if * className is null. * * @param className * The full class name * @return The new object instance */ public static Object newInstance(final String className) { if (!Strings.isEmpty(className)) { try { Class c = Classes.resolveClass(className); if (c == null) { throw new WicketRuntimeException("Unable to create " + className); } return c.newInstance(); } catch (ClassCastException e) { throw new WicketRuntimeException("Unable to create " + className, e); } catch (InstantiationException e) { throw new WicketRuntimeException("Unable to create " + className, e); } catch (IllegalAccessException e) { throw new WicketRuntimeException("Unable to create " + className, e); } } return null; } /** * Returns a new Number object of an appropriate type to hold the given integer value. The type * of the returned object is consistent with the given type argument, which is a constant from * the NumericTypes interface. * * @param type * the nominal numeric type of the result, a constant from the NumericTypes interface * @param value * the integer value to convert to a Number object * @return a Number object with the given value, of type implied by the type argument */ public static Number newInteger(int type, long value) { switch (type) { case BOOL : case CHAR : case INT : return new Integer((int)value); case FLOAT : if (value == value) { return new Float(value); } // else fall through: case DOUBLE : if (value == value) { return new Double(value); } // else fall through: case LONG : return new Long(value); case BYTE : return new Byte((byte)value); case SHORT : return new Short((short)value); default : return BigInteger.valueOf(value); } } /** * Serializes an object into a byte array. * * @param object * The object * @return The serialized object */ public static byte[] objectToByteArray(final Object object) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { objectStreamFactory.newObjectOutputStream(out).writeObject(object); } finally { out.close(); } return out.toByteArray(); } catch (Exception e) { log.error("Error serializing object " + object.getClass() + " [object=" + object + "]", e); } return null; } /** * Sets the strategy for determining the sizes of objects. * * @param objectSizeOfStrategy * the strategy. Pass null to reset to the default. */ public static void setObjectSizeOfStrategy(IObjectSizeOfStrategy objectSizeOfStrategy) { if (objectSizeOfStrategy == null) { Objects.objectSizeOfStrategy = new SerializingObjectSizeOfStrategy(); } else { Objects.objectSizeOfStrategy = objectSizeOfStrategy; } log.info("using " + objectSizeOfStrategy + " for calculating object sizes"); } /** * Configure this utility class to use the provided {@link IObjectStreamFactory} instance. * * @param objectStreamFactory * The factory instance to use. If you pass in null, the * {@link DefaultObjectStreamFactory default} will be set (again). Pass null to reset * to the default. */ public static void setObjectStreamFactory(IObjectStreamFactory objectStreamFactory) { if (objectStreamFactory == null) { Objects.objectStreamFactory = new IObjectStreamFactory.DefaultObjectStreamFactory(); } else { Objects.objectStreamFactory = objectStreamFactory; } log.info("using " + Objects.objectStreamFactory + " for creating object streams"); } /** * Computes the size of an object. Note that this is an estimation, never an absolute accurate * size. * * @param object * Object to compute size of * @return The size of the object in bytes */ public static long sizeof(final Object object) { return objectSizeOfStrategy.sizeOf(object); } /** * Evaluates the given object as a String. * * @param value * an object to interpret as a String * @return the String value implied by the given object as returned by the toString() method, or * "null" if the object is null. */ public static String stringValue(Object value) { return stringValue(value, false); } /** * returns hashcode of the objects by calling obj.hashcode(). safe to use when obj is null. * * @param obj * @return hashcode of the object or 0 if obj is null */ // TODO when on Java 5, we can use Object... obj public static int hashCode(final Object[] obj) { if (obj == null || obj.length == 0) { return 0; } int result = 37; for (int i = obj.length - 1; i > -1; i--) { result = 37 * result + (obj[i] != null ? obj[i].hashCode() : 0); } return result; } /** * Evaluates the given object as a String and trims it if the trim flag is true. * * @param value * an object to interpret as a String * @param trim * whether to trim the string * @return the String value implied by the given object as returned by the toString() method, or * "null" if the object is null. */ public static String stringValue(Object value, boolean trim) { String result; if (value == null) { result = "null"; } else { result = value.toString(); if (trim) { result = result.trim(); } } return result; } /** * Instantiation not allowed */ private Objects() { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -