View Javadoc

1   /*
2    *  Copyright 2006 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.contrib.i18n;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.joda.time.DateTimeZone;
23  
24  /**
25   * Provides time data for a specific territory, typically a country.
26   * <p>
27   * Many pieces of data used in dates and times varies by territory.
28   * This class provides access to that data.
29   */
30  public abstract class Territory {
31  
32  //    /** An empty chronology array. */
33  //    private static final Chronology[] EMPTY_CHRONOLOGY_ARRAY = new Chronology[0];
34      /** A cache of territories. */
35      private static final Map cTerritoryMap = Collections.synchronizedMap(new HashMap());
36  
37      //-----------------------------------------------------------------------
38      /**
39       * Gets a territory instance for the specified id.
40       * <p>
41       * The territory id must be one of those returned by getAvailableIDs.
42       *
43       * @param id  the ID of the territory, not null
44       * @return the territory object for the ID
45       * @throws IllegalArgumentException if the ID is not recognised
46       */
47      public static Territory forID(String id) {
48          if (id != null && id.length() == 2) {
49              Territory t = (Territory) cTerritoryMap.get(id);
50              if (t == null) {
51                  t = new CLDRTerritory(id);
52                  cTerritoryMap.put(id, t);
53              }
54              return t;
55          }
56          throw new IllegalArgumentException("The territory id is not recognised: " + id);
57      }
58  
59      //-----------------------------------------------------------------------
60      /**
61       * Constructor.
62       *
63       * @param id  the territory id, not null
64       */
65      protected Territory() {
66          super();
67      }
68  
69      //-----------------------------------------------------------------------
70      /**
71       * Gets the territory id.
72       *
73       * @return the territory id, never null
74       */
75      public abstract String getID();
76  
77      //-----------------------------------------------------------------------
78      /**
79       * Gets the time zones applicable for the territory.
80       *
81       * @return the array of zones, never null
82       */
83  	public abstract DateTimeZone[] getZones();
84  
85      /**
86       * Gets the time zone for the territory, selecting the zone of the most
87       * important city (the capital) if there are multiple zones.
88       *
89       * @return the zone that best represents the territory, null if unknown
90       */
91      public DateTimeZone getZone() {
92          DateTimeZone[] zones = getZones();
93          if (zones.length == 0) {
94              return null;
95          }
96          return zones[0];
97      }
98  
99  //    //-----------------------------------------------------------------------
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 }