aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/util/Locale.java
blob: e47cd1d59d749eafcb57c4bb0f1c8f2204f0cff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright (C) 1998, 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.util;

/**
 * @author Per Bothner <bothner@cygnus.com>
 * @date October 24, 1998.
 */

/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3,
 * and "The Java Language Specification", ISBN 0-201-63451-1.
 * Status:  None of the getDisplayXXX or getISO3XXX methods are implemented.
 */
public final class Locale implements java.io.Serializable, Cloneable
{
  // The fields are as specified in Sun's "Serialized Form"
  // in the JDK 1.2 beta 4 API specification.
  private String country;
  private int hashcode;
  private String language;
  private String variant;
  private static Locale defaultLocale;

  // FIXME: many are still missing.
  public static final Locale CANADA = new Locale ("en", "CA");
  public static final Locale FRANCE = new Locale ("fr", "FR");
  public static final Locale JAPAN = new Locale ("ja", "JP");
  public static final Locale UK = new Locale ("en", "GB");
  public static final Locale US = new Locale ("en", "US");

  public Locale (String languageCode, String countryCode)
  {
    language = languageCode.toLowerCase();
    country = countryCode.toUpperCase();
    hashcode = languageCode.hashCode() ^ countryCode.hashCode();
  }

  public Locale (String languageCode, String countryCode,
		 String variantCode)
  {
    this (languageCode, countryCode);
    variant = variantCode;
    hashcode ^= variantCode.hashCode();
  }

  public Object clone ()
    {
      return (Object) new Locale (language, country, variant);
    }

  public boolean equals (Object obj)
    {
      if (! (obj instanceof Locale))
	return false;
      Locale loc = (Locale) obj;
      if ((language == null && loc.language != null)
	  || (country == null && loc.country != null)
	  || (variant == null && loc.variant != null))
	return false;
      return (language.equals(loc.language)
	      && country.equals(loc.country)
	      && variant.equals(loc.variant));
    }

  public String getCountry ()
  {
    return country;
  }

  public String getLanguage ()
  {
    return language;
  }

  public String getVariant ()
  {
    return variant;
  }

  public int hashCode ()
  {
    return hashcode;
  }

  private static synchronized Locale setDefault()
  {
    if (defaultLocale != null)
      return defaultLocale;
    String language = System.getProperty("user.language");
    String country = System.getProperty("user.region");
    defaultLocale = new Locale (language == null ? "en" : language,
				country == null ? "" : country);
    return defaultLocale;
  }

  public static Locale getDefault ()
  {
    return defaultLocale == null ? setDefault() : defaultLocale;
  }

  public static void setDefault (Locale newLocale)
  {
    defaultLocale = newLocale;
  }

  public String toString ()
  {
    StringBuffer result = new StringBuffer(20);
    result.append(language);
    result.append('_');
    result.append(country);
    if (variant != null && variant.length() > 0)
      {
	result.append('_');
	result.append(variant);
      }
    return result.toString();
  }
}