com.google.common.collect.Maps.EntryTransformer Java Examples

The following examples show how to use com.google.common.collect.Maps.EntryTransformer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RelationshipNormalFormGenerator.java    From snomed-owl-toolkit with Apache License 2.0 6 votes vote down vote up
private Iterable<Group> toGroups(final boolean preserveNumbers, final Collection<Relationship> nonIsARelationshipFragments) {

		final Map<Integer, Collection<Relationship>> relationshipsByGroupId = Multimaps.index(nonIsARelationshipFragments, Relationship::getGroup).asMap();

		final Collection<Collection<Group>> groups = Maps.transformEntries(relationshipsByGroupId,
				(EntryTransformer<Integer, Collection<Relationship>, Collection<Group>>) (key, values) -> {
					final Iterable<UnionGroup> unionGroups = toUnionGroups(preserveNumbers, values);
					final Set<UnionGroup> disjointUnionGroups = getDisjointComparables(unionGroups);

					if (key == 0) {
						// Relationships in group 0 form separate groups
						return ImmutableList.copyOf(toZeroGroups(disjointUnionGroups));
					} else {
						// Other group numbers produce a single group from all fragments
						return ImmutableList.of(toNonZeroGroup(preserveNumbers, key, disjointUnionGroups));
					}
				}).values();

		return Iterables.concat(groups);
	}
 
Example #2
Source File: RelationshipNormalFormGenerator.java    From snomed-owl-toolkit with Apache License 2.0 6 votes vote down vote up
private Iterable<UnionGroup> toUnionGroups(final boolean preserveNumbers, final Collection<Relationship> values) {
	final Map<Integer, Collection<Relationship>> relationshipsByUnionGroupId = Multimaps.index(values, Relationship::getUnionGroup).asMap();

	final Collection<Collection<UnionGroup>> unionGroups = Maps.transformEntries(relationshipsByUnionGroupId,
			(EntryTransformer<Integer, Collection<Relationship>, Collection<UnionGroup>>) (key, values1) -> {
				if (key == 0) {
					// Relationships in union group 0 form separate union groups
					return ImmutableList.copyOf(toZeroUnionGroups(values1));
				} else {
					// Other group numbers produce a single union group from all fragments
					return ImmutableList.of(toNonZeroUnionGroup(preserveNumbers, key, values1));
				}
			}).values();

	return Iterables.concat(unionGroups);
}
 
Example #3
Source File: KafkaMetadataUtil.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * @param brokers in multiple clusters, keyed by cluster id
 * @param topic
 * @return Get the partition metadata list for the specific topic via the brokers
 * null if topic is not found
 */
public static Map<String, List<PartitionMetadata>> getPartitionsForTopic(SetMultimap<String, String> brokers, final String topic)
{
  return Maps.transformEntries(brokers.asMap(), new EntryTransformer<String, Collection<String>, List<PartitionMetadata>>()
  {
    @Override
    public List<PartitionMetadata> transformEntry(String key, Collection<String> bs)
    {
      return getPartitionsForTopic(new HashSet<String>(bs), topic);
    }
  });
}
 
Example #4
Source File: KafkaPosition.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Map<Integer, IPartitionPosition> getPartitionPositions() {
    return Maps.transformEntries(partitionOffsetMap, new EntryTransformer<Integer, Long, IPartitionPosition>() {
        @Override
        public IPartitionPosition transformEntry(@Nullable Integer key, @Nullable Long value) {
            return new KafkaPartitionPosition(key, value);
        }
    });
}
 
Example #5
Source File: ExtDirectServlet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected RequestRouter createRequestRouter(final Registry registry, final GlobalConfiguration globalConfiguration) {
  final Dispatcher dispatcher = createDispatcher(globalConfiguration.getDispatcherClass());
  return new RequestRouter(registry, globalConfiguration, dispatcher)
  {
    @Override
    public void processPollRequest(final Reader reader, final Writer writer, final String pathInfo)
        throws IOException
    {
      new PollRequestProcessor(registry, dispatcher, globalConfiguration)
      {
        @Override
        // HACK: we determine parameters from request not by reading request content as request content could had
        // been already read exactly for getting the params, case when request content is already empty
        protected Object[] getParameters()
        {
          if (reader instanceof RequestBoundReader) {
            ServletRequest request = ((RequestBoundReader) reader).getRequest();
            Map<String, String[]> parameterMap = request.getParameterMap();
            Map<String, String> parameters = Maps.newHashMap();
            if (parameterMap != null) {
              parameters = Maps.transformEntries(parameterMap, new EntryTransformer<String, String[], String>()
              {
                @Override
                public String transformEntry(@Nullable final String key, @Nullable final String[] values) {
                  return values == null || values.length == 0 ? null : values[0];
                }
              });
            }
            return new Object[]{parameters};
          }
          return super.getParameters();
        }
      }.process(reader, writer, pathInfo);
    }
  };
}
 
