2005-08-18 07:06:44 +00:00
|
|
|
|
|
|
|
|
/* ====================================================================
|
2006-12-22 19:18:16 +00:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
2005-08-18 07:06:44 +00:00
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.apache.poi.util;
|
|
|
|
|
|
2019-12-22 21:44:45 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.common.Duplicatable;
|
2005-08-18 07:06:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A List of objects that are indexed AND keyed by an int; also allows for getting
|
|
|
|
|
* the index of a value in the list
|
2019-12-22 21:44:45 +00:00
|
|
|
* <p>
|
|
|
|
|
* I am happy is someone wants to re-implement this without using the
|
2005-08-18 07:06:44 +00:00
|
|
|
* internal list and hashmap. If so could you please make sure that
|
|
|
|
|
* you can add elements half way into the list and have the value-key mappings
|
2019-12-22 21:44:45 +00:00
|
|
|
* update
|
2005-08-18 07:06:44 +00:00
|
|
|
*/
|
|
|
|
|
|
2019-12-22 21:44:45 +00:00
|
|
|
public class IntMapper<T> implements Duplicatable {
|
|
|
|
|
private final List<T> elements;
|
|
|
|
|
private final Map<T, Integer> valueKeyMap;
|
2005-08-18 07:06:44 +00:00
|
|
|
|
2019-12-22 21:44:45 +00:00
|
|
|
private static final int _default_size = 10;
|
2005-08-18 07:06:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create an IntMapper of default size
|
|
|
|
|
*/
|
2019-12-22 21:44:45 +00:00
|
|
|
public IntMapper() {
|
2005-08-18 07:06:44 +00:00
|
|
|
this(_default_size);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 21:44:45 +00:00
|
|
|
public IntMapper(final int initialCapacity) {
|
2017-09-16 08:27:23 +00:00
|
|
|
elements = new ArrayList<>(initialCapacity);
|
|
|
|
|
valueKeyMap = new HashMap<>(initialCapacity);
|
2005-08-18 07:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-22 21:44:45 +00:00
|
|
|
public IntMapper(IntMapper<T> other) {
|
|
|
|
|
elements = new ArrayList<>(other.elements);
|
|
|
|
|
valueKeyMap = new HashMap<>(other.valueKeyMap);
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-18 07:06:44 +00:00
|
|
|
/**
|
|
|
|
|
* Appends the specified element to the end of this list
|
|
|
|
|
*
|
|
|
|
|
* @param value element to be appended to this list.
|
|
|
|
|
* @return true (as per the general contract of the Collection.add
|
2019-12-22 21:44:45 +00:00
|
|
|
* method).
|
2005-08-18 07:06:44 +00:00
|
|
|
*/
|
2019-12-22 21:44:45 +00:00
|
|
|
public boolean add(final T value) {
|
|
|
|
|
int index = elements.size();
|
|
|
|
|
elements.add(value);
|
|
|
|
|
valueKeyMap.put(value, index);
|
|
|
|
|
return true;
|
2005-08-18 07:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int size() {
|
2019-12-22 21:44:45 +00:00
|
|
|
return elements.size();
|
2005-08-18 07:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-18 12:56:18 +00:00
|
|
|
public T get(int index) {
|
2019-12-22 21:44:45 +00:00
|
|
|
return elements.get(index);
|
2005-08-18 07:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-18 12:56:18 +00:00
|
|
|
public int getIndex(T o) {
|
2019-12-22 21:44:45 +00:00
|
|
|
return valueKeyMap.getOrDefault(o, -1);
|
2005-08-18 07:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-18 12:56:18 +00:00
|
|
|
public Iterator<T> iterator() {
|
2019-12-22 21:44:45 +00:00
|
|
|
return elements.iterator();
|
2005-08-18 07:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-22 21:44:45 +00:00
|
|
|
@Override
|
|
|
|
|
public IntMapper<T> copy() {
|
|
|
|
|
return new IntMapper<>(this);
|
|
|
|
|
}
|
2020-04-12 22:03:52 +00:00
|
|
|
|
|
|
|
|
public List<T> getElements() {
|
|
|
|
|
return elements;
|
|
|
|
|
}
|
2019-12-22 21:44:45 +00:00
|
|
|
}
|