Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DoubleMap.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import java.util.ArrayList;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. /**
  28. * An object that maps keys to values, and values back to keys. Currently
  29. * does no checking for duplicates. Does not allow null values.
  30. *
  31. * @param <A> The first type of data to be mapped
  32. * @param <B> The second type of data to be mapped
  33. * @author chris
  34. */
  35. public class DoubleMap<A,B> {
  36. /** The keys in this map. */
  37. protected final List<A> keys = new ArrayList<A>();
  38. /** The values in this map. */
  39. protected final List<B> values = new ArrayList<B>();
  40. /**
  41. * Adds the specified pair to this map.
  42. *
  43. * @param key The key for the map
  44. * @param value The value for the map
  45. */
  46. public void put(final A key, final B value) {
  47. if (key == null || value == null) {
  48. throw new NullPointerException();
  49. }
  50. keys.add(key);
  51. values.add(value);
  52. }
  53. /**
  54. * Retrieves the value associated with the specified key.
  55. *
  56. * @param key The key to search for
  57. * @return The value of the specified key
  58. */
  59. public B getValue(final A key) {
  60. return values.get(keys.indexOf(key));
  61. }
  62. /**
  63. * Retrieves the key associated with the specified value.
  64. *
  65. * @param value The value to search for
  66. * @return The key of the specified value
  67. */
  68. public A getKey(final B value) {
  69. return keys.get(values.indexOf(value));
  70. }
  71. /**
  72. * Retrieves the set of keys in this double map.
  73. *
  74. * @return This map's key set
  75. */
  76. public Set<A> keySet() {
  77. return new HashSet<A>(keys);
  78. }
  79. /**
  80. * Retrieves the set of values in this double map.
  81. *
  82. * @return This map's value set
  83. */
  84. public Set<B> valueSet() {
  85. return new HashSet<B>(values);
  86. }
  87. }