KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > ColumnsMap


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.Map JavaDoc;
19
20 /**
21  * Represents a map of columns accessible by name and index.
22  * <p>This class is useful for queries producing results similar to ResultSet.
23  *
24  * @author Fyodor Kupolov
25  * @version 1.0
26  */

27 public class ColumnsMap {
28     private Map JavaDoc<String JavaDoc, Integer JavaDoc> map;
29
30     /**
31      * Registers column to for later lookup.
32      *
33      * @param name column name.
34      * @param index positive column index.
35      * @throws IllegalArgumentException if index has illegal value
36      */

37     public void registerColumn(String JavaDoc name, int index) throws IllegalArgumentException JavaDoc {
38         if (index <= 0) {
39             throw new IllegalArgumentException JavaDoc("Index must be positive integer");
40         }
41         if (map == null) {
42             map = CollectionUtils.newCaseInsensitiveAsciiMap();
43         }
44         map.put(name, index);
45     }
46
47     /**
48      * Finds column index by name.
49      *
50      * @param name column name.
51      * @return column index, or null if column not found.
52      */

53     public Integer JavaDoc find(String JavaDoc name) {
54         if (name == null) {
55             throw new IllegalArgumentException JavaDoc("Parameter name cannot be null");
56         }
57         Integer JavaDoc index = map == null ? null : map.get(name);
58         //If name is not a column name and is integer
59
if (index == null && StringUtils.isDecimalInt(name)) {
60             index = Integer.valueOf(name); //Try to parse name as index
61
}
62         return index;
63     }
64
65
66 }
67
Popular Tags