KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > util > ArrayMap


1 /*
2 Copyright (c) 2003-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.util;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33
34
35 /**
36  * Array with reverse mapping from values to indices. This operates as the
37  * combination of an array with ordinary int indices and a hashmap from values
38  * back to the corresponding index position. Values are assured to be unique.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public class ArrayMap
45 {
46     /** Array of values. */
47     private ArrayList JavaDoc m_array;
48     
49     /** Map from values to indices. */
50     private HashMap JavaDoc m_map;
51
52     /**
53      * Default constructor.
54      */

55     
56     public ArrayMap() {
57         m_array = new ArrayList JavaDoc();
58         m_map = new HashMap JavaDoc();
59     }
60
61     /**
62      * Constructor with initial capacity supplied.
63      *
64      * @param size initial capacity for array map
65      */

66     
67     public ArrayMap(int size) {
68         m_array = new ArrayList JavaDoc(size);
69         m_map = new HashMap JavaDoc(size);
70     }
71
72     /**
73      * Get value for index. The index must be within the valid range (from 0 to
74      * one less than the number of values present).
75      *
76      * @param index number to be looked up
77      * @return value at that index position
78      */

79     
80     public Object JavaDoc get(int index) {
81         return m_array.get(index);
82     }
83
84     /**
85      * Find existing object. If the supplied value object is present in the
86      * array map its index position is returned.
87      *
88      * @param obj value to be found
89      * @return index number assigned to value, or <code>-1</code> if not found
90      */

91     
92     public int find(Object JavaDoc obj) {
93         Integer JavaDoc index = (Integer JavaDoc)m_map.get(obj);
94         if (index == null) {
95             return -1;
96         } else {
97             return index.intValue();
98         }
99     }
100
101     /**
102      * Add object. If the supplied value object is already present in the array
103      * map the existing index position is returned.
104      *
105      * @param obj value to be added
106      * @return index number assigned to value
107      */

108     
109     public int findOrAdd(Object JavaDoc obj) {
110         Integer JavaDoc index = (Integer JavaDoc)m_map.get(obj);
111         if (index == null) {
112             index = IntegerCache.getInteger(m_array.size());
113             m_map.put(obj, index);
114             m_array.add(obj);
115         }
116         return index.intValue();
117     }
118
119     /**
120      * Get count of values present.
121      *
122      * @return number of values in array map
123      */

124     
125     public int size() {
126         return m_array.size();
127     }
128 }
129
Popular Tags