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.*;
053: import org.jaffa.util.LocaleHelper;
054:
055: /**
056: * An instance of this class will hold meta information for a Currency field.
057: */
058: public class CurrencyFieldMetaData extends FieldMetaData {
059:
060: /** Default width.*/
061: public static final int DEFAULT_WIDTH = 20;
062:
063: /** Default Integer Size.*/
064: public static final int DEFAULT_INT_SIZE = 10;
065:
066: /** Default Fraction Size.*/
067: public static final int DEFAULT_FRAC_SIZE = 2;
068:
069: /** Default Layout To Use.*/
070: //public static final String DEFAULT_LAYOUT = null;
071: // NOTE: keep the equals(), clone(), compareTo(), hashCode() methods in sync
072: private String m_layout = null;
073: private Currency m_minValue = null;
074: private Currency m_maxValue = null;
075: private Integer m_intSize = null;
076: private Integer m_fracSize = null;
077:
078: /** Creates an instance.
079: */
080: public CurrencyFieldMetaData() {
081: this (null, null, new Boolean(false), getCurrencyFormat(),
082: new Currency(Double.MIN_VALUE), new Currency(
083: Double.MAX_VALUE),
084: new Integer(DEFAULT_INT_SIZE), new Integer(
085: DEFAULT_FRAC_SIZE));
086: }
087:
088: /** Creates an instance.
089: * @param name The field name.
090: * @param labelToken The token used for displaying labels.
091: * @param isMandatory Indicates if the field is mandatory.
092: * @param layout The layout.
093: * @param minValue The minimum value.
094: * @param maxValue The maximum value.
095: * @param intSize The maximum number of significant digits.
096: * @param fracSize The maximum number of fractional digits.
097: */
098: public CurrencyFieldMetaData(String name, String labelToken,
099: Boolean isMandatory, String layout, Currency minValue,
100: Currency maxValue, Integer intSize, Integer fracSize) {
101: super (name, Defaults.CURRENCY, labelToken, isMandatory);
102: m_layout = layout;
103: m_minValue = minValue;
104: m_maxValue = maxValue;
105: m_intSize = intSize;
106: m_fracSize = fracSize;
107: }
108:
109: /** Getter for property layout.
110: * @return Value of property layout.
111: */
112: public String getLayout() {
113: return m_layout;
114: }
115:
116: /** Getter for property minValue.
117: * @return Value of property minValue.
118: */
119: public Currency getMinValue() {
120: return m_minValue;
121: }
122:
123: /** Getter for property maxValue.
124: * @return Value of property maxValue.
125: */
126: public Currency getMaxValue() {
127: return m_maxValue;
128: }
129:
130: /** Getter for property intSize.
131: * @return Value of property intSize.
132: */
133: public Integer getIntSize() {
134: return m_intSize;
135: }
136:
137: /** Getter for property fracSize.
138: * @return Value of property fracSize.
139: */
140: public Integer getFracSize() {
141: return m_fracSize;
142: }
143:
144: /** Returns a clone of the object.
145: * @return a clone of the object.
146: */
147: public Object clone() {
148: // no more processing required since the fields are immutable
149: return super .clone();
150: }
151:
152: /** Returns the hash code.
153: * @return the hash code.
154: */
155: public int hashCode() {
156: int i = 0;
157: i = super .hashCode();
158: if (m_layout != null)
159: i += m_layout.hashCode();
160: if (m_minValue != null)
161: i += m_minValue.hashCode();
162: if (m_minValue != null)
163: i += m_maxValue.hashCode();
164: if (m_intSize != null)
165: i += m_intSize.hashCode();
166: if (m_fracSize != null)
167: i += m_fracSize.hashCode();
168: return i;
169: }
170:
171: /** Compares this object with another CurrencyFieldMetaData object.
172: * Returns a true if both the objects have the same properties.
173: * @param obj the other CurrencyFieldMetaData object.
174: * @return a true if both the objects have the same properties.
175: */
176: public boolean equals(Object obj) {
177: boolean isEqual = false;
178: if (obj instanceof CurrencyFieldMetaData) {
179: CurrencyFieldMetaData field2 = (CurrencyFieldMetaData) obj;
180: if (super .equals(field2)) {
181: if (((m_layout != null && m_layout
182: .equals(field2.m_layout)) || (m_layout == null && field2.m_layout == null))
183: && ((m_minValue != null && m_minValue
184: .equals(field2.m_minValue)) || (m_minValue == null && field2.m_minValue == null))
185: && ((m_maxValue != null && m_maxValue
186: .equals(field2.m_maxValue)) || (m_maxValue == null && field2.m_maxValue == null))
187: && ((m_fracSize != null && m_fracSize
188: .equals(field2.m_fracSize)) || (m_fracSize == null && field2.m_fracSize == null))
189: && ((m_intSize != null && m_intSize
190: .equals(field2.m_intSize)) || (m_intSize == null && field2.m_intSize == null)))
191: isEqual = true;
192: }
193: }
194: return isEqual;
195: }
196:
197: /** Compares this object with another CurrencyFieldMetaData object.
198: * Note: this class has a natural ordering that is inconsistent with equals
199: * @param obj the other CurrencyFieldMetaData object.
200: * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
201: */
202: public int compareTo(Object obj) {
203: // NOTE: this isnt a perfect compare !!!
204: return super .compareTo(obj);
205: }
206:
207: /** Returns the diagnostic information.
208: * @return the diagnostic information.
209: */
210: public String toString() {
211: String comma = ", ";
212: String equals = "=";
213: StringBuffer buffer = new StringBuffer(super .toString());
214:
215: buffer.append(comma);
216: buffer.append("Layout");
217: buffer.append(equals);
218: buffer.append(m_layout);
219: buffer.append(comma);
220: buffer.append("MinValue");
221: buffer.append(equals);
222: buffer.append(m_minValue);
223: buffer.append(comma);
224: buffer.append("MaxValue");
225: buffer.append(equals);
226: buffer.append(m_maxValue);
227: buffer.append(comma);
228: buffer.append("IntSize");
229: buffer.append(equals);
230: buffer.append(m_intSize);
231: buffer.append(comma);
232: buffer.append("FracSize");
233: buffer.append(equals);
234: buffer.append(m_fracSize);
235:
236: return buffer.toString();
237: }
238:
239: /** Getter for property width.
240: * @return Value of property width.
241: */
242: public int getWidth() {
243: int i = 0;
244: if (m_intSize != null)
245: i = m_intSize.intValue();
246:
247: if (m_fracSize != null)
248: i = m_fracSize.intValue();
249:
250: if (i == 0)
251: i = DEFAULT_WIDTH;
252: else {
253: //decimal separator
254: i += 1;
255: //group separator
256: i += i / 3;
257: //negative sign
258: i += 1;
259: }
260: return i;
261: }
262:
263: public static String getCurrencyFormat() {
264: return LocaleHelper.getProperty("currency.format");
265: }
266: }
|