Example #6
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
Map<K, Collection<V2>> createAsMap() {
  return Maps.transformEntries(
      fromMultimap.asMap(),
      new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
        @Override
        public Collection<V2> transformEntry(K key, Collection<V1> value) {
          return transform(key, value);
        }
      });
}
 
Example #7
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
Map<K, Collection<V2>> createAsMap() {
  return Maps.transformEntries(fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
                                                       @Override
                                                       public Collection<V2> transformEntry(K key, Collection<V1> value) {
                                                         return transform(key, value);
                                                       }
                                                     });
}
 
Example #8
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
Map<K, Collection<V2>> createAsMap() {
  return Maps.transformEntries(fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
                                                       @Override
                                                       public Collection<V2> transformEntry(K key, Collection<V1> value) {
                                                         return transform(key, value);
                                                       }
                                                     });
}
 
Example #9
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
Map<K, Collection<V2>> createAsMap() {
  return Maps.transformEntries(fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
                                                       @Override
                                                       public Collection<V2> transformEntry(K key, Collection<V1> value) {
                                                         return transform(key, value);
                                                       }
                                                     });
}
 
Example #10
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
Map<K, Collection<V2>> createAsMap() {
  return Maps.transformEntries(fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
                                                       @Override
                                                       public Collection<V2> transformEntry(K key, Collection<V1> value) {
                                                         return transform(key, value);
                                                       }
                                                     });
}
 
Example #11
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesListMultimap(
    ListMultimap<K, V1> fromMultimap, EntryTransformer<? super K, ? super V1, V2> transformer) {
  super(fromMultimap, transformer);
}
 
Example #12
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesMultimap(
                 Multimap<K, V1> fromMultimap,
                 final EntryTransformer<? super K, ? super V1, V2> transformer) {
  this.fromMultimap = checkNotNull(fromMultimap);
  this.transformer = checkNotNull(transformer);
}
 
Example #13
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesMultimap(
                 Multimap<K, V1> fromMultimap,
                 final EntryTransformer<? super K, ? super V1, V2> transformer) {
  this.fromMultimap = checkNotNull(fromMultimap);
  this.transformer = checkNotNull(transformer);
}
 
Example #14
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesMultimap(
    Multimap<K, V1> fromMultimap,
    final EntryTransformer<? super K, ? super V1, V2> transformer) {
  this.fromMultimap = checkNotNull(fromMultimap);
  this.transformer = checkNotNull(transformer);
}
 
Example #15
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesListMultimap(ListMultimap<K, V1> fromMultimap, EntryTransformer<? super K, ? super V1, V2> transformer) {
  super(fromMultimap, transformer);
}
 
Example #16
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesListMultimap(ListMultimap<K, V1> fromMultimap, EntryTransformer<? super K, ? super V1, V2> transformer) {
  super(fromMultimap, transformer);
}
 
Example #17
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesMultimap(
                 Multimap<K, V1> fromMultimap,
                 final EntryTransformer<? super K, ? super V1, V2> transformer) {
  this.fromMultimap = checkNotNull(fromMultimap);
  this.transformer = checkNotNull(transformer);
}
 
Example #18
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesMultimap(
                 Multimap<K, V1> fromMultimap,
                 final EntryTransformer<? super K, ? super V1, V2> transformer) {
  this.fromMultimap = checkNotNull(fromMultimap);
  this.transformer = checkNotNull(transformer);
}
 
Example #19
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesListMultimap(ListMultimap<K, V1> fromMultimap, EntryTransformer<? super K, ? super V1, V2> transformer) {
  super(fromMultimap, transformer);
}
 
Example #20
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
TransformedEntriesListMultimap(ListMultimap<K, V1> fromMultimap, EntryTransformer<? super K, ? super V1, V2> transformer) {
  super(fromMultimap, transformer);
}
 
