PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/fugue/ImmutableMaps.java

https://bitbucket.org/atlassian/fugue
Java | 153 lines | 54 code | 12 blank | 87 comment | 8 complexity | 44042d8c412dcc7e14222f3d1ecb0c4f MD5 | raw file
  1. /*
  2. Copyright 2011 Atlassian
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.atlassian.fugue;
  14. import java.util.Map;
  15. import com.google.common.base.Function;
  16. import com.google.common.collect.ImmutableMap;
  17. import com.google.common.collect.Maps;
  18. /**
  19. * Provides some utility methods to convert Iterables to ImmutableMap, and to
  20. * transform Maps.
  21. *
  22. * @since 1.3
  23. */
  24. public class ImmutableMaps {
  25. private ImmutableMaps() {
  26. throw new UnsupportedOperationException();
  27. }
  28. /**
  29. * Builds an immutable map from the given iterable of
  30. * {@link java.util.Map.Entry}.
  31. * <p/>
  32. * Any <code>null</code> entries will be filtered out. Additionally, any
  33. * entries containing <code>null</code> key or value will also be filtered
  34. * out. If multiple entries return the same key,
  35. * {@link IllegalArgumentException} will be thrown.
  36. */
  37. public static <K, V> ImmutableMap<K, V> toMap(Iterable<Map.Entry<K, V>> fromIterable) {
  38. ImmutableMap.Builder<K, V> mapBuilder = ImmutableMap.builder();
  39. for (Map.Entry<K, V> entry : fromIterable) {
  40. if (entry != null) {
  41. K key = entry.getKey();
  42. V value = entry.getValue();
  43. if (key != null && value != null) {
  44. mapBuilder.put(key, value);
  45. }
  46. }
  47. }
  48. return mapBuilder.build();
  49. }
  50. /**
  51. * Builds an immutable map from the given iterable, with key derived from the
  52. * application of the iterable to the keyTransformer, and value derived from
  53. * the application of the iterable to the valueTransformer.
  54. * <p/>
  55. * <code>null</code> value is allowed and will be passed to the keyTransformer
  56. * and valueTransformer. However, if either the keyTransformer or the
  57. * valueTransformer returns <code>null</code> for an entry, the entry is
  58. * ignored. If keyTransformer returns the same key for multiple entries,
  59. * {@link IllegalArgumentException} will be thrown.
  60. */
  61. public static <T, K, V> ImmutableMap<K, V> toMap(Iterable<T> fromIterable, final Function<T, K> keyTransformer,
  62. final Function<T, V> valueTransformer) {
  63. return toMap(com.google.common.collect.Iterables.transform(fromIterable, new Function<T, Map.Entry<K, V>>() {
  64. @Override public Map.Entry<K, V> apply(T input) {
  65. return Maps.immutableEntry(keyTransformer.apply(input), valueTransformer.apply(input));
  66. }
  67. }));
  68. }
  69. /**
  70. * Builds an immutable map that is keyed by the result of applying
  71. * keyTransformer to each element of the given iterable of values.
  72. * <p/>
  73. * <code>null</code> value is allowed but will be ignored. If keyTransformer
  74. * returns the same key for multiple entries, {@link IllegalArgumentException}
  75. * will be thrown.
  76. */
  77. public static <K, V> ImmutableMap<K, V> mapBy(Iterable<V> fromIterable, final Function<V, K> keyTransformer) {
  78. return toMap(fromIterable, keyTransformer, com.google.common.base.Functions.<V> identity());
  79. }
  80. /**
  81. * Builds an immutable map from the given iterable and compute the value by
  82. * applying the valueTransformer.
  83. * <p/>
  84. * <code>null</code> value is allowed but will be ignored. If there are
  85. * duplicate entries in the iterable, {@link IllegalArgumentException} will be
  86. * thrown.
  87. */
  88. public static <K, V> ImmutableMap<K, V> mapTo(Iterable<K> fromIterable, final Function<K, V> valueTransformer) {
  89. return toMap(fromIterable, com.google.common.base.Functions.<K> identity(), valueTransformer);
  90. }
  91. /**
  92. * Returns an immutable map that applies function to each entry of
  93. * {@code fromMap}. If <code>null</code> is returned by the function for any
  94. * entry, or if an entry returned by the function contains a <code>null</code>
  95. * key or value, that entry is discarded in the result. If the function
  96. * returns entries with the same key for multiple entries,
  97. * {@link IllegalArgumentException} will be thrown.
  98. */
  99. public static <K1, K2, V1, V2> ImmutableMap<K2, V2> transform(Map<K1, V1> fromMap, Function<Map.Entry<K1, V1>, Map.Entry<K2, V2>> function) {
  100. return toMap(com.google.common.collect.Iterables.transform(fromMap.entrySet(), function));
  101. }
  102. /**
  103. * Returns an immutable map that applies the keyTransformer and
  104. * valueTransformer functions to each entry of {@code fromMap}. If for any
  105. * entry, a <code>null</code> key or value is returned, that entry is
  106. * discarded in the result. If the keyTransformer function returns the same
  107. * key for multiple entries, {@link IllegalArgumentException} will be thrown.
  108. */
  109. public static <K1, K2, V1, V2> ImmutableMap<K2, V2> transform(Map<K1, V1> fromMap, final Function<K1, K2> keyTransformer,
  110. final Function<V1, V2> valueTransformer) {
  111. return toMap(com.google.common.collect.Iterables.transform(fromMap.entrySet(), new Function<Map.Entry<K1, V1>, Map.Entry<K2, V2>>() {
  112. @Override public Map.Entry<K2, V2> apply(Map.Entry<K1, V1> input) {
  113. return input == null ? null : Maps.immutableEntry(keyTransformer.apply(input.getKey()), valueTransformer.apply(input.getValue()));
  114. }
  115. }));
  116. }
  117. /**
  118. * Returns an immutable map that applies keyTransformer to the key of each
  119. * entry of the source map. If <code>null</code> is returned by the
  120. * keyTransformer for any entry, that entry is discarded in the result. If an
  121. * entry contains a <code>null</code> value, it will also be discarded in the
  122. * result. If the {@code function} returns the same result key for multiple
  123. * keys, {@link IllegalArgumentException} will be thrown.
  124. */
  125. public static <K1, K2, V> ImmutableMap<K2, V> transformKey(Map<K1, V> fromMap, final Function<K1, K2> keyTransformer) {
  126. return transform(fromMap, keyTransformer, com.google.common.base.Functions.<V> identity());
  127. }
  128. /**
  129. * Returns an immutable map that applies valueTransformer to the value of each
  130. * entry of the source map. If <code>null</code> is returned by the
  131. * valueTransformer for any entry, that entry is discarded in the result. If
  132. * an entry contains a <code>null</code> key, it will also be discarded in the
  133. * result.
  134. */
  135. public static <K, V1, V2> ImmutableMap<K, V2> transformValue(Map<K, V1> fromMap, final Function<V1, V2> valueTransformer) {
  136. return transform(fromMap, com.google.common.base.Functions.<K> identity(), valueTransformer);
  137. }
  138. }