View Javadoc
1   /*
2   Copyright (c) 2013 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl;
18  
19  import java.nio.ByteBuffer;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.SystemUtils;
25  import org.apache.commons.lang.builder.StandardToStringStyle;
26  import org.apache.commons.lang.builder.ToStringBuilder;
27  
28  /**
29   * Custom ToStringStyle for use with ToStringBuilder.
30   *
31   * @author James Ahlborn
32   */
33  public class CustomToStringStyle extends StandardToStringStyle 
34  {
35    private static final long serialVersionUID = 0L;
36  
37    private static final String ML_FIELD_SEP = SystemUtils.LINE_SEPARATOR + "  ";
38    private static final String IMPL_SUFFIX = "Impl";
39    private static final int MAX_BYTE_DETAIL_LEN = 20;
40    
41    public static final CustomToStringStyle INSTANCE = new CustomToStringStyle() {
42      private static final long serialVersionUID = 0L;
43      {
44        setContentStart("[");
45        setFieldSeparator(ML_FIELD_SEP);
46        setFieldSeparatorAtStart(true);
47        setFieldNameValueSeparator(": ");
48        setArraySeparator("," + ML_FIELD_SEP);
49        setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
50        setUseShortClassName(true);
51      }
52    };
53  
54    public static final CustomToStringStyle VALUE_INSTANCE = new CustomToStringStyle() {
55      private static final long serialVersionUID = 0L;
56      {
57        setUseShortClassName(true);
58        setUseIdentityHashCode(false);
59      }
60    };
61  
62    private CustomToStringStyle() {    
63    }
64  
65    public static ToStringBuilder builder(Object obj) {
66      return new ToStringBuilder(obj, INSTANCE);
67    }
68  
69    public static ToStringBuilder valueBuilder(Object obj) {
70      return new ToStringBuilder(obj, VALUE_INSTANCE);
71    }
72  
73    @Override
74    protected void appendClassName(StringBuffer buffer, Object obj) {
75      if(obj instanceof String) {
76        // the caller gave an "explicit" class name
77        buffer.append(obj);
78      } else {
79        super.appendClassName(buffer, obj);
80      }
81    }
82  
83    @Override
84    protected String getShortClassName(Class clss) {
85      String shortName = super.getShortClassName(clss);
86      if(shortName.endsWith(IMPL_SUFFIX)) {
87        shortName = shortName.substring(0, 
88                                        shortName.length() - IMPL_SUFFIX.length());
89      }
90      int idx = shortName.lastIndexOf('.');
91      if(idx >= 0) {
92        shortName = shortName.substring(idx + 1);
93      }
94      return shortName;
95    }
96  
97    @Override
98    protected void appendDetail(StringBuffer buffer, String fieldName, 
99                                Object value) {
100     if(value instanceof ByteBuffer) {
101       appendDetail(buffer, (ByteBuffer)value);
102     } else {
103       buffer.append(indent(value));
104     }
105   }
106 
107   @Override
108   protected void appendDetail(StringBuffer buffer, String fieldName, 
109                               Collection value) {
110     buffer.append("[");
111 
112     // gather contents of list in a new StringBuffer
113     StringBuffer sb = new StringBuffer();
114     Iterator<?> iter = value.iterator();
115     if(iter.hasNext()) {
116       if(isFieldSeparatorAtStart()) {
117         appendFieldSeparator(sb);
118       }
119       appendValueDetail(sb, fieldName, iter.next());
120     }
121     while(iter.hasNext()) {
122       sb.append(getArraySeparator());
123       appendValueDetail(sb, fieldName, iter.next());
124     }
125 
126     // indent entire list contents another level
127     buffer.append(indent(sb));
128 
129     if(isFieldSeparatorAtStart()) {
130       appendFieldSeparator(buffer);
131     }
132     buffer.append("]");
133   }
134 
135 
136   @Override
137   protected void appendDetail(StringBuffer buffer, String fieldName,
138                               Map value) {
139     buffer.append("{");
140 
141     // gather contents of map in a new StringBuffer
142     StringBuffer sb = new StringBuffer();
143     @SuppressWarnings("unchecked")
144     Iterator<Map.Entry<?,?>> iter = value.entrySet().iterator();
145     if(iter.hasNext()) {
146       if(isFieldSeparatorAtStart()) {
147         appendFieldSeparator(sb);
148       }
149       Map.Entry<?,?> e = iter.next();
150       sb.append(e.getKey()).append("=");
151       appendValueDetail(sb, fieldName, e.getValue());
152     }
153     while(iter.hasNext()) {
154       sb.append(getArraySeparator());
155       Map.Entry<?,?> e = iter.next();
156       sb.append(e.getKey()).append("=");
157       appendValueDetail(sb, fieldName, e.getValue());
158     }
159 
160     // indent entire map contents another level
161     buffer.append(indent(sb));
162 
163     if(isFieldSeparatorAtStart()) {
164       appendFieldSeparator(buffer);
165     }
166     buffer.append("}");
167   }
168 
169   @Override
170   protected void appendDetail(StringBuffer buffer, String fieldName, 
171                               byte[] array) {
172     appendDetail(buffer, PageChannel.wrap(array));
173   }
174 
175   private void appendValueDetail(StringBuffer buffer, String fieldName, 
176                                  Object value) {
177     if (value == null) {
178       appendNullText(buffer, fieldName);
179     } else {
180       appendInternal(buffer, fieldName, value, true);
181     }    
182   }
183 
184   private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
185     int len = bb.remaining();
186     buffer.append("(").append(len).append(") ");
187     buffer.append(ByteUtil.toHexString(bb, bb.position(), 
188                                        Math.min(len, MAX_BYTE_DETAIL_LEN)));
189     if(len > MAX_BYTE_DETAIL_LEN) {
190       buffer.append(" ...");
191     }      
192   }
193 
194   private static String indent(Object obj) {
195     return ((obj != null) ? obj.toString().replaceAll(
196                 SystemUtils.LINE_SEPARATOR, ML_FIELD_SEP) : null);
197   }
198 }