move filtering logic to NamePredicateUtils

This commit is contained in:
Stepan Koltsov
2012-06-17 05:55:24 +04:00
parent b0f98711a0
commit e7d957436b
2 changed files with 36 additions and 30 deletions
@@ -17,10 +17,13 @@
package org.jetbrains.jet.lang.resolve.name;
import com.google.common.collect.Collections2;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* @author Stepan Koltsov
@@ -37,4 +40,32 @@ public class NamePredicateUtils {
return Collections2.filter(descriptors, predicate.asGuavaDescriptorPredicate());
}
}
public static <B>Map<Name, B> filterKeys(
@NotNull Map<Name, B> map,
@NotNull NamePredicate predicate) {
Name exact = predicate.getExact();
if (predicate.isAll()) {
return map;
}
else if (exact != null) {
B value = map.get(exact);
if (value != null) {
return Collections.singletonMap(exact, value);
}
else {
return Collections.emptyMap();
}
}
else {
Map<Name, B> r = Maps.newHashMap();
for (Map.Entry<Name, B> entry : map.entrySet()) {
if (predicate.matches(entry.getKey())) {
r.put(entry.getKey(), entry.getValue());
}
}
return r;
}
}
}