aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/text/Collator.java
blob: 30b746ef5990ea223feec36a5c53e89dc8105c02 (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
126
127
128
129
130
131
132
133
134
135
// Collator.java - Locale-sensitive string comparison.

/* Copyright (C) 1999, 2000  Free Software Foundation

   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.text;

import java.io.Serializable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date March 18, 1999
 */
/* Written using "Java Class Libraries", 2nd edition, plus online
 * API docs for JDK 1.2 from http://www.javasoft.com.
 * Status: Mostly complete, but parts stubbed out.  Look for FIXME.
 */

public abstract class Collator implements Cloneable, Serializable
{
  public static final int NO_DECOMPOSITION = 0;
  public static final int CANONICAL_DECOMPOSITION = 1;
  public static final int FULL_DECOMPOSITION = 2;

  public static final int PRIMARY = 0;
  public static final int SECONDARY = 1;
  public static final int TERTIARY = 2;
  public static final int IDENTICAL = 3;

  protected Collator ()
  {
    strength = TERTIARY;
    decmp = CANONICAL_DECOMPOSITION;
  }

  public abstract int compare (String source, String target);

  public boolean equals (Object obj)
  {
    if (! (obj instanceof Collator))
      return false;
    Collator c = (Collator) obj;
    return decmp == c.decmp && strength == c.strength;
  }

  public boolean equals (String source, String target)
  {
    return compare (source, target) == 0;
  }

  public Object clone ()
  {
    return super.clone ();
  }

  public static synchronized Locale[] getAvailableLocales ()
  {
    // FIXME.
    return null;
  }

  public abstract CollationKey getCollationKey (String source);

  public synchronized int getDecomposition ()
  {
    return decmp;
  }

  public static Collator getInstance ()
  {
    return getInstance (Locale.getDefault());
  }

  public static Collator getInstance (Locale loc)
  {
    ResourceBundle res;
    String pattern;
    try
      {
	res = ResourceBundle.getBundle("gnu.gcj.text.LocaleData", loc);
	pattern = res.getString("collatorRule");
      }
    catch (MissingResourceException x)
      {
	return null;
      }
    try
      {
	return new RuleBasedCollator (pattern);
      }
    catch (ParseException x)
      {
	return null;
      }
  }

  public synchronized int getStrength ()
  {
    return strength;
  }

  public abstract int hashCode ();

  public synchronized void setDecomposition (int mode)
  {
    if (mode != NO_DECOMPOSITION
	&& mode != CANONICAL_DECOMPOSITION
	&& mode != FULL_DECOMPOSITION)
      throw new IllegalArgumentException ();
    decmp = mode;
  }

  public synchronized void setStrength (int strength)
  {
    if (strength != PRIMARY && strength != SECONDARY
	&& strength != TERTIARY && strength != IDENTICAL)
      throw new IllegalArgumentException ();
    this.strength = strength;
  }

  // Decompose a single character and append results to the buffer.
  protected native final void decomposeCharacter (char c, StringBuffer buf);

  // These names are fixed by the serialization spec.
  protected int decmp;
  protected int strength;
}