KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > CollectionUtils


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  */

16 package scriptella.util;
17
18 import java.util.Comparator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.util.TreeMap JavaDoc;
22
23 /**
24  * Collections utility methods.
25  *
26  * @author Fyodor Kupolov
27  * @version 1.0
28  */

29 public final class CollectionUtils {
30     private CollectionUtils() {//Singleton
31
}
32
33     /**
34      * Comparator similar to {@link String#CASE_INSENSITIVE_ORDER}, but
35      * handles only ASCII characters
36      */

37     private static final Comparator JavaDoc<String JavaDoc> ASCII_CASE_INSENSITIVE_ORDER = new Comparator JavaDoc<String JavaDoc>() {
38         public int compare(String JavaDoc s1, String JavaDoc s2) {
39             int n1=s1.length(), n2=s2.length();
40             int n=n1<n2?n1:n2;
41             for (int i=0; i<n; i++) {
42                 char c1 = s1.charAt(i);
43                 char c2 = s2.charAt(i);
44                 if (c1 != c2) {
45                     if (c1>='A' && c1<='Z') { //Fast lower case
46
c1=(char)(c1|0x20);
47                     }
48                     if (c2>='A' && c2<='Z') {
49                         c2=(char)(c2|0x20);
50                     }
51                     if (c1 != c2) {
52                         return c1 - c2;
53                     }
54                 }
55             }
56             return n1 - n2;
57         }
58     };
59
60     /**
61      * Create a map optimized for case insensitive search for keys.
62      * The case insensitive rules are simplified to ASCII chars for performance reasons.
63      *
64      * @return case insensitive map.
65      */

66     public static <V> Map JavaDoc<String JavaDoc, V> newCaseInsensitiveAsciiMap() {
67         return new TreeMap JavaDoc<String JavaDoc, V>(ASCII_CASE_INSENSITIVE_ORDER);
68     }
69
70     /**
71      * Returns parameterized version of {@link Properties} the instance
72      * remains the same.
73      * @param properties properties to represent as a map.
74      */

75     @SuppressWarnings JavaDoc("unchecked")
76     public static Map JavaDoc<String JavaDoc,String JavaDoc> asMap(Properties JavaDoc properties) {
77         return (Map JavaDoc)properties;
78     }
79 }
80
Popular Tags