Example #21
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a multimap where each value is transformed by a function.
 * All other properties of the multimap, such as iteration order, are left
 * intact. For example, the code: <pre>   {@code
 *
 * Multimap<String, Integer> multimap =
 *     ImmutableSetMultimap.of("a", 2, "b", -3, "b", -3, "a", 4, "c", 6);
 * Function<Integer, String> square = new Function<Integer, String>() {
 *     public String apply(Integer in) {
 *       return Integer.toString(in * in);
 *     }
 * };
 * Multimap<String, String> transformed =
 *     Multimaps.transformValues(multimap, square);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[4, 16], b=[9, 9], c=[36]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.  The {@code equals} and {@code hashCode} methods
 * of the returned multimap are meaningless, since there is not a definition
 * of {@code equals} or {@code hashCode} for general collections, and
 * {@code get()} will return a general {@code Collection} as opposed to a
 * {@code List} or a {@code Set}.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> Multimap<K, V2> transformValues(Multimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #22
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a multimap where each value is transformed by a function.
 * All other properties of the multimap, such as iteration order, are left
 * intact. For example, the code: <pre>   {@code
 *
 * Multimap<String, Integer> multimap =
 *     ImmutableSetMultimap.of("a", 2, "b", -3, "b", -3, "a", 4, "c", 6);
 * Function<Integer, String> square = new Function<Integer, String>() {
 *     public String apply(Integer in) {
 *       return Integer.toString(in * in);
 *     }
 * };
 * Multimap<String, String> transformed =
 *     Multimaps.transformValues(multimap, square);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[4, 16], b=[9, 9], c=[36]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.  The {@code equals} and {@code hashCode} methods
 * of the returned multimap are meaningless, since there is not a definition
 * of {@code equals} or {@code hashCode} for general collections, and
 * {@code get()} will return a general {@code Collection} as opposed to a
 * {@code List} or a {@code Set}.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> Multimap<K, V2> transformValues(Multimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #23
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a {@code ListMultimap} where each value is transformed by
 * a function. All other properties of the multimap, such as iteration order,
 * are left intact. For example, the code: <pre>   {@code
 *
 *   ListMultimap<String, Integer> multimap
 *        = ImmutableListMultimap.of("a", 4, "a", 16, "b", 9);
 *   Function<Integer, Double> sqrt =
 *       new Function<Integer, Double>() {
 *         public Double apply(Integer in) {
 *           return Math.sqrt((int) in);
 *         }
 *       };
 *   ListMultimap<String, Double> transformed = Multimaps.transformValues(map,
 *       sqrt);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[2.0, 4.0], b=[3.0]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */
public static <K, V1, V2> ListMultimap<K, V2> transformValues(
    ListMultimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #24
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a multimap where each value is transformed by a function.
 * All other properties of the multimap, such as iteration order, are left
 * intact. For example, the code: <pre>   {@code
 *
 * Multimap<String, Integer> multimap =
 *     ImmutableSetMultimap.of("a", 2, "b", -3, "b", -3, "a", 4, "c", 6);
 * Function<Integer, String> square = new Function<Integer, String>() {
 *     public String apply(Integer in) {
 *       return Integer.toString(in * in);
 *     }
 * };
 * Multimap<String, String> transformed =
 *     Multimaps.transformValues(multimap, square);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[4, 16], b=[9, 9], c=[36]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.  The {@code equals} and {@code hashCode} methods
 * of the returned multimap are meaningless, since there is not a definition
 * of {@code equals} or {@code hashCode} for general collections, and
 * {@code get()} will return a general {@code Collection} as opposed to a
 * {@code List} or a {@code Set}.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */
public static <K, V1, V2> Multimap<K, V2> transformValues(
    Multimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #25
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a {@code ListMultimap} where each value is transformed by
 * a function. All other properties of the multimap, such as iteration order,
 * are left intact. For example, the code: <pre>   {@code
 *
 *   ListMultimap<String, Integer> multimap
 *        = ImmutableListMultimap.of("a", 4, "a", 16, "b", 9);
 *   Function<Integer, Double> sqrt =
 *       new Function<Integer, Double>() {
 *         public Double apply(Integer in) {
 *           return Math.sqrt((int) in);
 *         }
 *       };
 *   ListMultimap<String, Double> transformed = Multimaps.transformValues(map,
 *       sqrt);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[2.0, 4.0], b=[3.0]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> ListMultimap<K, V2> transformValues(ListMultimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #26
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a {@code ListMultimap} where each value is transformed by
 * a function. All other properties of the multimap, such as iteration order,
 * are left intact. For example, the code: <pre>   {@code
 *
 *   ListMultimap<String, Integer> multimap
 *        = ImmutableListMultimap.of("a", 4, "a", 16, "b", 9);
 *   Function<Integer, Double> sqrt =
 *       new Function<Integer, Double>() {
 *         public Double apply(Integer in) {
 *           return Math.sqrt((int) in);
 *         }
 *       };
 *   ListMultimap<String, Double> transformed = Multimaps.transformValues(map,
 *       sqrt);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[2.0, 4.0], b=[3.0]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> ListMultimap<K, V2> transformValues(ListMultimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #27
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a {@code ListMultimap} where each value is transformed by
 * a function. All other properties of the multimap, such as iteration order,
 * are left intact. For example, the code: <pre>   {@code
 *
 *   ListMultimap<String, Integer> multimap
 *        = ImmutableListMultimap.of("a", 4, "a", 16, "b", 9);
 *   Function<Integer, Double> sqrt =
 *       new Function<Integer, Double>() {
 *         public Double apply(Integer in) {
 *           return Math.sqrt((int) in);
 *         }
 *       };
 *   ListMultimap<String, Double> transformed = Multimaps.transformValues(map,
 *       sqrt);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[2.0, 4.0], b=[3.0]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> ListMultimap<K, V2> transformValues(ListMultimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #28
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a multimap where each value is transformed by a function.
 * All other properties of the multimap, such as iteration order, are left
 * intact. For example, the code: <pre>   {@code
 *
 * Multimap<String, Integer> multimap =
 *     ImmutableSetMultimap.of("a", 2, "b", -3, "b", -3, "a", 4, "c", 6);
 * Function<Integer, String> square = new Function<Integer, String>() {
 *     public String apply(Integer in) {
 *       return Integer.toString(in * in);
 *     }
 * };
 * Multimap<String, String> transformed =
 *     Multimaps.transformValues(multimap, square);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[4, 16], b=[9, 9], c=[36]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.  The {@code equals} and {@code hashCode} methods
 * of the returned multimap are meaningless, since there is not a definition
 * of {@code equals} or {@code hashCode} for general collections, and
 * {@code get()} will return a general {@code Collection} as opposed to a
 * {@code List} or a {@code Set}.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> Multimap<K, V2> transformValues(Multimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #29
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a {@code ListMultimap} where each value is transformed by
 * a function. All other properties of the multimap, such as iteration order,
 * are left intact. For example, the code: <pre>   {@code
 *
 *   ListMultimap<String, Integer> multimap
 *        = ImmutableListMultimap.of("a", 4, "a", 16, "b", 9);
 *   Function<Integer, Double> sqrt =
 *       new Function<Integer, Double>() {
 *         public Double apply(Integer in) {
 *           return Math.sqrt((int) in);
 *         }
 *       };
 *   ListMultimap<String, Double> transformed = Multimaps.transformValues(map,
 *       sqrt);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[2.0, 4.0], b=[3.0]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> ListMultimap<K, V2> transformValues(ListMultimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}
 
Example #30
Source File: Multimaps.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns a view of a multimap where each value is transformed by a function.
 * All other properties of the multimap, such as iteration order, are left
 * intact. For example, the code: <pre>   {@code
 *
 * Multimap<String, Integer> multimap =
 *     ImmutableSetMultimap.of("a", 2, "b", -3, "b", -3, "a", 4, "c", 6);
 * Function<Integer, String> square = new Function<Integer, String>() {
 *     public String apply(Integer in) {
 *       return Integer.toString(in * in);
 *     }
 * };
 * Multimap<String, String> transformed =
 *     Multimaps.transformValues(multimap, square);
 *   System.out.println(transformed);}</pre>
 *
 * ... prints {@code {a=[4, 16], b=[9, 9], c=[36]}}.
 *
 * <p>Changes in the underlying multimap are reflected in this view.
 * Conversely, this view supports removal operations, and these are reflected
 * in the underlying multimap.
 *
 * <p>It's acceptable for the underlying multimap to contain null keys, and
 * even null values provided that the function is capable of accepting null
 * input.  The transformed multimap might contain null values, if the function
 * sometimes gives a null result.
 *
 * <p>The returned multimap is not thread-safe or serializable, even if the
 * underlying multimap is.  The {@code equals} and {@code hashCode} methods
 * of the returned multimap are meaningless, since there is not a definition
 * of {@code equals} or {@code hashCode} for general collections, and
 * {@code get()} will return a general {@code Collection} as opposed to a
 * {@code List} or a {@code Set}.
 *
 * <p>The function is applied lazily, invoked when needed. This is necessary
 * for the returned multimap to be a view, but it means that the function will
 * be applied many times for bulk operations like
 * {@link Multimap#containsValue} and {@code Multimap.toString()}. For this to
 * perform well, {@code function} should be fast. To avoid lazy evaluation
 * when the returned multimap doesn't need to be a view, copy the returned
 * multimap into a new multimap of your choosing.
 *
 * @since 7.0
 */


public static <K, V1, V2> Multimap<K, V2> transformValues(Multimap<K, V1> fromMultimap, final Function<? super V1, V2> function) {
  checkNotNull(function);
  EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
  return transformEntries(fromMultimap, transformer);
}