KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > field > MillisDurationField


1 /*
2  * Copyright 2001-2005 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 JavaDoc;
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 JavaDoc {
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 JavaDoc 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(Object JavaDoc durationField) {
134         DurationField otherField = (DurationField) durationField;
135         long otherMillis = otherField.getUnitMillis();
136         long thisMillis = getUnitMillis();
137         // cannot do (thisMillis - otherMillis) as can overflow
138
if (thisMillis == otherMillis) {
139             return 0;
140         }
141         if (thisMillis < otherMillis) {
142             return -1;
143         } else {
144             return 1;
145         }
146     }
147
148     /**
149      * Get a suitable debug string.
150      *
151      * @return debug string
152      */

153     public String JavaDoc toString() {
154         return "DurationField[millis]";
155     }
156
157     /**
158      * Deserialize to the singleton.
159      */

160     private Object JavaDoc readResolve() {
161         return INSTANCE;
162     }
163
164 }
165
Popular Tags