001: package org.julp;
002:
003: public class ValueObject implements Cloneable, java.io.Serializable,
004: Comparable {
005:
006: protected Object value;
007: protected String valueLabel;
008: protected boolean compareByValue = false;
009: protected boolean errorOnNullCompare;
010:
011: public ValueObject() {
012: }
013:
014: public ValueObject(Object value, String valueLabel) {
015: this .value = value;
016: this .valueLabel = valueLabel;
017: }
018:
019: public ValueObject(Object value, String valueLabel,
020: boolean compareByValue) {
021: this .value = value;
022: this .valueLabel = valueLabel;
023: this .compareByValue = compareByValue;
024: }
025:
026: public java.lang.Object getValue() {
027: return value;
028: }
029:
030: public void setValue(java.lang.Object value) {
031: this .value = value;
032: }
033:
034: public java.lang.String getValueLabel() {
035: if (valueLabel == null) {
036: if (value != null) {
037: valueLabel = value.toString();
038: } else {
039: valueLabel = "";
040: }
041: }
042: return valueLabel;
043: }
044:
045: public void setValueLabel(java.lang.String valueLabel) {
046: this .valueLabel = valueLabel;
047: }
048:
049: public boolean isCompareByValue() {
050: return compareByValue;
051: }
052:
053: public void setCompareByValue(boolean compareByValue) {
054: this .compareByValue = compareByValue;
055: }
056:
057: public String toString() {
058: return getValueLabel();
059: }
060:
061: public int compareTo(Object otherObj) {
062: if (otherObj == null) {
063: if (errorOnNullCompare) {
064: throw new NullPointerException("ValueObject is null");
065: } else {
066: return -1;
067: }
068: }
069: if (this .equals(otherObj)) {
070: return 0;
071: }
072: ValueObject other = (ValueObject) otherObj;
073: int i1 = this .valueLabel.compareTo(other.valueLabel);
074: int i2 = ((Comparable) this .value)
075: .compareTo((Comparable) other.value);
076: if (i1 > 0 && i2 > 0) {
077: return 1;
078: } else if (i1 < 0 && i2 < 0) {
079: return -1;
080: } else {
081: if (compareByValue) {
082: return i2;
083: } else {
084: return i1;
085: }
086: }
087: }
088:
089: public boolean equals(java.lang.Object otherObj) {
090: if (!(otherObj instanceof ValueObject)) {
091: return false;
092: }
093: ValueObject other = (ValueObject) otherObj;
094: return ((value == null ? other.value == null : value
095: .equals(other.value)) && (valueLabel == null ? other.valueLabel == null
096: : valueLabel.equals(other.valueLabel)));
097: }
098:
099: public int hashCode() {
100: return ((value == null ? -1 : value.hashCode()) ^ (valueLabel == null ? -1
101: : valueLabel.hashCode()));
102: }
103:
104: public boolean isErrorOnNullCompare() {
105: return errorOnNullCompare;
106: }
107:
108: public void setErrorOnNullCompare(boolean errorOnNullCompare) {
109: this.errorOnNullCompare = errorOnNullCompare;
110: }
111:
112: }
|