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:
020: import org.joda.time.DurationField;
021: import org.joda.time.DurationFieldType;
022:
023: /**
024: * Duration field class representing a field with a fixed unit length of one
025: * millisecond.
026: * <p>
027: * MillisDurationField is thread-safe and immutable.
028: *
029: * @author Brian S O'Neill
030: * @since 1.0
031: */
032: public final class MillisDurationField extends DurationField implements
033: Serializable {
034:
035: /** Serialization lock. */
036: private static final long serialVersionUID = 2656707858124633367L;
037:
038: /** Singleton instance. */
039: public static final DurationField INSTANCE = new MillisDurationField();
040:
041: /**
042: * Restricted constructor.
043: */
044: private MillisDurationField() {
045: super ();
046: }
047:
048: //------------------------------------------------------------------------
049: public DurationFieldType getType() {
050: return DurationFieldType.millis();
051: }
052:
053: public String getName() {
054: return "millis";
055: }
056:
057: /**
058: * Returns true as this field is supported.
059: *
060: * @return true always
061: */
062: public boolean isSupported() {
063: return true;
064: }
065:
066: /**
067: * Returns true as this field is precise.
068: *
069: * @return true always
070: */
071: public final boolean isPrecise() {
072: return true;
073: }
074:
075: /**
076: * Returns the amount of milliseconds per unit value of this field.
077: *
078: * @return one always
079: */
080: public final long getUnitMillis() {
081: return 1;
082: }
083:
084: //------------------------------------------------------------------------
085: public int getValue(long duration) {
086: return FieldUtils.safeToInt(duration);
087: }
088:
089: public long getValueAsLong(long duration) {
090: return duration;
091: }
092:
093: public int getValue(long duration, long instant) {
094: return FieldUtils.safeToInt(duration);
095: }
096:
097: public long getValueAsLong(long duration, long instant) {
098: return duration;
099: }
100:
101: public long getMillis(int value) {
102: return value;
103: }
104:
105: public long getMillis(long value) {
106: return value;
107: }
108:
109: public long getMillis(int value, long instant) {
110: return value;
111: }
112:
113: public long getMillis(long value, long instant) {
114: return value;
115: }
116:
117: public long add(long instant, int value) {
118: return FieldUtils.safeAdd(instant, value);
119: }
120:
121: public long add(long instant, long value) {
122: return FieldUtils.safeAdd(instant, value);
123: }
124:
125: public int getDifference(long minuendInstant, long subtrahendInstant) {
126: return FieldUtils.safeToInt(FieldUtils.safeSubtract(
127: minuendInstant, subtrahendInstant));
128: }
129:
130: public long getDifferenceAsLong(long minuendInstant,
131: long subtrahendInstant) {
132: return FieldUtils.safeSubtract(minuendInstant,
133: subtrahendInstant);
134: }
135:
136: //------------------------------------------------------------------------
137: public int compareTo(Object durationField) {
138: DurationField otherField = (DurationField) durationField;
139: long otherMillis = otherField.getUnitMillis();
140: long this Millis = getUnitMillis();
141: // cannot do (thisMillis - otherMillis) as can overflow
142: if (this Millis == otherMillis) {
143: return 0;
144: }
145: if (this Millis < otherMillis) {
146: return -1;
147: } else {
148: return 1;
149: }
150: }
151:
152: /**
153: * Get a suitable debug string.
154: *
155: * @return debug string
156: */
157: public String toString() {
158: return "DurationField[millis]";
159: }
160:
161: /**
162: * Deserialize to the singleton.
163: */
164: private Object readResolve() {
165: return INSTANCE;
166: }
167:
168: }
|