001    /*
002     *  Copyright 2006 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.contrib.i18n;
017    
018    import java.util.Collections;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.joda.time.DateTimeZone;
023    
024    /**
025     * Provides time data for a specific territory, typically a country.
026     * <p>
027     * Many pieces of data used in dates and times varies by territory.
028     * This class provides access to that data.
029     */
030    public abstract class Territory {
031    
032    //    /** An empty chronology array. */
033    //    private static final Chronology[] EMPTY_CHRONOLOGY_ARRAY = new Chronology[0];
034        /** A cache of territories. */
035        private static final Map cTerritoryMap = Collections.synchronizedMap(new HashMap());
036    
037        //-----------------------------------------------------------------------
038        /**
039         * Gets a territory instance for the specified id.
040         * <p>
041         * The territory id must be one of those returned by getAvailableIDs.
042         *
043         * @param id  the ID of the territory, not null
044         * @return the territory object for the ID
045         * @throws IllegalArgumentException if the ID is not recognised
046         */
047        public static Territory forID(String id) {
048            if (id != null && id.length() == 2) {
049                Territory t = (Territory) cTerritoryMap.get(id);
050                if (t == null) {
051                    t = new CLDRTerritory(id);
052                    cTerritoryMap.put(id, t);
053                }
054                return t;
055            }
056            throw new IllegalArgumentException("The territory id is not recognised: " + id);
057        }
058    
059        //-----------------------------------------------------------------------
060        /**
061         * Constructor.
062         *
063         * @param id  the territory id, not null
064         */
065        protected Territory() {
066            super();
067        }
068    
069        //-----------------------------------------------------------------------
070        /**
071         * Gets the territory id.
072         *
073         * @return the territory id, never null
074         */
075        public abstract String getID();
076    
077        //-----------------------------------------------------------------------
078        /**
079         * Gets the time zones applicable for the territory.
080         *
081         * @return the array of zones, never null
082         */
083            public abstract DateTimeZone[] getZones();
084    
085        /**
086         * Gets the time zone for the territory, selecting the zone of the most
087         * important city (the capital) if there are multiple zones.
088         *
089         * @return the zone that best represents the territory, null if unknown
090         */
091        public DateTimeZone getZone() {
092            DateTimeZone[] zones = getZones();
093            if (zones.length == 0) {
094                return null;
095            }
096            return zones[0];
097        }
098    
099    //    //-----------------------------------------------------------------------
100    //    /**
101    //     * Gets the altenate non-ISO chronologies used in the territory.
102    //     * For example, countries in the middle east would include
103    //     * IslamicChronology in the result.
104    //     *
105    //     * @return the non-ISO chronologies, empty array if none
106    //     */
107    //    public Chronology[] getChronologies() {
108    //        return EMPTY_CHRONOLOGY_ARRAY;
109    //    }
110    
111        //-----------------------------------------------------------------------
112        /**
113         * Gets the first day of the week.
114         * The value is expressed using ISO values (1=Mon,7=Sun).
115         *
116         * @return the first day of the week
117         */
118        public abstract int getFirstDayOfWeek();
119    
120        /**
121         * Gets the day of week that the business week starts.
122         * The value is expressed using ISO values (1=Mon,7=Sun).
123         *
124         * @return the day of week that the business week starts
125         */
126        public abstract int getBusinessWeekStart();
127    
128        /**
129         * Gets the day of week that the business week ends.
130         * The value is expressed using ISO values (1=Mon,7=Sun).
131         *
132         * @return the day of week that the business week ends
133         */
134        public abstract int getBusinessWeekEnd();
135    
136        /**
137         * Gets the day of week that the weekend starts.
138         * The value is expressed using ISO values (1=Mon,7=Sun).
139         *
140         * @return the day of week that the weekend starts
141         */
142        public abstract int getWeekendStart();
143    
144        /**
145         * Gets the day of week that the weekend ends.
146         * The value is expressed using ISO values (1=Mon,7=Sun).
147         *
148         * @return the day of week that the weekend ends
149         */
150        public abstract int getWeekendEnd();
151    
152        //-----------------------------------------------------------------------
153        /**
154         * Is this territory equal (by id and class) to another.
155         *
156         * @param other  the other object to compare to
157         * @return trur if equal
158         */
159        public boolean equals(Object other) {
160            if (other == this) {
161                return true;
162            }
163            if (other.getClass() != getClass()) {
164                return false;
165            }
166            return ((Territory) other).getID().equals(getID());
167        }
168    
169        /**
170         * Gets a suitable hashcode for this territory.
171         *
172         * @return a hashcode
173         */
174        public int hashCode() {
175            return 19 * getClass().hashCode() + getID().hashCode();
176        }
177    
178        /**
179         * Outputs a string vesion of the territory.
180         *
181         * @return string
182         */
183        public String toString() {
184            return "Territory[" + getID() + "]";
185        }
186    
187    }