1 /*
2  *  Copyright 2001-2009 Stephen Colebourne
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */

16 package org.joda.time.field;
17
18 import java.io.Serializable;
19
20 import org.joda.time.DurationField;
21 import org.joda.time.DurationFieldType;
22
23 /**
24  * Duration field class representing a field with a fixed unit length of one
25  * millisecond.
26  * <p>
27  * MillisDurationField is thread-safe and immutable.
28  *
29  * @author Brian S O'Neill
30  * @since 1.0
31  */

32 public final class MillisDurationField extends DurationField implements Serializable {
33
34     /** Serialization lock. */
35     private static final long serialVersionUID = 2656707858124633367L;
36
37     /** Singleton instance. */
38     public static final DurationField INSTANCE = new MillisDurationField();
39
40     /**
41      * Restricted constructor.
42      */

43     private MillisDurationField() {
44         super();
45     }
46     
47     //------------------------------------------------------------------------
48     public DurationFieldType getType() {
49         return DurationFieldType.millis();
50     }
51
52     public String getName() {
53         return "millis";
54     }
55
56     /**
57      * Returns true as this field is supported.
58      * 
59      * @return true always
60      */

61     public boolean isSupported() {
62         return true;
63     }
64
65     /**
66      * Returns true as this field is precise.
67      * 
68      * @return true always
69      */

70     public final boolean isPrecise() {
71         return true;
72     }
73
74     /**
75      * Returns the amount of milliseconds per unit value of this field.
76      *
77      * @return one always
78      */

79     public final long getUnitMillis() {
80         return 1;
81     }
82
83     //------------------------------------------------------------------------
84     public int getValue(long duration) {
85         return FieldUtils.safeToInt(duration);
86     }
87
88     public long getValueAsLong(long duration) {
89         return duration;
90     }
91
92     public int getValue(long duration, long instant) {
93         return FieldUtils.safeToInt(duration);
94     }
95
96     public long getValueAsLong(long duration, long instant) {
97         return duration;
98     }
99
100     public long getMillis(int value) {
101         return value;
102     }
103
104     public long getMillis(long value) {
105         return value;
106     }
107
108     public long getMillis(int value, long instant) {
109         return value;
110     }
111
112     public long getMillis(long value, long instant) {
113         return value;
114     }
115
116     public long add(long instant, int value) {
117         return FieldUtils.safeAdd(instant, value);
118     }
119
120     public long add(long instant, long value) {
121         return FieldUtils.safeAdd(instant, value);
122     }
123
124     public int getDifference(long minuendInstant, long subtrahendInstant) {
125         return FieldUtils.safeToInt(FieldUtils.safeSubtract(minuendInstant, subtrahendInstant));
126     }
127
128     public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
129         return FieldUtils.safeSubtract(minuendInstant, subtrahendInstant);
130     }
131
132     //------------------------------------------------------------------------
133     public int compareTo(DurationField otherField) {
134         long otherMillis = otherField.getUnitMillis();
135         long thisMillis = getUnitMillis();
136         // cannot do (thisMillis - otherMillis) as can overflow
137         if (thisMillis == otherMillis) {
138             return 0;
139         }
140         if (thisMillis < otherMillis) {
141             return -1;
142         } else {
143             return 1;
144         }
145     }
146
147     public boolean equals(Object obj) {
148         if (obj instanceof MillisDurationField) {
149             return getUnitMillis() == ((MillisDurationField) obj).getUnitMillis();
150         }
151         return false;
152     }
153
154     public int hashCode() {
155         return (int) getUnitMillis();
156     }
157
158     /**
159      * Get a suitable debug string.
160      * 
161      * @return debug string
162      */

163     public String toString() {
164         return "DurationField[millis]";
165     }
166
167     /**
168      * Deserialize to the singleton.
169      */

170     private Object readResolve() {
171         return INSTANCE;
172     }
173
174 }
175