1
2
3 package com.pnpconsult.zeiterfassung.util;
4
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Date;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.Map;
11
12 import org.apache.commons.lang.StringUtils;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 /***
17 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
18 * @version $Id: Description.java,v 1.4 2004/06/23 21:55:12 powerpete Exp $
19 */
20 public class Description implements Comparable
21 {
22 private static final Log LOG = LogFactory.getLog(Description.class);
23 private Collection content = new ArrayList();
24 private StringBuffer sbuf = new StringBuffer();
25 private Map formatterMap = new HashMap();
26
27 public Description()
28 {
29 setFormatter(Date.class, new Formatter()
30 {
31 public String format(Object obj)
32 {
33 return DateUtils.format((Date) obj);
34 }
35 });
36 setFormatter(Float.class, new Formatter()
37 {
38 public String format(Object obj)
39 {
40 return NumberUtils.formatLong(((Float) obj).doubleValue());
41 }
42 });
43 }
44
45 public void setFormatter(Class clazz, Formatter formatter)
46 {
47 formatterMap.put(clazz, formatter);
48 }
49
50 private Formatter getFormatter(Class clazz)
51 {
52 return (Formatter) formatterMap.get(clazz);
53 }
54
55 public Description append(Object obj)
56 {
57 return append(null, null, obj, null);
58 }
59
60 public Description append(
61 Object optionalDecoration,
62 Object decorationPrefix,
63 Object obj,
64 Object decorationSuffix)
65 {
66 String objStr = format(obj);
67 if (StringUtils.isBlank(objStr))
68 {
69 return this;
70 }
71 appendDecoration(optionalDecoration, sbuf.length() != 0);
72 appendDecoration(decorationPrefix, true);
73 sbuf.append(objStr);
74 content.add(obj);
75 appendDecoration(decorationSuffix, true);
76 return this;
77 }
78
79 private void appendDecoration(Object obj, boolean shouldAppend)
80 {
81 String str = format(obj);
82 if (StringUtils.isNotEmpty(str) && shouldAppend)
83 {
84 sbuf.append(str);
85 }
86 }
87
88 private String format(Object obj)
89 {
90 if (obj == null)
91 {
92 return null;
93 }
94 if (obj instanceof Date)
95 {
96 return getFormatter(Date.class).format(obj);
97 }
98 else if (obj instanceof Float)
99 {
100 return getFormatter(Float.class).format(obj);
101 }
102 else
103 {
104 return obj.toString();
105 }
106 }
107
108 /***
109 * @see java.lang.Comparable#compareTo(java.lang.Object)
110 */
111 public int compareTo(Object o)
112 {
113 Description d = (Description) o;
114 int result = 0;
115 for (Iterator it1 = content.iterator(), it2 = d.content.iterator(); it1
116 .hasNext()
117 && it2.hasNext();)
118 {
119 result = safeCompareTo(it1.next(), it2.next());
120 if (result != 0)
121 {
122 return result;
123 }
124 }
125 return content.size() - d.content.size();
126 }
127
128 public String toString()
129 {
130 return sbuf.toString();
131 }
132
133 private int safeCompareTo(Object o1, Object o2)
134 {
135 if (o1 == null)
136 {
137 if (o2 == null)
138 {
139 return 0;
140 }
141 return 1;
142 }
143 if (o2 == null)
144 {
145 return -1;
146 }
147 if (o1 instanceof Comparable)
148 {
149
150
151
152
153
154 if (o1.getClass() == o2.getClass())
155 {
156 return ((Comparable) o1).compareTo(o2);
157 }
158 else
159 {
160 LOG.warn("Different classes in collection: "
161 + o1.getClass()
162 + " != "
163 + o2.getClass());
164 }
165 }
166 return o1.toString().compareTo(o2.toString());
167 }
168
169 public interface Formatter
170 {
171 public String format(Object obj);
172 }
173 }