001    /*
002     *  Copyright 2001-2005 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time.tz;
017    
018    import org.joda.time.DateTimeZone;
019    
020    /**
021     * Basic DateTimeZone implementation that has a fixed name key and offsets.
022     * <p>
023     * FixedDateTimeZone is thread-safe and immutable.
024     * 
025     * @author Brian S O'Neill
026     * @since 1.0
027     */
028    public final class FixedDateTimeZone extends DateTimeZone {
029    
030        private static final long serialVersionUID = -3513011772763289092L;
031    
032        private final String iNameKey;
033        private final int iWallOffset;
034        private final int iStandardOffset;
035    
036        public FixedDateTimeZone(String id, String nameKey,
037                                 int wallOffset, int standardOffset) {
038            super(id);
039            iNameKey = nameKey;
040            iWallOffset = wallOffset;
041            iStandardOffset = standardOffset;
042        }
043    
044        public String getNameKey(long instant) {
045            return iNameKey;
046        }
047    
048        public int getOffset(long instant) {
049            return iWallOffset;
050        }
051    
052        public int getStandardOffset(long instant) {
053            return iStandardOffset;
054        }
055    
056        public int getOffsetFromLocal(long instantLocal) {
057            return iWallOffset;
058        }
059    
060        public boolean isFixed() {
061            return true;
062        }
063    
064        public long nextTransition(long instant) {
065            return instant;
066        }
067    
068        public long previousTransition(long instant) {
069            return instant;
070        }
071    
072        /**
073         * Override to return the correct timzone instance.
074         * @since 1.5
075         */
076        public java.util.TimeZone toTimeZone() {
077            String id = getID();
078            if (id.length() == 6 && (id.startsWith("+") || id.startsWith("-"))) {
079                // standard format offset [+-]hh:mm
080                // our ID is without any prefix, so we need to add the GMT back
081                return java.util.TimeZone.getTimeZone("GMT" + getID());
082            }
083            // unusual offset, so setup a SimpleTimeZone as best we can
084            return new java.util.SimpleTimeZone(iWallOffset, getID());
085        }
086    
087        public boolean equals(Object obj) {
088            if (this == obj) {
089                return true;
090            }
091            if (obj instanceof FixedDateTimeZone) {
092                FixedDateTimeZone other = (FixedDateTimeZone) obj;
093                return
094                    getID().equals(other.getID()) &&
095                    iStandardOffset == other.iStandardOffset &&
096                    iWallOffset == other.iWallOffset;
097            }
098            return false;
099        }
100    
101        public int hashCode() {
102            return getID().hashCode() + 37 * iStandardOffset + 31 * iWallOffset;
103        }
104    
105    }