View Javadoc

1   /*
2    * Copyright (C) 2004 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
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.3 $, $Date: 2004/12/06 09:21:18 $, $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          // create builder for this object
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          // append all fields in iteration order
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          // delegate to static method
94          return toString(this);
95      }
96  
97  }