1 16 17 package org.springframework.core.style; 18 19 import org.springframework.util.Assert; 20 21 30 public class ToStringCreator { 31 32 35 private static final ToStringStyler DEFAULT_TO_STRING_STYLER = 36 new DefaultToStringStyler(StylerUtils.DEFAULT_VALUE_STYLER); 37 38 39 private StringBuffer buffer = new StringBuffer (512); 40 41 private ToStringStyler styler; 42 43 private Object object; 44 45 private boolean styledFirstField; 46 47 48 52 public ToStringCreator(Object obj) { 53 this(obj, (ToStringStyler) null); 54 } 55 56 61 public ToStringCreator(Object obj, ValueStyler styler) { 62 this(obj, new DefaultToStringStyler(styler != null ? styler : StylerUtils.DEFAULT_VALUE_STYLER)); 63 } 64 65 70 public ToStringCreator(Object obj, ToStringStyler styler) { 71 Assert.notNull(obj, "The object to be styled must not be null"); 72 this.object = obj; 73 this.styler = (styler != null ? styler : DEFAULT_TO_STRING_STYLER); 74 this.styler.styleStart(this.buffer, this.object); 75 } 76 77 78 84 public ToStringCreator append(String fieldName, byte value) { 85 return append(fieldName, new Byte (value)); 86 } 87 88 94 public ToStringCreator append(String fieldName, short value) { 95 return append(fieldName, new Short (value)); 96 } 97 98 104 public ToStringCreator append(String fieldName, int value) { 105 return append(fieldName, new Integer (value)); 106 } 107 108 114 public ToStringCreator append(String fieldName, float value) { 115 return append(fieldName, new Float (value)); 116 } 117 118 124 public ToStringCreator append(String fieldName, double value) { 125 return append(fieldName, new Double (value)); 126 } 127 128 134 public ToStringCreator append(String fieldName, long value) { 135 return append(fieldName, new Long (value)); 136 } 137 138 144 public ToStringCreator append(String fieldName, boolean value) { 145 return append(fieldName, value ? Boolean.TRUE : Boolean.FALSE); 146 } 147 148 154 public ToStringCreator append(String fieldName, Object value) { 155 printFieldSeparatorIfNecessary(); 156 this.styler.styleField(this.buffer, fieldName, value); 157 return this; 158 } 159 160 private void printFieldSeparatorIfNecessary() { 161 if (this.styledFirstField) { 162 this.styler.styleFieldSeparator(this.buffer); 163 } 164 else { 165 this.styledFirstField = true; 166 } 167 } 168 169 174 public ToStringCreator append(Object value) { 175 this.styler.styleValue(this.buffer, value); 176 return this; 177 } 178 179 180 183 public String toString() { 184 this.styler.styleEnd(this.buffer, this.object); 185 return this.buffer.toString(); 186 } 187 188 } 189 | Popular Tags |