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.

DoubleMap.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2011 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.AbstractMap.SimpleEntry;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Map.Entry;
  30. import java.util.Set;
  31. /**
  32. * An object that maps keys to values, and values back to keys. Currently
  33. * does no checking for duplicates. Does not allow null values.
  34. *
  35. * @param <A> The first type of data to be mapped
  36. * @param <B> The second type of data to be mapped
  37. */
  38. public class DoubleMap<A,B> implements Map<A,B> {
  39. /** The keys in this map. */
  40. protected final List<A> keys = new ArrayList<A>();
  41. /** The values in this map. */
  42. protected final List<B> values = new ArrayList<B>();
  43. /**
  44. * Retrieves the value associated with the specified key.
  45. *
  46. * @param key The key to search for
  47. * @return The value of the specified key
  48. */
  49. public B getValue(final A key) {
  50. if (keys.indexOf(key) == -1) {
  51. return null;
  52. }
  53. return values.get(keys.indexOf(key));
  54. }
  55. /**
  56. * Retrieves the key associated with the specified value.
  57. *
  58. * @param value The value to search for
  59. * @return The key of the specified value
  60. */
  61. public A getKey(final B value) {
  62. if (values.indexOf(value) == -1) {
  63. return null;
  64. }
  65. return keys.get(values.indexOf(value));
  66. }
  67. /** {@inheritDoc} */
  68. @Override
  69. public B put(final A key, final B value) {
  70. if (key == null || value == null) {
  71. throw new NullPointerException();
  72. }
  73. keys.add(key);
  74. values.add(value);
  75. return value;
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public Set<A> keySet() {
  80. return new HashSet<A>(keys);
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public int size() {
  85. return keys.size();
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public boolean isEmpty() {
  90. return keys.isEmpty();
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public boolean containsKey(final Object key) {
  95. return keys.contains((A) key);
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public boolean containsValue(final Object value) {
  100. return values.contains((B) value);
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public B get(final Object key) {
  105. return getValue((A) key);
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public B remove(final Object key) {
  110. if (keys.indexOf(key) == -1) {
  111. return null;
  112. }
  113. final B value = values.remove(keys.indexOf(key));
  114. keys.remove(keys.indexOf(key));
  115. return value;
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public void putAll(final Map<? extends A, ? extends B> m) {
  120. for (Entry<? extends A, ? extends B> entry : m.entrySet()) {
  121. put(entry.getKey(), entry.getValue());
  122. }
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public void clear() {
  127. keys.clear();
  128. values.clear();
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public Collection<B> values() {
  133. return new ArrayList<B>(values);
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public Set<Entry<A, B>> entrySet() {
  138. final HashSet<Entry<A, B>> set = new HashSet<Entry<A, B>>();
  139. for (A key : keys) {
  140. set.add(new SimpleEntry(key, getValue(key)));
  141. }
  142. return set;
  143. }
  144. }