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.6KB

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