一、前言
关于apache的commons-lang通过org.apache.commons.lang3.text.FormattableUtils格式化工具类对java.util.Formattable、java.util.Formatter功能进行增加和优化处理。
二、源码说明
1.源码中org.apache.commons.lang3.Validate分别参加其他更多文章
package org.apache.commons.lang3.text;@b@@b@import java.util.Formattable;@b@import java.util.Formatter;@b@import org.apache.commons.lang3.ObjectUtils;@b@import org.apache.commons.lang3.Validate;@b@@b@public class FormattableUtils@b@{@b@ private static final String SIMPLEST_FORMAT = "%s";@b@@b@ public static String toString(Formattable formattable)@b@ {@b@ return String.format("%s", new Object[] { formattable });@b@ }@b@@b@ public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, int precision)@b@ {@b@ return append(seq, formatter, flags, width, precision, ' ', null);@b@ }@b@@b@ public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, int precision, char padChar)@b@ {@b@ return append(seq, formatter, flags, width, precision, padChar, null);@b@ }@b@@b@ public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, int precision, CharSequence ellipsis)@b@ {@b@ return append(seq, formatter, flags, width, precision, ' ', ellipsis);@b@ }@b@@b@ public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, int precision, char padChar, CharSequence ellipsis)@b@ {@b@ Validate.isTrue(((ellipsis == null) || (precision < 0) || (ellipsis.length() <= precision)) ? 1 : false, "Specified ellipsis '%1$s' exceeds precision of %2$s", new Object[] { ellipsis, Integer.valueOf(precision) });@b@@b@ StringBuilder buf = new StringBuilder(seq);@b@ if ((precision >= 0) && (precision < seq.length())) {@b@ CharSequence _ellipsis = (CharSequence)ObjectUtils.defaultIfNull(ellipsis, "");@b@ buf.replace(precision - _ellipsis.length(), seq.length(), _ellipsis.toString());@b@ }@b@ boolean leftJustify = (flags & 0x1) == 1;@b@ for (int i = buf.length(); i < width; ++i)@b@ buf.insert((leftJustify) ? i : 0, padChar);@b@@b@ formatter.format(buf.toString(), new Object[0]);@b@ return formatter;@b@ }@b@}
2.ObjectUtils
package org.apache.commons.lang3;@b@@b@import java.io.IOException;@b@import java.io.Serializable;@b@import java.lang.reflect.Array;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.util.Collections;@b@import java.util.Comparator;@b@import java.util.HashMap;@b@import java.util.Map.Entry;@b@import java.util.TreeSet;@b@import org.apache.commons.lang3.exception.CloneFailedException;@b@import org.apache.commons.lang3.mutable.MutableInt;@b@import org.apache.commons.lang3.text.StrBuilder;@b@@b@public class ObjectUtils@b@{@b@ public static final Null NULL = new Null();@b@@b@ public static <T> T defaultIfNull(T object, T defaultValue)@b@ {@b@ return ((object != null) ? object : defaultValue);@b@ }@b@@b@ public static <T> T firstNonNull(T[] values)@b@ {@b@ Object[] arr$;@b@ int i$;@b@ if (values != null) {@b@ arr$ = values; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { Object val = arr$[i$];@b@ if (val != null)@b@ return val;@b@ }@b@ }@b@@b@ return null;@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static boolean equals(Object object1, Object object2)@b@ {@b@ if (object1 == object2)@b@ return true;@b@@b@ if ((object1 == null) || (object2 == null))@b@ return false;@b@@b@ return object1.equals(object2);@b@ }@b@@b@ public static boolean notEqual(Object object1, Object object2)@b@ {@b@ return (!(equals(object1, object2)));@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static int hashCode(Object obj)@b@ {@b@ return ((obj == null) ? 0 : obj.hashCode());@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static int hashCodeMulti(Object[] objects)@b@ {@b@ Object[] arr$;@b@ int i$;@b@ int hash = 1;@b@ if (objects != null) {@b@ arr$ = objects; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { Object object = arr$[i$];@b@ hash = hash * 31 + hashCode(object);@b@ }@b@ }@b@ return hash;@b@ }@b@@b@ public static String identityToString(Object object)@b@ {@b@ if (object == null)@b@ return null;@b@@b@ StringBuilder builder = new StringBuilder();@b@ identityToString(builder, object);@b@ return builder.toString();@b@ }@b@@b@ public static void identityToString(Appendable appendable, Object object)@b@ throws IOException@b@ {@b@ if (object == null)@b@ throw new NullPointerException("Cannot get the toString of a null identity");@b@@b@ appendable.append(object.getClass().getName()).append('@').append(Integer.toHexString(System.identityHashCode(object)));@b@ }@b@@b@ public static void identityToString(StrBuilder builder, Object object)@b@ {@b@ if (object == null)@b@ throw new NullPointerException("Cannot get the toString of a null identity");@b@@b@ builder.append(object.getClass().getName()).append('@').append(Integer.toHexString(System.identityHashCode(object)));@b@ }@b@@b@ public static void identityToString(StringBuffer buffer, Object object)@b@ {@b@ if (object == null)@b@ throw new NullPointerException("Cannot get the toString of a null identity");@b@@b@ buffer.append(object.getClass().getName()).append('@').append(Integer.toHexString(System.identityHashCode(object)));@b@ }@b@@b@ public static void identityToString(StringBuilder builder, Object object)@b@ {@b@ if (object == null)@b@ throw new NullPointerException("Cannot get the toString of a null identity");@b@@b@ builder.append(object.getClass().getName()).append('@').append(Integer.toHexString(System.identityHashCode(object)));@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static String toString(Object obj)@b@ {@b@ return ((obj == null) ? "" : obj.toString());@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static String toString(Object obj, String nullStr)@b@ {@b@ return ((obj == null) ? nullStr : obj.toString());@b@ }@b@@b@ public static <T extends Comparable<? super T>> T min(T[] values)@b@ {@b@ Comparable[] arr$;@b@ int i$;@b@ Comparable result = null;@b@ if (values != null) {@b@ arr$ = values; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { Comparable value = arr$[i$];@b@ if (compare(value, result, true) < 0)@b@ result = value;@b@ }@b@ }@b@@b@ return result;@b@ }@b@@b@ public static <T extends Comparable<? super T>> T max(T[] values)@b@ {@b@ Comparable[] arr$;@b@ int i$;@b@ Comparable result = null;@b@ if (values != null) {@b@ arr$ = values; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { Comparable value = arr$[i$];@b@ if (compare(value, result, false) > 0)@b@ result = value;@b@ }@b@ }@b@@b@ return result;@b@ }@b@@b@ public static <T extends Comparable<? super T>> int compare(T c1, T c2)@b@ {@b@ return compare(c1, c2, false);@b@ }@b@@b@ public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean nullGreater)@b@ {@b@ if (c1 == c2)@b@ return 0;@b@ if (c1 == null)@b@ return ((nullGreater) ? 1 : -1);@b@ if (c2 == null)@b@ return ((nullGreater) ? -1 : 1);@b@@b@ return c1.compareTo(c2);@b@ }@b@@b@ public static <T extends Comparable<? super T>> T median(T[] items)@b@ {@b@ Validate.notEmpty(items);@b@ Validate.noNullElements(items);@b@ TreeSet sort = new TreeSet();@b@ Collections.addAll(sort, items);@b@@b@ Comparable result = (Comparable)sort.toArray()[((sort.size() - 1) / 2)];@b@ return result;@b@ }@b@@b@ public static <T> T median(Comparator<T> comparator, T[] items)@b@ {@b@ Validate.notEmpty(items, "null/empty items", new Object[0]);@b@ Validate.noNullElements(items);@b@ Validate.notNull(comparator, "null comparator", new Object[0]);@b@ TreeSet sort = new TreeSet(comparator);@b@ Collections.addAll(sort, items);@b@@b@ Object result = sort.toArray()[((sort.size() - 1) / 2)];@b@ return result;@b@ }@b@@b@ public static <T> T mode(T[] items)@b@ {@b@ if (ArrayUtils.isNotEmpty(items)) {@b@ HashMap occurrences = new HashMap(items.length);@b@ Object[] arr$ = items; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { Object t = arr$[i$];@b@ MutableInt count = (MutableInt)occurrences.get(t);@b@ if (count == null)@b@ occurrences.put(t, new MutableInt(1));@b@ else@b@ count.increment();@b@ }@b@@b@ Object result = null;@b@ int max = 0;@b@ for (Map.Entry e : occurrences.entrySet()) {@b@ int cmp = ((MutableInt)e.getValue()).intValue();@b@ if (cmp == max) {@b@ result = null;@b@ } else if (cmp > max) {@b@ max = cmp;@b@ result = e.getKey();@b@ }@b@ }@b@ return result;@b@ }@b@ return null;@b@ }@b@@b@ public static <T> T clone(T obj)@b@ {@b@ if (obj instanceof Cloneable)@b@ {@b@ Object result;@b@ if (obj.getClass().isArray()) {@b@ Class componentType = obj.getClass().getComponentType();@b@ if (!(componentType.isPrimitive())) {@b@ result = ((Object[])(Object[])obj).clone();@b@ } else {@b@ int length = Array.getLength(obj);@b@ result = Array.newInstance(componentType, length);@b@ while (length-- > 0)@b@ Array.set(result, length, Array.get(obj, length));@b@ }@b@ }@b@ else {@b@ try {@b@ Method clone = obj.getClass().getMethod("clone", new Class[0]);@b@ result = clone.invoke(obj, new Object[0]);@b@ } catch (NoSuchMethodException e) {@b@ throw new CloneFailedException("Cloneable type " + obj.getClass().getName() + " has no clone method", e);@b@ }@b@ catch (IllegalAccessException e)@b@ {@b@ throw new CloneFailedException("Cannot clone Cloneable type " + obj.getClass().getName(), e);@b@ }@b@ catch (InvocationTargetException e) {@b@ throw new CloneFailedException("Exception cloning Cloneable type " + obj.getClass().getName(), e.getCause());@b@ }@b@@b@ }@b@@b@ Object checked = result;@b@ return checked;@b@ }@b@@b@ return null;@b@ }@b@@b@ public static <T> T cloneIfPossible(T obj)@b@ {@b@ Object clone = clone(obj);@b@ return ((clone == null) ? obj : clone);@b@ }@b@@b@ public static boolean CONST(boolean v)@b@ {@b@ return v;@b@ }@b@@b@ public static byte CONST(byte v)@b@ {@b@ return v;@b@ }@b@@b@ public static byte CONST_BYTE(int v)@b@ throws IllegalArgumentException@b@ {@b@ if ((v < -128) || (v > 127))@b@ throw new IllegalArgumentException("Supplied value must be a valid byte literal between -128 and 127: [" + v + "]");@b@@b@ return (byte)v;@b@ }@b@@b@ public static char CONST(char v)@b@ {@b@ return v;@b@ }@b@@b@ public static short CONST(short v)@b@ {@b@ return v;@b@ }@b@@b@ public static short CONST_SHORT(int v)@b@ throws IllegalArgumentException@b@ {@b@ if ((v < -32768) || (v > 32767))@b@ throw new IllegalArgumentException("Supplied value must be a valid byte literal between -32768 and 32767: [" + v + "]");@b@@b@ return (short)v;@b@ }@b@@b@ public static int CONST(int v)@b@ {@b@ return v;@b@ }@b@@b@ public static long CONST(long v)@b@ {@b@ return v;@b@ }@b@@b@ public static float CONST(float v)@b@ {@b@ return v;@b@ }@b@@b@ public static double CONST(double v)@b@ {@b@ return v;@b@ }@b@@b@ public static <T> T CONST(T v)@b@ {@b@ return v;@b@ }@b@@b@ public static class Null@b@ implements Serializable@b@ {@b@ private static final long serialVersionUID = 7092611880189329093L;@b@@b@ private Object readResolve()@b@ {@b@ return ObjectUtils.NULL;@b@ }@b@ }@b@}