Skip to main content
summaryrefslogtreecommitdiffstats
blob: d2d071a5c3bbac62e773a004050a979d2ffd9846 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011, 2013 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.comparator;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import org.eclipse.jpt.common.utility.internal.ObjectTools;

/**
 * This collator simply wraps a Java text collator and implements a
 * {@link String} {@link Comparator} (instead of an {@link Object}
 * {@link Comparator}, which is what {@link Collator} does, possibly for
 * backward-compatibility reasons(?)).
 * 
 * @see Collator
 */
public class StringCollator
	implements Comparator<String>
{
	private final Collator collator;


	/**
	 * Wrap the default collator.
	 * @see Collator#getInstance()
	 */
	public StringCollator() {
		this(Collator.getInstance());
	}

	/**
	 * Wrap the collator for the specified locale.
	 * @see Collator#getInstance(Locale)
	 */
	public StringCollator(Locale locale) {
		this(Collator.getInstance(locale));
	}

	/**
	 * Wrap the specified collator.
	 */
	public StringCollator(Collator collator) {
		super();
		if (collator == null) {
			throw new NullPointerException();
		}
		this.collator = collator;
	}

	public int compare(String string1, String string2) {
		return this.collator.compare(string1, string2);
	}

	@Override
	public String toString() {
		return ObjectTools.toString(this, this.collator);
	}
}

Back to the top