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

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