001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.metadata;
051:
052: import org.jaffa.datatypes.Defaults;
053: import org.jaffa.util.LocaleHelper;
054:
055: /**
056: * An instance of this class will hold meta information for an Integer field.
057: */
058: public class IntegerFieldMetaData extends FieldMetaData {
059:
060: /** Default width.*/
061: public static final int DEFAULT_WIDTH = 20;
062:
063: /** Default Size.*/
064: public static final int DEFAULT_SIZE = 10;
065:
066: /** Default Layout To Use.*/
067: //public static final String DEFAULT_LAYOUT = null;
068: // NOTE: keep the equals(), clone(), compareTo(), hashCode() methods in sync
069: private String m_layout = null;
070: private Long m_minValue = null;
071: private Long m_maxValue = null;
072: private Integer m_intSize = null;
073:
074: /** Creates an instance.
075: */
076: public IntegerFieldMetaData() {
077: this (null, null, new Boolean(false), getIntegerFormat(),
078: new Long(Long.MIN_VALUE), new Long(Long.MAX_VALUE),
079: new Integer(DEFAULT_SIZE));
080: }
081:
082: /** Creates an instance.
083: * @param name The field name.
084: * @param labelToken The token used for displaying labels.
085: * @param isMandatory Indicates if the field is mandatory.
086: * @param layout The layout.
087: * @param minValue The minimum value.
088: * @param maxValue The maximum value.
089: * @param intSize The maximum number of significant digits.
090: */
091: public IntegerFieldMetaData(String name, String labelToken,
092: Boolean isMandatory, String layout, Long minValue,
093: Long maxValue, Integer intSize) {
094: super (name, Defaults.INTEGER, labelToken, isMandatory);
095: m_layout = layout;
096: m_minValue = minValue;
097: m_maxValue = maxValue;
098: m_intSize = intSize;
099: }
100:
101: /** Getter for property layout.
102: * @return Value of property layout.
103: */
104: public String getLayout() {
105: return m_layout;
106: }
107:
108: /** Getter for property minValue.
109: * @return Value of property minValue.
110: */
111: public Long getMinValue() {
112: return m_minValue;
113: }
114:
115: /** Getter for property maxValue.
116: * @return Value of property maxValue.
117: */
118: public Long getMaxValue() {
119: return m_maxValue;
120: }
121:
122: /** Getter for property intSize.
123: * @return Value of property intSize.
124: */
125: public Integer getIntSize() {
126: return m_intSize;
127: }
128:
129: /** Returns a clone of the object.
130: * @return a clone of the object.
131: */
132: public Object clone() {
133: // no more processing required since the fields are immutable
134: return super .clone();
135: }
136:
137: /** Returns the hash code.
138: * @return the hash code.
139: */
140: public int hashCode() {
141: int i = 0;
142: i = super .hashCode();
143: if (m_layout != null)
144: i += m_layout.hashCode();
145: if (m_minValue != null)
146: i += m_minValue.hashCode();
147: if (m_minValue != null)
148: i += m_maxValue.hashCode();
149: if (m_intSize != null)
150: i += m_intSize.hashCode();
151: return i;
152: }
153:
154: /** Compares this object with another IntegerFieldMetaData object.
155: * Returns a true if both the objects have the same properties.
156: * @param obj the other IntegerFieldMetaData object.
157: * @return a true if both the objects have the same properties.
158: */
159: public boolean equals(Object obj) {
160: boolean isEqual = false;
161: if (obj instanceof IntegerFieldMetaData) {
162: IntegerFieldMetaData field2 = (IntegerFieldMetaData) obj;
163: if (super .equals(field2)) {
164: if (((m_layout != null && m_layout
165: .equals(field2.m_layout)) || (m_layout == null && field2.m_layout == null))
166: && ((m_minValue != null && m_minValue
167: .equals(field2.m_minValue)) || (m_minValue == null && field2.m_minValue == null))
168: && ((m_maxValue != null && m_maxValue
169: .equals(field2.m_maxValue)) || (m_maxValue == null && field2.m_maxValue == null))
170: && ((m_intSize != null && m_intSize
171: .equals(field2.m_intSize)) || (m_intSize == null && field2.m_intSize == null)))
172: isEqual = true;
173: }
174: }
175: return isEqual;
176: }
177:
178: /** Compares this object with another IntegerFieldMetaData object.
179: * Note: this class has a natural ordering that is inconsistent with equals
180: * @param obj the other IntegerFieldMetaData object.
181: * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
182: */
183: public int compareTo(Object obj) {
184: // NOTE: this isnt a perfect compare !!!
185: return super .compareTo(obj);
186: }
187:
188: /** Returns the diagnostic information.
189: * @return the diagnostic information.
190: */
191: public String toString() {
192: String comma = ", ";
193: String equals = "=";
194: StringBuffer buffer = new StringBuffer(super .toString());
195:
196: buffer.append(comma);
197: buffer.append("Layout");
198: buffer.append(equals);
199: buffer.append(m_layout);
200: buffer.append(comma);
201: buffer.append("MinValue");
202: buffer.append(equals);
203: buffer.append(m_minValue);
204: buffer.append(comma);
205: buffer.append("MaxValue");
206: buffer.append(equals);
207: buffer.append(m_maxValue);
208: buffer.append(comma);
209: buffer.append("IntSize");
210: buffer.append(equals);
211: buffer.append(m_intSize);
212:
213: return buffer.toString();
214: }
215:
216: /** Getter for property width.
217: * @return Value of property width.
218: */
219: public int getWidth() {
220: int i = 0;
221: if (m_intSize == null)
222: i = DEFAULT_WIDTH;
223: else {
224: i = m_intSize.intValue();
225: //group separator
226: i += i / 3;
227: //negative sign
228: i += 1;
229: }
230: return i;
231: }
232:
233: public static String getIntegerFormat() {
234: return LocaleHelper.getProperty("integer.format");
235: }
236: }
|