1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25
26
27
28
29
30
31
32 public final class MillisDurationField extends DurationField implements Serializable {
33
34
35 private static final long serialVersionUID = 2656707858124633367L;
36
37
38 public static final DurationField INSTANCE = new MillisDurationField();
39
40
41
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
58
59
60
61 public boolean isSupported() {
62 return true;
63 }
64
65
66
67
68
69
70 public final boolean isPrecise() {
71 return true;
72 }
73
74
75
76
77
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
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
160
161
162
163 public String toString() {
164 return "DurationField[millis]";
165 }
166
167
168
169
170 private Object readResolve() {
171 return INSTANCE;
172 }
173
174 }