1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package de.fu_berlin.ties.io;
23
24 import java.util.Iterator;
25
26 import org.apache.commons.lang.builder.ToStringBuilder;
27
28 /***
29 * Convenience implementations of the {@link de.fu_berlin.ties.io.Storable}
30 * interface that implements the {@link #toString()} method based on the field
31 * map created by {@link de.fu_berlin.ties.io.Storable#storeFields()}.
32 *
33 * <p>Classes that cannot extend this interface can easily implement this method
34 * by delegating to the static {@link #toString(Storable)} method provided by
35 * this class, passing itself as argument and returning the result.
36 *
37 * <p>The {@link java.lang.Object#equals(java.lang.Object)} and
38 * {@link java.lang.Object#hashCode()} methods are <em>not</em> overwritten by
39 * this class because it would not only be inefficent, but also error-prone
40 * to do this based on the field map (e.g. a number could by represented either
41 * by a {@link java.lang.Number} object or by a string representation, thus
42 * classes might be equal even if their field map serializations are not).
43 * Non-abstract {@link de.fu_berlin.ties.io.Storable} implementations should
44 * provide their own implementations of these methods, e.g. using the builder
45 * tools from <a href="http://jakarta.apache.org/commons/lang/">Jakarta
46 * Commons Lang</a>.
47 *
48 * @author Christian Siefkes
49 * @version $Revision: 1.6 $, $Date: 2006/10/21 16:04:21 $, $Author: siefkes $
50 */
51 public abstract class BaseStorable implements Storable {
52
53 /***
54 * Returns a String representation of a {@link Storable} object,
55 * printing all field name/value pairs in the order used to insert them
56 * into the {@link FieldMap}.
57 *
58 * @param storable the storable to represent
59 * @return a string representation of the storable
60 */
61 public static String toString(final Storable storable) {
62
63 final ToStringBuilder builder = new ToStringBuilder(storable);
64 final FieldMap fields = storable.storeFields();
65 final Iterator fieldIter = fields.keySet().iterator();
66 Object currentKey;
67 Object currentValue;
68
69
70 while (fieldIter.hasNext()) {
71 currentKey = fieldIter.next();
72 currentValue = fields.get(currentKey);
73 builder.append(currentKey.toString(), currentValue);
74 }
75 return builder.toString();
76 }
77
78 /***
79 * Creates a new instance.
80 */
81 protected BaseStorable() {
82 super();
83 }
84
85 /***
86 * Returns a String representation of this object,
87 * printing all field name/value pairs in the order used to insert them
88 * into the {@link FieldMap}.
89 *
90 * @return a string representation of this object
91 */
92 public String toString() {
93
94 return toString(this);
95 }
96
97 }