KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > chrono > BasicYearDateTimeField


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.chrono;
17
18 import org.joda.time.DateTimeFieldType;
19 import org.joda.time.DurationField;
20 import org.joda.time.field.FieldUtils;
21 import org.joda.time.field.ImpreciseDateTimeField;
22
23 /**
24  * A year field suitable for many calendars.
25  *
26  * @author Guy Allard
27  * @author Stephen Colebourne
28  * @author Brian S O'Neill
29  * @since 1.1, refactored from GJYearDateTimeField
30  */

31 class BasicYearDateTimeField extends ImpreciseDateTimeField {
32
33     /** Serialization version. */
34     private static final long serialVersionUID = -98628754872287L;
35
36     /** The underlying basic chronology. */
37     protected final BasicChronology iChronology;
38
39     /**
40      * Restricted constructor.
41      *
42      * @param chronology the chronology this field belogs to
43      */

44     BasicYearDateTimeField(BasicChronology chronology) {
45         super(DateTimeFieldType.year(), chronology.getAverageMillisPerYear());
46         iChronology = chronology;
47     }
48
49     public boolean isLenient() {
50         return false;
51     }
52
53     public int get(long instant) {
54         return iChronology.getYear(instant);
55     }
56
57     public long add(long instant, int years) {
58         if (years == 0) {
59             return instant;
60         }
61         int thisYear = get(instant);
62         int newYear = FieldUtils.safeAdd(thisYear, years);
63         return set(instant, newYear);
64     }
65
66     public long add(long instant, long years) {
67         return add(instant, FieldUtils.safeToInt(years));
68     }
69
70     public long addWrapField(long instant, int years) {
71         if (years == 0) {
72             return instant;
73         }
74         // Return newly calculated millis value
75
int thisYear = iChronology.getYear(instant);
76         int wrappedYear = FieldUtils.getWrappedValue
77             (thisYear, years, iChronology.getMinYear(), iChronology.getMaxYear());
78         return set(instant, wrappedYear);
79     }
80
81     public long set(long instant, int year) {
82         FieldUtils.verifyValueBounds
83             (this, year, iChronology.getMinYear(), iChronology.getMaxYear());
84         return iChronology.setYear(instant, year);
85     }
86
87     public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
88         if (minuendInstant < subtrahendInstant) {
89             return -iChronology.getYearDifference(subtrahendInstant, minuendInstant);
90         }
91         return iChronology.getYearDifference(minuendInstant, subtrahendInstant);
92     }
93
94     public DurationField getRangeDurationField() {
95         return null;
96     }
97
98     public boolean isLeap(long instant) {
99         return iChronology.isLeapYear(get(instant));
100     }
101
102     public int getLeapAmount(long instant) {
103         if (iChronology.isLeapYear(get(instant))) {
104             return 1;
105         } else {
106             return 0;
107         }
108     }
109
110     public DurationField getLeapDurationField() {
111         return iChronology.days();
112     }
113
114     public int getMinimumValue() {
115         return iChronology.getMinYear();
116     }
117
118     public int getMaximumValue() {
119         return iChronology.getMaxYear();
120     }
121
122     public long roundFloor(long instant) {
123         return iChronology.getYearMillis(get(instant));
124     }
125
126     public long roundCeiling(long instant) {
127         int year = get(instant);
128         long yearStartMillis = iChronology.getYearMillis(year);
129         if (instant != yearStartMillis) {
130             // Bump up to start of next year.
131
instant = iChronology.getYearMillis(year + 1);
132         }
133         return instant;
134     }
135
136     public long remainder(long instant) {
137         return instant - roundFloor(instant);
138     }
139
140     /**
141      * Serialization singleton
142      */

143     private Object JavaDoc readResolve() {
144         return iChronology.year();
145     }
146 }
147
Popular Tags