001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.list.util;
016:
017: import java.util.Comparator;
018: import java.util.Locale;
019: import org.araneaframework.uilib.list.util.comparator.BooleanComparator;
020: import org.araneaframework.uilib.list.util.comparator.ComparableComparator;
021: import org.araneaframework.uilib.list.util.comparator.IgnoreCaseComparator;
022: import org.araneaframework.uilib.list.util.comparator.LocaleStringComparator;
023: import org.araneaframework.uilib.list.util.comparator.NullComparator;
024:
025: /**
026: * Factory class for constructing <code>Comparator</code> objects.
027: */
028: public class ComparatorFactory {
029:
030: /**
031: * Whether the <code>null</code> value should be ordered before not-null
032: * values by default.
033: */
034: public static final boolean NULL_FIRST_BY_DEFAULT = true;
035:
036: /**
037: * Whether the <code>String</code> objects <code>Comparator</code>
038: * should be case insensitve by default.
039: */
040: public static final boolean IGNORE_CASE_BY_DEFAULT = true;
041:
042: /**
043: * Whether the <code>Boolean</code> objects <code>Comparator</code>
044: * should order the <code>true</code> value before the <code>false</code>
045: * value by default.
046: */
047: public static final boolean TRUE_FIRST_BY_DEFAULT = true;
048:
049: /**
050: * Returns default <code>Comparator</code> for comparing all objects
051: * including <code>null</code> references.
052: *
053: * @return default <code>Comparator</code> for comparing all objects
054: * including <code>null</code> references.
055: */
056: public static Comparator getDefault() {
057: return getDefault(NULL_FIRST_BY_DEFAULT);
058: }
059:
060: /**
061: * Returns default <code>Comparator</code> for comparing all objects
062: * including <code>null</code> values.
063: *
064: * @param nullFirst
065: * whether the <code>null</code> value is ordered before
066: * not-null values.
067: * @return default <code>Comparator</code> for comparing all objects
068: * including <code>null</code> references.
069: */
070: public static Comparator getDefault(boolean nullFirst) {
071: return new NullComparator(ComparableComparator.INSTANCE,
072: nullFirst);
073: }
074:
075: /**
076: * Returns the <code>Comparator</code> for <code>String</code> objects
077: * including <code>null</code> values.
078: *
079: * @param nullFirst
080: * whether the <code>null</code> value is ordered before
081: * not-null values.
082: * @param ignoreCase
083: * whether the Comparator should be case insensitive.
084: * @param locale
085: * the <code>Locale</code> of the Comparator or
086: * <code>null</code> if the Comparator should not be
087: * Locale-specific.
088: * @return the <code>Comparator</code> for <code>String</code> objects
089: * including <code>null</code> values.
090: */
091: public static Comparator getStringComparator(boolean nullFirst,
092: boolean ignoreCase, Locale locale) {
093: return new NullComparator(getNotNullStringComparator(
094: ignoreCase, locale), nullFirst);
095: }
096:
097: /**
098: * Returns the <code>Comparator</code> for <code>String</code> objects
099: * excluding <code>null</code> values.
100: *
101: * @param ignoreCase
102: * whether the Comparator should be case insensitive.
103: * @param locale
104: * the <code>Locale</code> of the Comparator or
105: * <code>null</code> if the Comparator should not be
106: * Locale-specific.
107: * @return the <code>Comparator</code> for <code>String</code> objects
108: * excluding <code>null</code> values.
109: */
110: protected static Comparator getNotNullStringComparator(
111: boolean ignoreCase, Locale locale) {
112: if (locale != null) {
113: return new LocaleStringComparator(ignoreCase, locale);
114: }
115: if (ignoreCase) {
116: return IgnoreCaseComparator.INSTANCE;
117: }
118: return ComparableComparator.INSTANCE;
119: }
120:
121: /**
122: * Returns the <code>Comparator</code> for <code>Boolean</code> objects
123: * including <code>null</code> values.
124: *
125: * @param nullFirst
126: * whether the <code>null</code> value is ordered before
127: * not-null values.
128: * @param trueFirst
129: * whether the <code>true</code> value should be ordered before
130: * the <code>false</code> value.
131: * @return the <code>Comparator</code> for <code>Boolean</code> objects
132: * including <code>null</code> values.
133: */
134: public static Comparator getBooleanComparator(boolean nullFirst,
135: boolean trueFirst) {
136: return new NullComparator(
137: trueFirst ? BooleanComparator.TRUE_FIRST
138: : BooleanComparator.FALSE_FIRST, nullFirst);
139: }
140: }
|