View Javadoc
1   package org.pojomatic.formatter;
2   
3   import org.pojomatic.Pojomatic;
4   import org.pojomatic.PropertyElement;
5   
6   /**
7    * Default formatter for classes that use {@link Pojomatic}.  This implementation first presents
8    * the class name, and then each property in turn, separated by commas, using braces to indicate
9    * nesting.
10   * <p>
11   * For example, if a class Person has two properties, firstName and LastName, and these properties
12   * are using {@link DefaultPropertyFormatter}, then the Person
13   * instance representing Joe Blow would be represented as
14   * <code>"Person{firstName: {Joe}, lastName: {Blow}}"</code>
15   *
16   * @since 2.0
17   */
18  public class DefaultEnhancedPojoFormatter implements EnhancedPojoFormatter {
19    private boolean firstPropertyPrinted = false;
20  
21    @Override
22    public final String getPropertyPrefix(PropertyElement property) {
23      StringBuilder builder = new StringBuilder();
24      appendPropertyPrefix(builder, property);
25      return builder.toString();
26    }
27  
28    @Override
29    public final String getPropertySuffix(PropertyElement property) {
30      StringBuilder builder = new StringBuilder();
31      appendPropertySuffix(builder, property);
32      return builder.toString();
33    }
34  
35    @Override
36    public final String getToStringPrefix(Class<?> pojoClass) {
37      StringBuilder builder = new StringBuilder();
38      appendToStringPrefix(builder, pojoClass);
39      return builder.toString();
40    }
41  
42    @Override
43    public final String getToStringSuffix(Class<?> pojoClass) {
44      StringBuilder builder = new StringBuilder();
45      appendToStringSuffix(builder, pojoClass);
46      return builder.toString();
47    }
48  
49    @Override
50    public void appendToStringPrefix(StringBuilder builder, Class<?> pojoClass) {
51      builder.append(pojoClass.getSimpleName()).append('{');
52    }
53  
54    @Override
55    public void appendToStringSuffix(StringBuilder builder, Class<?> pojoClass) {
56      builder.append('}');
57    }
58  
59    @Override
60    public void appendPropertyPrefix(StringBuilder builder, PropertyElement property) {
61      if (firstPropertyPrinted) {
62        builder.append(", ");
63      }
64      else {
65        firstPropertyPrinted = true;
66      }
67      builder.append(property.getName()).append(": {");
68    }
69  
70    @Override
71    public void appendPropertySuffix(StringBuilder builder, PropertyElement property) {
72      builder.append('}');
73    }
74  }