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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2014 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<>();
  42. /** The values in this map. */
  43. protected final List<B> values = new ArrayList<>();
  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. @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. @Override
  78. public Set<A> keySet() {
  79. return new HashSet<>(keys);
  80. }
  81. @Override
  82. public int size() {
  83. return keys.size();
  84. }
  85. @Override
  86. public boolean isEmpty() {
  87. return keys.isEmpty();
  88. }
  89. @Override
  90. @SuppressWarnings("unchecked")
  91. public boolean containsKey(final Object key) {
  92. return keys.contains((A) key);
  93. }
  94. @Override
  95. @SuppressWarnings("unchecked")
  96. public boolean containsValue(final Object value) {
  97. return values.contains((B) value);
  98. }
  99. @Override
  100. @SuppressWarnings("unchecked")
  101. public B get(final Object key) {
  102. return getValue((A) key);
  103. }
  104. @Override
  105. public B remove(final Object key) {
  106. if (keys.indexOf(key) == -1) {
  107. return null;
  108. }
  109. final B value = values.remove(keys.indexOf(key));
  110. keys.remove(keys.indexOf(key));
  111. return value;
  112. }
  113. @Override
  114. public void putAll(final Map<? extends A, ? extends B> m) {
  115. for (Entry<? extends A, ? extends B> entry : m.entrySet()) {
  116. put(entry.getKey(), entry.getValue());
  117. }
  118. }
  119. @Override
  120. public void clear() {
  121. keys.clear();
  122. values.clear();
  123. }
  124. @Override
  125. public Collection<B> values() {
  126. return new ArrayList<>(values);
  127. }
  128. @Override
  129. public Set<Entry<A, B>> entrySet() {
  130. final Set<Entry<A, B>> set = new HashSet<>();
  131. for (A key : keys) {
  132. set.add(new SimpleEntry<>(key, getValue(key)));
  133. }
  134. return set;
  135. }
  136. }