01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.uilib.list.util.comparator;
16:
17: import java.io.Serializable;
18: import java.text.Collator;
19: import java.util.Locale;
20: import org.apache.commons.lang.builder.HashCodeBuilder;
21: import org.araneaframework.core.Assert;
22:
23: /**
24: * Not-null comparator that compares <code>String</code> values according to
25: * the <code>Locale</code> and case sensitive option.
26: */
27: public class LocaleStringComparator implements StringComparator,
28: Serializable {
29: private boolean ignoreCase;
30: private Locale locale;
31:
32: private transient Collator collator;
33:
34: public LocaleStringComparator(boolean ignoreCase, Locale locale) {
35: Assert.notNullParam(locale, "locale");
36:
37: this .ignoreCase = ignoreCase;
38: this .locale = locale;
39: }
40:
41: public boolean getIgnoreCase() {
42: return this .ignoreCase;
43: }
44:
45: public Locale getLocale() {
46: return this .locale;
47: }
48:
49: public int compare(Object o1, Object o2) {
50: return getCollator().compare(o1, o2);
51: }
52:
53: private Collator getCollator() {
54: if (this .collator == null) {
55: this .collator = Collator.getInstance(locale);
56: this .collator.setStrength(ignoreCase ? Collator.SECONDARY
57: : Collator.TERTIARY);
58: }
59: return this .collator;
60: }
61:
62: public boolean equals(Object obj) {
63: if (obj instanceof LocaleStringComparator == false) {
64: return false;
65: }
66:
67: if (this == obj) {
68: return true;
69: }
70:
71: LocaleStringComparator rhs = (LocaleStringComparator) obj;
72: return ignoreCase == rhs.ignoreCase
73: && locale.equals(rhs.locale);
74: }
75:
76: public int hashCode() {
77: return new HashCodeBuilder(20070327, 1221).append(ignoreCase)
78: .append(locale).toHashCode();
79: }
80: }
|