001: /*
002: Copyright (c) 2003-2005, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.binding.util;
030:
031: import java.util.ArrayList;
032: import java.util.HashMap;
033:
034: /**
035: * Array with reverse mapping from values to indices. This operates as the
036: * combination of an array with ordinary int indices and a hashmap from values
037: * back to the corresponding index position. Values are assured to be unique.
038: *
039: * @author Dennis M. Sosnoski
040: */
041: public class ArrayMap {
042: /** Array of values. */
043: private ArrayList m_array;
044:
045: /** Map from values to indices. */
046: private HashMap m_map;
047:
048: /**
049: * Default constructor.
050: */
051: public ArrayMap() {
052: m_array = new ArrayList();
053: m_map = new HashMap();
054: }
055:
056: /**
057: * Constructor with initial capacity supplied.
058: *
059: * @param size initial capacity for array map
060: */
061: public ArrayMap(int size) {
062: m_array = new ArrayList(size);
063: m_map = new HashMap(size);
064: }
065:
066: /**
067: * Get value for index. The index must be within the valid range (from 0 to
068: * one less than the number of values present).
069: *
070: * @param index number to be looked up
071: * @return value at that index position
072: */
073: public Object get(int index) {
074: return m_array.get(index);
075: }
076:
077: /**
078: * Find existing object. If the supplied value object is present in the
079: * array map its index position is returned.
080: *
081: * @param obj value to be found
082: * @return index number assigned to value, or <code>-1</code> if not found
083: */
084: public int find(Object obj) {
085: Integer index = (Integer) m_map.get(obj);
086: if (index == null) {
087: return -1;
088: } else {
089: return index.intValue();
090: }
091: }
092:
093: /**
094: * Add object. If the supplied value object is already present in the array
095: * map the existing index position is returned.
096: *
097: * @param obj value to be added
098: * @return index number assigned to value
099: */
100: public int findOrAdd(Object obj) {
101: Integer index = (Integer) m_map.get(obj);
102: if (index == null) {
103: index = IntegerCache.getInteger(m_array.size());
104: m_map.put(obj, index);
105: m_array.add(obj);
106: }
107: return index.intValue();
108: }
109:
110: /**
111: * Get count of values present.
112: *
113: * @return number of values in array map
114: */
115: public int size() {
116: return m_array.size();
117: }
118: }
|