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 java.io.Serializable;
019: import org.joda.time.DurationField;
020: import org.joda.time.DurationFieldType;
021:
022: /**
023: * BaseDurationField provides the common behaviour for DurationField
024: * implementations.
025: * <p>
026: * This class should generally not be used directly by API users. The
027: * DurationField class should be used when different kinds of DurationField
028: * objects are to be referenced.
029: * <p>
030: * BaseDurationField is thread-safe and immutable, and its subclasses must
031: * be as well.
032: *
033: * @author Brian S O'Neill
034: * @see DecoratedDurationField
035: * @since 1.0
036: */
037: public abstract class BaseDurationField extends DurationField implements
038: Serializable {
039:
040: /** Serialization lock. */
041: private static final long serialVersionUID = -2554245107589433218L;
042:
043: /** A desriptive name for the field. */
044: private final DurationFieldType iType;
045:
046: protected BaseDurationField(DurationFieldType type) {
047: super ();
048: if (type == null) {
049: throw new IllegalArgumentException(
050: "The type must not be null");
051: }
052: iType = type;
053: }
054:
055: public final DurationFieldType getType() {
056: return iType;
057: }
058:
059: public final String getName() {
060: return iType.getName();
061: }
062:
063: /**
064: * @return true always
065: */
066: public final boolean isSupported() {
067: return true;
068: }
069:
070: //------------------------------------------------------------------------
071: /**
072: * Get the value of this field from the milliseconds, which is approximate
073: * if this field is imprecise.
074: *
075: * @param duration the milliseconds to query, which may be negative
076: * @return the value of the field, in the units of the field, which may be
077: * negative
078: */
079: public int getValue(long duration) {
080: return FieldUtils.safeToInt(getValueAsLong(duration));
081: }
082:
083: /**
084: * Get the value of this field from the milliseconds, which is approximate
085: * if this field is imprecise.
086: *
087: * @param duration the milliseconds to query, which may be negative
088: * @return the value of the field, in the units of the field, which may be
089: * negative
090: */
091: public long getValueAsLong(long duration) {
092: return duration / getUnitMillis();
093: }
094:
095: /**
096: * Get the value of this field from the milliseconds relative to an
097: * instant.
098: *
099: * <p>If the milliseconds is positive, then the instant is treated as a
100: * "start instant". If negative, the instant is treated as an "end
101: * instant".
102: *
103: * <p>The default implementation returns
104: * <code>Utils.safeToInt(getAsLong(millisDuration, instant))</code>.
105: *
106: * @param duration the milliseconds to query, which may be negative
107: * @param instant the start instant to calculate relative to
108: * @return the value of the field, in the units of the field, which may be
109: * negative
110: */
111: public int getValue(long duration, long instant) {
112: return FieldUtils.safeToInt(getValueAsLong(duration, instant));
113: }
114:
115: /**
116: * Get the millisecond duration of this field from its value, which is
117: * approximate if this field is imprecise.
118: *
119: * @param value the value of the field, which may be negative
120: * @return the milliseconds that the field represents, which may be
121: * negative
122: */
123: public long getMillis(int value) {
124: return value * getUnitMillis(); // safe
125: }
126:
127: /**
128: * Get the millisecond duration of this field from its value, which is
129: * approximate if this field is imprecise.
130: *
131: * @param value the value of the field, which may be negative
132: * @return the milliseconds that the field represents, which may be
133: * negative
134: */
135: public long getMillis(long value) {
136: return FieldUtils.safeMultiply(value, getUnitMillis());
137: }
138:
139: // Calculation API
140: //------------------------------------------------------------------------
141: public int getDifference(long minuendInstant, long subtrahendInstant) {
142: return FieldUtils.safeToInt(getDifferenceAsLong(minuendInstant,
143: subtrahendInstant));
144: }
145:
146: //------------------------------------------------------------------------
147: public int compareTo(Object durationField) {
148: DurationField otherField = (DurationField) durationField;
149: long otherMillis = otherField.getUnitMillis();
150: long this Millis = getUnitMillis();
151: // cannot do (thisMillis - otherMillis) as can overflow
152: if (this Millis == otherMillis) {
153: return 0;
154: }
155: if (this Millis < otherMillis) {
156: return -1;
157: } else {
158: return 1;
159: }
160: }
161:
162: /**
163: * Get a suitable debug string.
164: *
165: * @return debug string
166: */
167: public String toString() {
168: return "DurationField[" + getName() + ']';
169: }
170:
171: }
|