You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntMapper.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import org.apache.poi.common.Duplicatable;
  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. * <p>
  26. * 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
  30. */
  31. public class IntMapper<T> implements Duplicatable {
  32. private final List<T> elements;
  33. private final Map<T, Integer> valueKeyMap;
  34. private static final int _default_size = 10;
  35. /**
  36. * create an IntMapper of default size
  37. */
  38. public IntMapper() {
  39. this(_default_size);
  40. }
  41. public IntMapper(final int initialCapacity) {
  42. elements = new ArrayList<>(initialCapacity);
  43. valueKeyMap = new HashMap<>(initialCapacity);
  44. }
  45. public IntMapper(IntMapper<T> other) {
  46. elements = new ArrayList<>(other.elements);
  47. valueKeyMap = new HashMap<>(other.valueKeyMap);
  48. }
  49. /**
  50. * Appends the specified element to the end of this list
  51. *
  52. * @param value element to be appended to this list.
  53. * @return true (as per the general contract of the Collection.add
  54. * method).
  55. */
  56. public boolean add(final T value) {
  57. int index = elements.size();
  58. elements.add(value);
  59. valueKeyMap.put(value, index);
  60. return true;
  61. }
  62. public int size() {
  63. return elements.size();
  64. }
  65. public T get(int index) {
  66. return elements.get(index);
  67. }
  68. public int getIndex(T o) {
  69. return valueKeyMap.getOrDefault(o, -1);
  70. }
  71. public Iterator<T> iterator() {
  72. return elements.iterator();
  73. }
  74. @Override
  75. public IntMapper<T> copy() {
  76. return new IntMapper<>(this);
  77. }
  78. public List<T> getElements() {
  79. return elements;
  80. }
  81. }