001 package com.sptci.echo2;
002
003 import java.io.Serializable;
004 import java.util.Formatter;
005
006 /**
007 * A simple bean that represents an item that can be added to a
008 * <code>ListModel</code>.
009 *
010 * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
011 * @author Rakesh Vidyadharan 2006-01-31
012 * @version $Id: ListItem.java,v 1.3 2006/02/09 16:15:37 rakesh Exp $
013 */
014 public class ListItem
015 implements Serializable, Comparable<ListItem>, Cloneable
016 {
017 /**
018 * The item or value stored in the list.
019 */
020 protected String value;
021
022 /**
023 * A flag indicating whether the {@link #value} is selected or not.
024 */
025 protected boolean selected;
026
027 /**
028 * Default constructor. Cannot be initialised.
029 */
030 protected ListItem() {}
031
032 /**
033 * Create a new instance of the bean using the specified object.
034 * Set the {@link #selected} to <code>false</code> for the
035 * specified object.
036 *
037 * @param value The {@link #value} to set.
038 */
039 public ListItem( String value )
040 {
041 this( value, false );
042 }
043
044 /**
045 * Designated constructor. Create a new bean using the specified
046 * values.
047 *
048 * @param value The {@link #value} to set.
049 * @param selected The {@link #selected} value to set.
050 */
051 public ListItem( String value, boolean selected )
052 {
053 this();
054 setValue( value );
055 setSelected( selected );
056 }
057
058 /**
059 * Return a string representation of this object.
060 *
061 * @return String The string representation of the fields of the
062 * object.
063 */
064 @Override
065 public String toString()
066 {
067 StringBuilder builder = new StringBuilder( 256 );
068 Formatter formatter = new Formatter( builder );
069 formatter.format( "%s value: {%s} selected: {%s}%n",
070 getClass().getName(), value, selected );
071 return builder.toString();
072 }
073
074 /**
075 * Check the specified object with this object for equality.
076 *
077 * @param object The object that is to be checked for equality with.
078 * @return boolean Returns <code>true</code> if the object is of the
079 * same type as this object and has the same field values.
080 */
081 @Override
082 public boolean equals( Object object )
083 {
084 if ( this == object ) return true;
085
086 boolean result = false;
087 if ( object != null && object.getClass() == getClass() )
088 {
089 ListItem item = (ListItem) object;
090 result = ( selected == item.selected );
091 result = result && ( ( value == item.value ) ||
092 ( ( value != null ) && value.equals( item.value ) ) );
093 }
094
095 return result;
096 }
097
098 /**
099 * Return a hash code value for this object.
100 *
101 * @return int The hash code value.
102 */
103 @Override
104 public int hashCode()
105 {
106 int hash = 7;
107 hash = ( 31 * hash ) + ( ( value == null ) ? 0 : value.hashCode() );
108 hash = ( 31 * hash ) + ( ( selected ) ? 1 : 0 );
109 return hash;
110 }
111
112 /**
113 * Implementation of the <code>Comparable</code> interface.
114 * Compares this object with the specified object for order. Returns
115 * a negative integer, zero, or a positive integer as this object is
116 * less than, equal to, or greater than the specified object.
117 * The comparison is done by comparing the {@link #hashCode()} values
118 * of the objects.
119 *
120 * @param item - The object with which this class is to
121 * be compared.
122 * @return int - A negative integer, zero, or a positive integer as
123 * this object is less than, equal to, or greater than the
124 * specified object.
125 */
126 public int compareTo( ListItem item )
127 {
128 return ( this.hashCode() - item.hashCode() );
129 }
130
131 /**
132 * Creates and returns a copy of this object. Implementation of
133 * the <code>Cloneable</code> interface. No special actions are
134 * performed. This method simply allows public access to the
135 * <code>Object.clone</code> method.
136 *
137 * @return Object A clone of this instance.
138 * @throws CloneNotSupportedException If the super-class
139 * implementation throws an error.
140 */
141 @Override
142 public Object clone() throws CloneNotSupportedException
143 {
144 return super.clone();
145 }
146
147 /**
148 * Returns {@link #value}.
149 *
150 * @return String The value/reference of/to value.
151 */
152 public final String getValue()
153 {
154 return value;
155 }
156
157 /**
158 * Set {@link #value}.
159 *
160 * @param value The value to set.
161 */
162 public void setValue( String value )
163 {
164 this.value = value;
165 }
166
167 /**
168 * Returns {@link #selected}.
169 *
170 * @return boolean The value/reference of/to selected.
171 */
172 public final boolean getSelected()
173 {
174 return selected;
175 }
176
177 /**
178 * Returns the status of {@link #selected}.
179 *
180 * @return boolean The value/reference of/to selected.
181 */
182 public final boolean isSelected()
183 {
184 return getSelected();
185 }
186
187 /**
188 * Set {@link #selected}.
189 *
190 * @param selected The value to set.
191 */
192 public void setSelected( boolean selected )
193 {
194 this.selected = selected;
195 }
196 }