1 |
|
---|
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
|
---|
9 |
|
---|
10 | http://www.apache.org/licenses/LICENSE-2.0
|
---|
11 |
|
---|
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 | ==================================================================== */
|
---|
18 |
|
---|
19 |
|
---|
20 | package org.apache.poi.util;
|
---|
21 |
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.HashMap;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.Map;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A List of objects that are indexed AND keyed by an int; also allows for getting
|
---|
29 | * the index of a value in the list
|
---|
30 | *
|
---|
31 | * <p>I am happy is someone wants to re-implement this without using the
|
---|
32 | * internal list and hashmap. If so could you please make sure that
|
---|
33 | * you can add elements half way into the list and have the value-key mappings
|
---|
34 | * update</p>
|
---|
35 | *
|
---|
36 | *
|
---|
37 | * @author Jason Height
|
---|
38 | */
|
---|
39 |
|
---|
40 | public class IntMapper<T>
|
---|
41 | {
|
---|
42 | private List<T> elements;
|
---|
43 | private Map<T,Integer> valueKeyMap;
|
---|
44 |
|
---|
45 | private static final int _default_size = 10;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * create an IntMapper of default size
|
---|
49 | */
|
---|
50 |
|
---|
51 | public IntMapper()
|
---|
52 | {
|
---|
53 | this(_default_size);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public IntMapper(final int initialCapacity)
|
---|
57 | {
|
---|
58 | elements = new ArrayList<T>(initialCapacity);
|
---|
59 | valueKeyMap = new HashMap<T,Integer>(initialCapacity);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Appends the specified element to the end of this list
|
---|
64 | *
|
---|
65 | * @param value element to be appended to this list.
|
---|
66 | *
|
---|
67 | * @return true (as per the general contract of the Collection.add
|
---|
68 | * method).
|
---|
69 | */
|
---|
70 | public boolean add(final T value)
|
---|
71 | {
|
---|
72 | int index = elements.size();
|
---|
73 | elements.add(value);
|
---|
74 | valueKeyMap.put(value, index);
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public int size() {
|
---|
79 | return elements.size();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public T get(int index) {
|
---|
83 | return elements.get(index);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public int getIndex(T o) {
|
---|
87 | Integer i = valueKeyMap.get(o);
|
---|
88 | if (i == null)
|
---|
89 | return -1;
|
---|
90 | return i.intValue();
|
---|
91 | }
|
---|
92 |
|
---|
93 | } // end public class IntMapper
|
---|
94 |
|
---|