-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Java 8 modifier changes #244
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import javax.lang.model.element.Modifier; | ||
|
||
/** | ||
* Like Guava, but worse and standalone. This makes it easier to mix JavaPoet with libraries that | ||
* bring their own version of Guava. | ||
|
@@ -32,6 +34,16 @@ final class Util { | |
private Util() { | ||
} | ||
|
||
public static final Modifier DEFAULT; | ||
static { | ||
Modifier def = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat |
||
try { | ||
def = Modifier.valueOf("DEFAULT"); | ||
} catch (IllegalArgumentException ignored) { | ||
} | ||
DEFAULT = def; | ||
} | ||
|
||
public static <K, V> Map<K, List<V>> immutableMultimap(Map<K, List<V>> multimap) { | ||
LinkedHashMap<K, List<V>> result = new LinkedHashMap<>(); | ||
for (Map.Entry<K, List<V>> entry : multimap.entrySet()) { | ||
|
@@ -82,4 +94,63 @@ public static <T> Set<T> union(Set<T> a, Set<T> b) { | |
result.addAll(b); | ||
return result; | ||
} | ||
|
||
public static <T> Set<Set<T>> powerSet(Set<T> originalSet) { | ||
Set<Set<T>> sets = new LinkedHashSet<>(); | ||
if (originalSet.isEmpty()) { | ||
sets.add(new LinkedHashSet<T>()); | ||
return sets; | ||
} | ||
List<T> list = new ArrayList<T>(originalSet); | ||
T head = list.get(0); | ||
Set<T> rest = new LinkedHashSet<T>(list.subList(1, list.size())); | ||
for (Set<T> set : powerSet(rest)) { | ||
Set<T> newSet = new LinkedHashSet<T>(); | ||
newSet.add(head); | ||
newSet.addAll(set); | ||
sets.add(newSet); | ||
sets.add(set); | ||
} | ||
return sets; | ||
} | ||
|
||
/** | ||
* Finds a subset of {@code set} in {@code modifiers}. Ignores empty subsets, | ||
* and if {@code set} is empty then {@code true} is always returned. | ||
* | ||
* @param modifiers | ||
* - The input modifiers | ||
* @param set | ||
* - The set of modifiers to make subsets of | ||
*/ | ||
public static void requireSubsetOf(Set<Modifier> set, Set<Modifier> modifiers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Though neat!) |
||
Set<Set<Modifier>> powerSet = powerSet(set); | ||
boolean containsSubset = set.isEmpty(); | ||
for (Set<Modifier> subset : powerSet) { | ||
if (subset.isEmpty()) { | ||
continue; | ||
} | ||
containsSubset |= modifiers.containsAll(subset); | ||
} | ||
checkState(containsSubset, "%s should contain a subset of %s", modifiers, set); | ||
} | ||
|
||
public static void requireExactlyOneOf(Set<Modifier> set, Set<Modifier> modifiers) { | ||
boolean containsOne = false; | ||
boolean containsMany = false; | ||
for (Modifier check : set) { | ||
boolean containsCheck = modifiers.contains(check); | ||
containsMany = containsOne && containsCheck; | ||
if (containsMany) { | ||
break; | ||
} | ||
containsOne |= containsCheck; | ||
} | ||
checkState(containsOne, "%s must contain one of %s", modifiers, set); | ||
checkState(!containsMany, "%s must contain only one of %s", modifiers, set); | ||
} | ||
|
||
public static boolean hasDefaultModifier(Collection<Modifier> modifiers) { | ||
return DEFAULT != null && modifiers.contains(DEFAULT); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this