Skip to content

Instantly share code, notes, and snippets.

@axelerator
Created November 12, 2010 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axelerator/674032 to your computer and use it in GitHub Desktop.
Save axelerator/674032 to your computer and use it in GitHub Desktop.
public interface KeyValueFunction<KI, VI, KO, VO> {
Map.Entry<KO, VO> f(KI k, VI v);
}
/**
* Creates a new map by applying each element to the converter.
*
* @param <KI> key type of input map
* @param <VI> value type of input map
* @param <KO> key type of result map
* @param <VO> value type of result map
* @param in
* input map
* @param converter converts a input key-value-pair into an output pair
* @return map of the same size and order
*/
public static <KI, VI, KO, VO> Map<KO,VO> applyToMap(final Map<KI,VI> in, final KeyValueFunction<KI, VI, KO, VO> converter) {
final Map<KO, VO> result = new LinkedHashMap<KO, VO>(in.size());
for (final Map.Entry<KI, VI> entry : in.entrySet()) {
Entry<KO, VO> converted = converter.f(entry.getKey(), entry.getValue());
result.put(converted.getKey(), converted.getValue());
}
return result;
}
###################### IMPL ###################
private static class Block3Group extends SimpleEntry<Block3GroupType, Collection<WorkstreamMessage>> implements Map.Entry<Block3GroupType, Collection<WorkstreamMessage>> {
private static final long serialVersionUID = -8798387190864601959L;
public Block3Group(Block3GroupType type,
Collection<WorkstreamMessage> content) {
super(type, content);
}
}
private static final KeyValueFunction<
Block3GroupType, Collection<WorkstreamMessage>, // input
Block3GroupType, Collection<WorkstreamMessage>> // output
CROP_GROUP_FUNCTION =
new KeyValueFunction<Block3GroupType, Collection<WorkstreamMessage>,
Block3GroupType, Collection<WorkstreamMessage>>() {
@Override
public Block3Group f(Block3GroupType groupType, Collection<WorkstreamMessage> entries) {
...do stuff ..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment