KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > listOfValues > lovValue > DefaultLovValue


1 /**
2  * Copyright (c) 2002 Bright Side Factory. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * 3. The end-user documentation included with the redistribution,
17  * if any, must include the following acknowledgment:
18  * "This product includes software developed by the
19  * Bright Side Factory (http://www.bs-factory.org/)."
20  * Alternately, this acknowledgment may appear in the software itself,
21  * if and wherever such third-party acknowledgments normally appear.
22  *
23  * 4. The names "Bright Side", "BS Factory" and "Bright Side Factory" must
24  * not be used to endorse or promote products derived from this
25  * software without prior written permission. For written
26  * permission, please contact info@bs-factory.org.
27  *
28  * 5. Products derived from this software may not be called "Bright Side",
29  * nor may "Bright Side" appear in their name, without prior written
30  * permission of the Apache Software Foundation.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of the Bright Side Factory. For more
48  * information on the Bright Side Factory, please see
49  * <http://www.bs-factory.org/>.
50  */

51
52 package org.bsf.listOfValues.lovValue;
53
54 import java.util.HashMap JavaDoc;
55 import java.util.Iterator JavaDoc;
56
57 /**
58  * A default implementation of the LovValue interface. It represents a row of
59  * a list of values.
60  *
61  * @see LovValue
62  */

63 public class DefaultLovValue implements LovValue {
64     public static final String JavaDoc FIELD_SEPARATOR = " , ";
65
66     protected Object JavaDoc _pk;
67     protected HashMap JavaDoc _fields;
68
69     // *************************************************************************
70
// *************************** Constructor *********************************
71
// *************************************************************************
72

73     public DefaultLovValue( Object JavaDoc p_pk ) {
74         _pk = p_pk;
75         _fields = new HashMap JavaDoc();
76     }
77
78     // *************************************************************************
79
// ************************** LovValue methods *****************************
80
// *************************************************************************
81

82     /**
83      * This method is deprecated but should keep working as long as the first
84      * column returned is a Long.
85      *
86      * @return The oid of this particular LovValue.
87      *
88      * @see LovValue
89      * @see #getPK()
90      *
91      * @deprecated should use getPK()
92      */

93     public Long JavaDoc getOid() {
94         return (Long JavaDoc) _pk;
95     }
96
97     /**
98      * This method should be use instead of the old getOid() method.
99      *
100      * @return The pk of this particular LovValue.
101      *
102      * @see LovValue
103      * @see org.bsf.listOfValues.LovBean
104      */

105     public Object JavaDoc getPK() {
106         return _pk;
107     }
108
109     /**
110      * @param p_fieldName The name of the field for which the value is desired.
111      *
112      * @return the value associated with the given field name or null if no
113      * match is found.
114      */

115     public Object JavaDoc getFieldValue( String JavaDoc p_fieldName ) {
116         if ( !_fields.containsKey( p_fieldName ) ) {
117             return null;
118         }
119
120         return _fields.get( p_fieldName );
121     }
122
123     /**
124      * It will replace any previous value with the same field name.
125      *
126      * @param p_fieldName The field name identifying the given value.
127      * @param p_fieldValue The value to be stored for the given field name.
128      */

129     public void addField( String JavaDoc p_fieldName, Object JavaDoc p_fieldValue ) {
130         _fields.put( p_fieldName, p_fieldValue );
131     }
132
133     // *************************************************************************
134
// ********************** Overriden Object methods *************************
135
// *************************************************************************
136

137     /**
138      * @param p_lovValue The other LovValue to test against. The check is done
139      * on the Class of the other LovValue (needs to be a DefaultLovValue to be
140      * considered equal), the pk and on the fields (map comparison).
141      *
142      * @return true if two LovValue are equals, false otherwise.
143      */

144     public boolean equals( Object JavaDoc p_lovValue ) {
145         if ( p_lovValue == this ) {
146             return true;
147         }
148
149         if ( !( p_lovValue instanceof DefaultLovValue ) ) {
150             return false;
151         }
152
153         DefaultLovValue otherLov = (DefaultLovValue) p_lovValue;
154
155         if ( getPK().equals( otherLov.getPK() ) && _fields.equals( otherLov._fields ) ) {
156             return true;
157         }
158
159         return false;
160     }
161
162     /**
163      * @return a String representation of this LovValue, displaying the
164      * LovValue PK then each field.
165      *
166      * ex:
167      * 12, SHORT_NAME=FR, LONG_NAME=FRANCE, REGION=EUROPE, HEMISPHERE=NORTH HEMISPHERE
168      * 13, SHORT_NAME=UK, LONG_NAME=UNITED KINGDOM, REGION=EUROPE, HEMISPHERE=NORTH HEMISPHERE
169      */

170     public String JavaDoc toString() {
171         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
172         Iterator JavaDoc iter = _fields.entrySet().iterator();
173
174         sb.append( _pk );
175
176         while ( iter.hasNext() ) {
177             sb.append( FIELD_SEPARATOR );
178
179             Object JavaDoc object = iter.next();
180             sb.append( object );
181         }
182
183         return sb.toString();
184     }
185 }
186
Popular Tags