Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / util / IntMapper.java
blob89ae28c63105ba53ca3437b9f2a492f3477d67cd
2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
20 package org.apache.poi.util;
22 import java.util.*;
24 /**
25 * A List of objects that are indexed AND keyed by an int; also allows for getting
26 * the index of a value in the list
28 * <p>I am happy is someone wants to re-implement this without using the
29 * internal list and hashmap. If so could you please make sure that
30 * you can add elements half way into the list and have the value-key mappings
31 * update</p>
34 * @author Jason Height
37 public class IntMapper
39 private List elements;
40 private Map valueKeyMap;
42 private static final int _default_size = 10;
44 /**
45 * create an IntMapper of default size
48 public IntMapper()
50 this(_default_size);
53 public IntMapper(final int initialCapacity)
55 elements = new ArrayList(initialCapacity);
56 valueKeyMap = new HashMap(initialCapacity);
59 /**
60 * Appends the specified element to the end of this list
62 * @param value element to be appended to this list.
64 * @return true (as per the general contract of the Collection.add
65 * method).
68 public boolean add(final Object value)
70 int index = elements.size();
71 elements.add(value);
72 valueKeyMap.put(value, new Integer(index));
73 return true;
76 public int size() {
77 return elements.size();
80 public Object get(int index) {
81 return elements.get(index);
84 public int getIndex(Object o) {
85 Integer i = ((Integer)valueKeyMap.get(o));
86 if (i == null)
87 return -1;
88 return i.intValue();
91 public Iterator iterator() {
92 return elements.iterator();
94 } // end public class IntMapper