001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.field;
017:
018: import org.joda.time.DurationField;
019: import org.joda.time.DurationFieldType;
020:
021: /**
022: * Scales a DurationField such that it's unit millis becomes larger in
023: * magnitude.
024: * <p>
025: * ScaledDurationField is thread-safe and immutable.
026: *
027: * @see PreciseDurationField
028: *
029: * @author Brian S O'Neill
030: * @since 1.0
031: */
032: public class ScaledDurationField extends DecoratedDurationField {
033:
034: private static final long serialVersionUID = -3205227092378684157L;
035:
036: private final int iScalar;
037:
038: /**
039: * Constructor
040: *
041: * @param field the field to wrap, like "year()".
042: * @param type the type this field will actually use
043: * @param scalar scalar, such as 100 years in a century
044: * @throws IllegalArgumentException if scalar is zero or one.
045: */
046: public ScaledDurationField(DurationField field,
047: DurationFieldType type, int scalar) {
048: super (field, type);
049: if (scalar == 0 || scalar == 1) {
050: throw new IllegalArgumentException(
051: "The scalar must not be 0 or 1");
052: }
053: iScalar = scalar;
054: }
055:
056: public int getValue(long duration) {
057: return getWrappedField().getValue(duration) / iScalar;
058: }
059:
060: public long getValueAsLong(long duration) {
061: return getWrappedField().getValueAsLong(duration) / iScalar;
062: }
063:
064: public int getValue(long duration, long instant) {
065: return getWrappedField().getValue(duration, instant) / iScalar;
066: }
067:
068: public long getValueAsLong(long duration, long instant) {
069: return getWrappedField().getValueAsLong(duration, instant)
070: / iScalar;
071: }
072:
073: public long getMillis(int value) {
074: long scaled = ((long) value) * ((long) iScalar);
075: return getWrappedField().getMillis(scaled);
076: }
077:
078: public long getMillis(long value) {
079: long scaled = FieldUtils.safeMultiply(value, iScalar);
080: return getWrappedField().getMillis(scaled);
081: }
082:
083: public long getMillis(int value, long instant) {
084: long scaled = ((long) value) * ((long) iScalar);
085: return getWrappedField().getMillis(scaled, instant);
086: }
087:
088: public long getMillis(long value, long instant) {
089: long scaled = FieldUtils.safeMultiply(value, iScalar);
090: return getWrappedField().getMillis(scaled, instant);
091: }
092:
093: public long add(long instant, int value) {
094: long scaled = ((long) value) * ((long) iScalar);
095: return getWrappedField().add(instant, scaled);
096: }
097:
098: public long add(long instant, long value) {
099: long scaled = FieldUtils.safeMultiply(value, iScalar);
100: return getWrappedField().add(instant, scaled);
101: }
102:
103: public int getDifference(long minuendInstant, long subtrahendInstant) {
104: return getWrappedField().getDifference(minuendInstant,
105: subtrahendInstant)
106: / iScalar;
107: }
108:
109: public long getDifferenceAsLong(long minuendInstant,
110: long subtrahendInstant) {
111: return getWrappedField().getDifferenceAsLong(minuendInstant,
112: subtrahendInstant)
113: / iScalar;
114: }
115:
116: public long getUnitMillis() {
117: return getWrappedField().getUnitMillis() * iScalar;
118: }
119:
120: //-----------------------------------------------------------------------
121: /**
122: * Returns the scalar applied, in the field's units.
123: *
124: * @return the scalar
125: */
126: public int getScalar() {
127: return iScalar;
128: }
129:
130: /**
131: * Compares this duration field to another.
132: * Two fields are equal if of the same type and duration.
133: *
134: * @param obj the object to compare to
135: * @return if equal
136: */
137: public boolean equals(Object obj) {
138: if (this == obj) {
139: return true;
140: } else if (obj instanceof ScaledDurationField) {
141: ScaledDurationField other = (ScaledDurationField) obj;
142: return (getWrappedField().equals(other.getWrappedField()))
143: && (getType() == other.getType())
144: && (iScalar == other.iScalar);
145: }
146: return false;
147: }
148:
149: /**
150: * Gets a hash code for this instance.
151: *
152: * @return a suitable hashcode
153: */
154: public int hashCode() {
155: long scalar = iScalar;
156: int hash = (int) (scalar ^ (scalar >>> 32));
157: hash += getType().hashCode();
158: hash += getWrappedField().hashCode();
159: return hash;
160: }
161:
162: }
|