01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.util;
19:
20: import java.util.*;
21:
22: /**
23: * A List of objects that are indexed AND keyed by an int; also allows for getting
24: * the index of a value in the list
25: *
26: * <p>I am happy is someone wants to re-implement this without using the
27: * internal list and hashmap. If so could you please make sure that
28: * you can add elements half way into the list and have the value-key mappings
29: * update</p>
30: *
31: *
32: * @author Jason Height
33: */
34:
35: public class IntMapper {
36: private List elements;
37: private Map valueKeyMap;
38:
39: private static final int _default_size = 10;
40:
41: /**
42: * create an IntMapper of default size
43: */
44:
45: public IntMapper() {
46: this (_default_size);
47: }
48:
49: public IntMapper(final int initialCapacity) {
50: elements = new ArrayList(initialCapacity);
51: valueKeyMap = new HashMap(initialCapacity);
52: }
53:
54: /**
55: * Appends the specified element to the end of this list
56: *
57: * @param value element to be appended to this list.
58: *
59: * @return true (as per the general contract of the Collection.add
60: * method).
61: */
62:
63: public boolean add(final Object value) {
64: int index = elements.size();
65: elements.add(value);
66: valueKeyMap.put(value, new Integer(index));
67: return true;
68: }
69:
70: public int size() {
71: return elements.size();
72: }
73:
74: public Object get(int index) {
75: return elements.get(index);
76: }
77:
78: public int getIndex(Object o) {
79: Integer i = ((Integer) valueKeyMap.get(o));
80: if (i == null)
81: return -1;
82: return i.intValue();
83: }
84:
85: public Iterator iterator() {
86: return elements.iterator();
87: }
88: } // end public class IntMapper
|