KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.joda.time.DurationField;
19 import org.joda.time.DurationFieldType;
20
21 /**
22  * Scales a DurationField such that it's unit millis becomes larger in
23  * magnitude.
24  * <p>
25  * ScaledDurationField is thread-safe and immutable.
26  *
27  * @see PreciseDurationField
28  *
29  * @author Brian S O'Neill
30  * @since 1.0
31  */

32 public class ScaledDurationField extends DecoratedDurationField {
33
34     private static final long serialVersionUID = -3205227092378684157L;
35
36     private final int iScalar;
37
38     /**
39      * Constructor
40      *
41      * @param field the field to wrap, like "year()".
42      * @param type the type this field will actually use
43      * @param scalar scalar, such as 100 years in a century
44      * @throws IllegalArgumentException if scalar is zero or one.
45      */

46     public ScaledDurationField(DurationField field, DurationFieldType type, int scalar) {
47         super(field, type);
48         if (scalar == 0 || scalar == 1) {
49             throw new IllegalArgumentException JavaDoc("The scalar must not be 0 or 1");
50         }
51         iScalar = scalar;
52     }
53
54     public int getValue(long duration) {
55         return getWrappedField().getValue(duration) / iScalar;
56     }
57
58     public long getValueAsLong(long duration) {
59         return getWrappedField().getValueAsLong(duration) / iScalar;
60     }
61
62     public int getValue(long duration, long instant) {
63         return getWrappedField().getValue(duration, instant) / iScalar;
64     }
65
66     public long getValueAsLong(long duration, long instant) {
67         return getWrappedField().getValueAsLong(duration, instant) / iScalar;
68     }
69
70     public long getMillis(int value) {
71         long scaled = ((long) value) * ((long) iScalar);
72         return getWrappedField().getMillis(scaled);
73     }
74
75     public long getMillis(long value) {
76         long scaled = FieldUtils.safeMultiply(value, iScalar);
77         return getWrappedField().getMillis(scaled);
78     }
79
80     public long getMillis(int value, long instant) {
81         long scaled = ((long) value) * ((long) iScalar);
82         return getWrappedField().getMillis(scaled, instant);
83     }
84
85     public long getMillis(long value, long instant) {
86         long scaled = FieldUtils.safeMultiply(value, iScalar);
87         return getWrappedField().getMillis(scaled, instant);
88     }
89
90     public long add(long instant, int value) {
91         long scaled = ((long) value) * ((long) iScalar);
92         return getWrappedField().add(instant, scaled);
93     }
94
95     public long add(long instant, long value) {
96         long scaled = FieldUtils.safeMultiply(value, iScalar);
97         return getWrappedField().add(instant, scaled);
98     }
99
100     public int getDifference(long minuendInstant, long subtrahendInstant) {
101         return getWrappedField().getDifference(minuendInstant, subtrahendInstant) / iScalar;
102     }
103
104     public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
105         return getWrappedField().getDifferenceAsLong(minuendInstant, subtrahendInstant) / iScalar;
106     }
107
108     public long getUnitMillis() {
109         return getWrappedField().getUnitMillis() * iScalar;
110     }
111
112     //-----------------------------------------------------------------------
113
/**
114      * Returns the scalar applied, in the field's units.
115      *
116      * @return the scalar
117      */

118     public int getScalar() {
119         return iScalar;
120     }
121
122     /**
123      * Compares this duration field to another.
124      * Two fields are equal if of the same type and duration.
125      *
126      * @param obj the object to compare to
127      * @return if equal
128      */

129     public boolean equals(Object JavaDoc obj) {
130         if (this == obj) {
131             return true;
132         } else if (obj instanceof ScaledDurationField) {
133             ScaledDurationField other = (ScaledDurationField) obj;
134             return (getWrappedField().equals(other.getWrappedField())) &&
135                    (getType() == other.getType()) &&
136                    (iScalar == other.iScalar);
137         }
138         return false;
139     }
140
141     /**
142      * Gets a hash code for this instance.
143      *
144      * @return a suitable hashcode
145      */

146     public int hashCode() {
147         long scalar = iScalar;
148         int hash = (int) (scalar ^ (scalar >>> 32));
149         hash += getType().hashCode();
150         hash += getWrappedField().hashCode();
151         return hash;
152     }
153
154 }
155
Popular Tags