OSDN Git Service

1010c8620b26e985d6dcbb0336dc09d611199e85
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / TimeType.java
1 /*
2  * Copyright 2011 BitMeister Inc.
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 jp.bitmeister.asn1.type;
17
18 import java.nio.charset.Charset;
19 import java.text.DateFormat;
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.regex.Pattern;
23
24 import jp.bitmeister.asn1.processor.ASN1Visitor;
25
26 /**
27  * The base class for types which contains a {@code String} that represents date
28  * and time.
29  * 
30  * <p>
31  * This class provides generic interfaces and common methods for classes which
32  * represents date and time types.
33  * </p>
34  * 
35  * @author WATANABE, Jun. <jwat at bitmeister.jp>
36  */
37 public abstract class TimeType extends StringType {
38
39         private static final Pattern pattern = Pattern.compile("[0-9\\.Z\\+\\-]*");
40
41         /**
42          * Converts the {@code Date} object to a formatted {@code String} that
43          * represents date and time, and set it to this instance.
44          * 
45          * @param value
46          *            The {@code Date} to be assigned.
47          */
48         public void set(Date value) {
49                 set(form().format(value));
50         }
51
52         /**
53          * Returns a {@code Date} converted from the value of this instance.
54          * 
55          * @return A {@code Date}.
56          */
57         public Date date() {
58                 String time;
59                 String differential;
60                 String value = stringValue();
61                 if (value.endsWith("Z")) {
62                         // represents UTC time.
63                         differential = "+0000";
64                         time = value.substring(0, value.length() - 1);
65                 } else if (value.indexOf("+") < 0 && value.indexOf("-") < 0) {
66                         // represents the local time
67                         differential = String.format("%tz", Calendar.getInstance());
68                         time = value;
69                 } else {
70                         // represents the local time, and the time differential
71                         differential = value.substring(value.length() - 5, value.length());
72                         time = value.substring(0, value.length() - 5);
73                 }
74                 return parseDate(time, differential);
75         }
76
77         /**
78          * Returns the {@code DateFormat} used for formatting the date and time.
79          * 
80          * @return The date format.
81          */
82         public abstract DateFormat form();
83
84         @Override
85         public Charset charset() {
86                 return Charset.forName("US-ASCII");
87         }
88
89         /*
90          * (non-Javadoc)
91          * 
92          * @see jp.bitmeister.asn1.type.StringType#pattern()
93          */
94         @Override
95         protected Pattern pattern() {
96                 return pattern;
97         }
98
99         /*
100          * (non-Javadoc)
101          * 
102          * @see
103          * jp.bitmeister.asn1.type.StringType#accept(jp.bitmeister.asn1.processor
104          * .ASN1Visitor)
105          */
106         @Override
107         public <E extends Throwable> void accept(ASN1Visitor<E> visitor) throws E {
108                 visitor.visit(this);
109         }
110
111         /**
112          * Parses and converts the strings represents a set of date, time and time
113          * defferential to a {@code Date}.
114          * 
115          * @param time
116          *            The {@code String} represents a set of date.
117          * @param differential
118          *            The {@code String} represents a time differential.
119          * @return A {@code Date} object that converted from the time and the
120          *         differential.
121          */
122         protected abstract Date parseDate(String time, String differential);
123
124 }