View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.time;
19  
20  import java.time.ZoneId;
21  import java.util.Date;
22  import java.util.Locale;
23  import java.util.Objects;
24  import java.util.TimeZone;
25  
26  /**
27   * An immutable {@link TimeZone}.
28   */
29  final class ImmutableTimeZone extends TimeZone {
30  
31      private static final long serialVersionUID = 1L;
32      private final TimeZone timeZone;
33  
34      ImmutableTimeZone(final TimeZone timeZone) {
35          this.timeZone = Objects.requireNonNull(timeZone, "timeZone");
36      }
37  
38      @Override
39      public Object clone() {
40          // Immutable, so return this.
41          return this;
42      }
43  
44      /**
45       * {@inheritDoc}
46       */
47      @Override
48      public boolean equals(final Object obj) {
49          if (this == obj) {
50              return true;
51          }
52          if (!(obj instanceof TimeZone)) {
53              return false;
54          }
55          final TimeZone other = (TimeZone) obj;
56          return timeZone.equals(other instanceof ImmutableTimeZone ? ((ImmutableTimeZone) other).timeZone : other);
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public String getDisplayName(final boolean daylight, final int style, final Locale locale) {
64          return timeZone.getDisplayName(daylight, style, locale);
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public int getDSTSavings() {
72          return timeZone.getDSTSavings();
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public String getID() {
80          return timeZone.getID();
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public int getOffset(final int era, final int year, final int month, final int day, final int dayOfWeek, final int milliseconds) {
88          return timeZone.getOffset(era, year, month, day, dayOfWeek, milliseconds);
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public int getOffset(final long date) {
96          return timeZone.getOffset(date);
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public int getRawOffset() {
104         return timeZone.getRawOffset();
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public int hashCode() {
112         return timeZone.hashCode();
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public boolean hasSameRules(final TimeZone other) {
120         return timeZone.hasSameRules(other);
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public boolean inDaylightTime(final Date date) {
128         return timeZone.inDaylightTime(date);
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public boolean observesDaylightTime() {
136         return timeZone.observesDaylightTime();
137     }
138 
139     /**
140      * Sets no time zone property and always throws {@link UnsupportedOperationException}.
141      */
142     @Override
143     public void setID(final String ID) {
144         throw new UnsupportedOperationException("This class is immutable.");
145     }
146 
147     /**
148      * Sets no time zone property and always throws {@link UnsupportedOperationException}.
149      */
150     @Override
151     public void setRawOffset(final int offsetMillis) {
152         throw new UnsupportedOperationException("This class is immutable.");
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public String toString() {
160         return timeZone.toString();
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public ZoneId toZoneId() {
168         return timeZone.toZoneId();
169     }
170 
171     TimeZone unwrap() {
172         return timeZone;
173     }
174 
175     @Override
176     public boolean useDaylightTime() {
177         return timeZone.useDaylightTime();
178     }
179 }