View Javadoc
1   package org.pojomatic.formatter;
2   
3   import java.lang.reflect.AnnotatedElement;
4   import java.util.Arrays;
5   
6   /**
7    * The default property formatter used by Pojomatic.  While the particulars of the formatting
8    * strategy are subject to change, the general principle is to provide a meaningful representation.
9    * In particular, arrays are formatted "deeply", rather than simply showing the default toString
10   * representation of Java arrays.
11   *
12   * @deprecated Since 2.0. Use {@link DefaultEnhancedPropertyFormatter} instead.
13   */
14  @Deprecated
15  public class DefaultPropertyFormatter implements PropertyFormatter {
16    //FIXME - this currently prevents formatter reusability, and for very little benefit. Perhaps an initializable annotation?
17    @Override
18    public void initialize(AnnotatedElement element) {
19      //Not applicable
20    }
21  
22    @Override
23    public String format(Object value) {
24      if (value == null) {
25        return "null";
26      }
27      else if (value.getClass().isArray()) {
28        Class<?> componentClass = value.getClass().getComponentType();
29        if (componentClass.isPrimitive()) {
30          if (Boolean.TYPE == componentClass) {
31            return Arrays.toString((boolean[]) value);
32          }
33          if (Character.TYPE == componentClass) {
34            StringBuilder builder = new StringBuilder().append('[');
35            boolean seenOne = false;
36            for (char c: ((char[]) value)) {
37              if(seenOne) {
38                builder.append(", ");
39              }
40              else {
41                seenOne = true;
42              }
43              builder.append('\'');
44              if (Character.isISOControl(c)) {
45                builder.append("0x").append(Integer.toHexString(c));
46              }
47              else {
48                builder.append(c);
49              }
50              builder.append('\'');
51            }
52            return builder.append(']').toString();
53          }
54          if (Byte.TYPE == componentClass) {
55            return Arrays.toString((byte[]) value);
56          }
57          if (Short.TYPE == componentClass) {
58            return Arrays.toString((short[]) value);
59          }
60          if (Integer.TYPE == componentClass) {
61            return Arrays.toString((int[]) value);
62          }
63          if (Long.TYPE == componentClass) {
64            return Arrays.toString((long[]) value);
65          }
66          if (Float.TYPE == componentClass) {
67            return Arrays.toString((float[]) value);
68          }
69          if (Double.TYPE == componentClass) {
70            return Arrays.toString((double[]) value);
71          }
72          else {
73            throw new IllegalStateException("unexpected primitive array base type: " + componentClass);
74          }
75        }
76        else {
77          return Arrays.deepToString((Object[]) value);
78        }
79      }
80      else {
81        return value.toString();
82      }
83    }
84  
85  }