/**
* Gets duplicates of a given type based on a guard (predicate). A given function is used for converting an instance of type T
* to a string which is used for checking for duplicates.
*
* @param <T>
* the generic type
* @param predicate
* the predicate acting as a guard
* @param function
* returns a string for an instance of type T
* @param elements
* the elements to be checked
* @return the duplicates
*/
private <T extends EObject> Iterable<T> getDuplicates(final Predicate<T> predicate, final Function<T, String> function, final Iterable<T> elements) {
List<T> result = Lists.newArrayList();
Multimap<String, T> multiMap = Multimaps.newMultimap(Maps.<String, Collection<T>> newHashMap(), new Supplier<Collection<T>>() {
@Override
public Collection<T> get() {
return Lists.<T> newArrayList();
}
});
for (final T candidate : elements) {
if (predicate.apply(candidate)) {
multiMap.put(function.apply(candidate), candidate);
}
}
for (String elem : multiMap.keySet()) {
final Collection<T> duplicates = multiMap.get(elem);
if (duplicates.size() > 1) {
result.addAll(duplicates);
}
}
return result;
}
CheckCfgJavaValidator.java 文件源码
java
阅读 23
收藏 0
点赞 0
评论 0
项目:dsl-devkit
作者:
评论列表
文章目录