001: /*
002: * $Id: Col.java,v 1.11 2005/05/21 19:41:23 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/beans/Col.java,v $
004: * SQLUnit - a test harness for unit testing database stored procedures.
005: * Copyright (C) 2003 The SQLUnit Team
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: package net.sourceforge.sqlunit.beans;
022:
023: import net.sourceforge.sqlunit.IType;
024: import net.sourceforge.sqlunit.SQLUnitException;
025: import net.sourceforge.sqlunit.TypeFactory;
026: import net.sourceforge.sqlunit.TypeMapper;
027: import net.sourceforge.sqlunit.types.BigDecimalType;
028: import net.sourceforge.sqlunit.utils.TypeUtils;
029:
030: import org.apache.log4j.Logger;
031: import org.jdom.Element;
032:
033: /**
034: * Models a col element, representing a column in a database row.
035: * @author Sujit Pal (spal@users.sourceforge.net)
036: * @version $Revision: 1.11 $
037: */
038: public class Col implements Comparable {
039:
040: private String id;
041: private String name;
042: private String type;
043: private String className;
044: private String value;
045:
046: private static final Logger LOG = Logger.getLogger(Col.class);
047:
048: /**
049: * Returns the Id for this column.
050: * @return the id for this column.
051: */
052: public final String getId() {
053: return id;
054: }
055:
056: /**
057: * Set the id for this column.
058: * @param id the id for this column.
059: */
060: public final void setId(final String id) {
061: this .id = id;
062: }
063:
064: /**
065: * Return the name for this column.
066: * @return the name for this column.
067: */
068: public final String getName() {
069: return name;
070: }
071:
072: /**
073: * Set the name for this column,
074: * @param name the name for this column.
075: */
076: public final void setName(final String name) {
077: this .name = name;
078: }
079:
080: /**
081: * Return the XML type for this column.
082: * @return the XML type for this column,
083: */
084: public final String getType() {
085: return type;
086: }
087:
088: /**
089: * Set the XML type for this column,
090: * @param type the XML type for this column.
091: */
092: public final void setType(final String type) {
093: this .type = type;
094: }
095:
096: /**
097: * Returns the class name (for IJavaObject objects) for this column,
098: * @return the class name for this column.
099: */
100: public final String getClassName() {
101: return className;
102: }
103:
104: /**
105: * Set the class name (for IJavaObject objects) for this column,
106: * @param className the class name for this column.
107: */
108: public final void setClassName(final String className) {
109: this .className = className;
110: }
111:
112: /**
113: * Returns the value of this column as a String.
114: * @return the value of this column as a String.
115: */
116: public final String getValue() {
117: return value;
118: }
119:
120: /**
121: * Set the value of this column from a String.
122: * @param value the value to set.
123: */
124: public final void setValue(final String value) {
125: this .value = value;
126: }
127:
128: /**
129: * Returns a negative, zero or positive value depending on whether
130: * the contents of this column is less than, equal to or greater
131: * than the contents of the passed in Col object.
132: * @param obj an instance of a Col object.
133: * @return a negative, zero or positive number.
134: */
135: public final int compareTo(final Object obj) {
136: LOG.debug("compareTo(" + obj.toString() + ")");
137: if (!(obj instanceof Col)) {
138: return 0;
139: }
140: Col that = (Col) obj;
141: if (!(this .getType().equals(that.getType()))) {
142: return 0;
143: }
144: try {
145: IType this TypeObj = TypeFactory.getInstance(this .getType());
146: IType thatTypeObj = TypeFactory.getInstance(that.getType());
147: this TypeObj.toObject(this .getValue());
148: thatTypeObj.toObject(that.getValue());
149: return (this TypeObj.compareTo(thatTypeObj));
150: } catch (SQLUnitException e) {
151: return 0;
152: }
153: }
154:
155: /**
156: * Returns a true or false based on whether the objects are equal.
157: * @param obj the Object to compare against.
158: * @return true or false.
159: */
160: public final boolean equals(final Object obj) {
161: if (!(obj instanceof Col)) {
162: return false;
163: }
164: Col that = (Col) obj;
165: try {
166: boolean isTypeEqual = this .getType().equals(that.getType());
167: //(TypeUtils.getSqlTypeFromXmlType(this.getType())
168: //== TypeUtils.getSqlTypeFromXmlType(that.getType()));
169: if (isTypeEqual) {
170: String typeClassName = TypeMapper
171: .findClassById(TypeUtils
172: .getSqlTypeFromXmlType(this .getType()));
173: // special treatment for BigDecimal, we need to normalize
174: // the scales.
175: if (BigDecimalType.class.getName()
176: .equals(typeClassName)) {
177: return isTypeEqual
178: && TypeUtils.isValueOfBigDecimalEqual(this
179: .getValue(), that.getValue());
180: } else {
181: return isTypeEqual
182: && this .getValue().equals(that.getValue());
183: }
184: } else {
185: return false;
186: }
187: } catch (SQLUnitException e) {
188: return false;
189: }
190: }
191:
192: /**
193: * Returns a hashcode for the Col object. This is the sum of the
194: * hashcodes for the type and the value.
195: * @return a hash code for the Col object.
196: */
197: public final int hashCode() {
198: return this .getType().hashCode() + this .getValue().hashCode();
199: }
200:
201: /**
202: * Returns a JDOM element that represents this Col object.
203: * @return a JDOM element representing this object.
204: */
205: public final Element toElement() {
206: Element elCol = new Element("col");
207: elCol.setAttribute("id", getId());
208: elCol.setAttribute("name", (getName() == null ? "c" + getId()
209: : getName()));
210: if (getClassName() != null) {
211: elCol.setAttribute("class", getClassName());
212: }
213: elCol.setAttribute("type", getType());
214: elCol.setText(getValue());
215: return elCol;
216: }
217:
218: /**
219: * Returns a compact String representation of the Col object.
220: * @return a String representation of the Col object.
221: */
222: public final String toString() {
223: return getValue() + "(" + getType() + ")";
224: }
225: }
|