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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2006-2015 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.Set;
  30. import java.util.stream.Collectors;
  31. import javax.annotation.Nonnull;
  32. /**
  33. * An object that maps keys to values, and values back to keys. Currently
  34. * does no checking for duplicates. Does not allow null values.
  35. * <p>Note that this implementation is NOT thread safe.
  36. *
  37. * @param <A> The first type of data to be mapped
  38. * @param <B> The second type of data to be mapped
  39. */
  40. public class DoubleMap<A,B> implements Map<A, B> {
  41. /** The keys in this map. */
  42. private final List<A> keys = new ArrayList<>();
  43. /** The values in this map. */
  44. private final List<B> values = new ArrayList<>();
  45. /**
  46. * Retrieves the value associated with the specified key.
  47. *
  48. * @param key The key to search for
  49. * @return The value of the specified key
  50. */
  51. public B getValue(final A key) {
  52. if (keys.indexOf(key) == -1) {
  53. return null;
  54. }
  55. return values.get(keys.indexOf(key));
  56. }
  57. /**
  58. * Retrieves the key associated with the specified value.
  59. *
  60. * @param value The value to search for
  61. * @return The key of the specified value
  62. */
  63. public A getKey(final B value) {
  64. if (values.indexOf(value) == -1) {
  65. return null;
  66. }
  67. return keys.get(values.indexOf(value));
  68. }
  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. @Nonnull
  79. @Override
  80. public Set<A> keySet() {
  81. return new HashSet<>(keys);
  82. }
  83. @Override
  84. public int size() {
  85. return keys.size();
  86. }
  87. @Override
  88. public boolean isEmpty() {
  89. return keys.isEmpty();
  90. }
  91. @Override
  92. public boolean containsKey(final Object key) {
  93. return keys.contains(key);
  94. }
  95. @Override
  96. public boolean containsValue(final Object value) {
  97. return values.contains(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(@Nonnull 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. @Nonnull
  125. @Override
  126. public Collection<B> values() {
  127. return new ArrayList<>(values);
  128. }
  129. @Nonnull
  130. @Override
  131. public Set<Entry<A, B>> entrySet() {
  132. return keys.stream()
  133. .map(key -> new SimpleEntry<>(key, getValue(key)))
  134. .collect(Collectors.toSet());
  135. }
  136. }