View Javadoc

1   /*
2    * Copyright 2005 Filipe Tavares
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  package org.devyant.decorutils.sdo;
17  
18  import java.text.Format;
19  import java.util.Comparator;
20  
21  /***
22   * Sortable Decorated Object.
23   * 
24   * @author Filipe Tavares
25   * @version @version $Revision: 1.1$ ($Author: ftavares$)
26   * @since Dec 8, 2004 7:29:42 PM
27   *
28   * @see java.lang.Comparable
29   * @see java.util.Comparator
30   */
31  public class SDO implements Displayable {
32      /***
33       * The <code>String</code> to output.
34       */
35      private transient String displayString;
36      /***
37       * The <code>Object</code> to be used in comparison.
38       */
39      private final transient Object object;
40      /***
41       * The comparator.
42       */
43      private transient Comparator comparator = null;
44  
45      /***
46       * Constructs a new, empty Sortable Decorated Object.
47       */
48      public SDO() {
49          object = "";
50          displayString = "";
51      }
52  
53      /***
54       * Constructs a new Sortable Decorated Object with an empty
55       * display <code>String</code>.
56       *
57       * @param object <code>Comparable</code> object for sorting purposes
58       */
59      public SDO(final Comparable object) {
60          this.object = object;
61          displayString = "";
62      }
63  
64      /***
65       * Constructs a new Sortable Decorated Object and creates display
66       * <code>String</code> using the specified formatter on the object.
67       *
68       * @param object    <code>Comparable</code> object for sorting purposes
69       * @param formatter output formmater
70       */
71      public SDO(final Comparable object, final Format formatter) {
72          this.object = object;
73          displayString = formatter.format(object);
74      }
75  
76      /***
77       * Constructs a new Sortable Decorated Object.
78       *
79       * @param object        <code>Comparable</code> object for sorting purposes
80       * @param displayString <code>String</code> to return on
81       * the <code>toString()</code> method
82       */
83      public SDO(final Comparable object, final String displayString) {
84          this.object = object;
85          this.displayString = displayString;
86      }
87  
88      /***
89       * Constructs a new Sortable Decorated Object and creates display
90       * <code>String</code> using the specified formatter on the object.
91       *
92       * @param comparator <code>Comparator</code> for sorting purposes
93       * @param object     decorated object
94       * @param formatter  output formmater
95       */
96      public SDO(final Comparator comparator,
97              final Object object, final Format formatter) {
98          this.object = object;
99          this.comparator = comparator;
100         this.displayString = formatter.format(object);
101     }
102 
103     /***
104      * Constructs a new Sortable Decorated Object.
105      *
106      * @param comparator    <code>Comparator</code> for sorting purposes
107      * @param object        decorated object
108      * @param displayString <code>String</code> to return
109      * on <code>toString()</code> method
110      */
111     public SDO(final Comparator comparator,
112             final Object object, final String displayString) {
113         this.object = object;
114         this.comparator = comparator;
115         this.displayString = displayString;
116     }
117 
118     /***
119      * This method overrides <code>Object</code> <code>toString</code>
120      * method returning the specified output <code>String</code>.
121      * 
122      * @return decorated output
123      */
124     public final String toString() {
125         return displayString;
126     }
127 
128     /***
129      * @see Comparable#compareTo(java.lang.Object)
130      */
131     public final int compareTo(final Object o) {
132         final SDO sdo = (SDO) o;
133         if (object instanceof Comparable) {
134             return ((Comparable) object).compareTo(sdo.getObject());
135         } else {
136             return comparator.compare(object, o);
137         }
138     }
139 
140     /***
141      * Set the output <code>String</code>.
142      *
143      * @param s output <code>String</code>
144      */
145     public final void setDisplayString(final String s) {
146         displayString = s;
147     }
148 
149     /***
150      * Returns the decorated object.
151      *
152      * @return the object
153      */
154     public final Object getObject() {
155         return object;
156     }
157 }