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 3.1KB

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