Skip to content

Commit eec68e4

Browse files
blicklycopybara-github
authored andcommitted
Get rid of unnecessary anonymous classes
These can generally be replaced with functions or inlined completely. PiperOrigin-RevId: 601809673
1 parent 62745bb commit eec68e4

File tree

4 files changed

+22
-54
lines changed

4 files changed

+22
-54
lines changed

src/com/google/javascript/jscomp/ConformanceRules.java

+10-27
Original file line numberDiff line numberDiff line change
@@ -588,17 +588,14 @@ static final class BannedName extends AbstractRule {
588588
}
589589

590590
private static final Precondition IS_CANDIDATE_NODE =
591-
new Precondition() {
592-
@Override
593-
public boolean shouldCheck(Node n) {
594-
switch (n.getToken()) {
595-
case GETPROP:
596-
return n.getFirstChild().isQualifiedName();
597-
case NAME:
598-
return !n.getString().isEmpty();
599-
default:
600-
return false;
601-
}
591+
(Node n) -> {
592+
switch (n.getToken()) {
593+
case GETPROP:
594+
return n.getFirstChild().isQualifiedName();
595+
case NAME:
596+
return !n.getString().isEmpty();
597+
default:
598+
return false;
602599
}
603600
};
604601

@@ -1953,17 +1950,10 @@ protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
19531950
return ConformanceResult.CONFORMANCE;
19541951
}
19551952

1956-
private static final Precondition IS_SCRIPT_NODE =
1957-
new Precondition() {
1958-
@Override
1959-
public boolean shouldCheck(Node n) {
1960-
return n.isScript();
1961-
}
1962-
};
19631953

19641954
@Override
19651955
public final Precondition getPrecondition() {
1966-
return IS_SCRIPT_NODE;
1956+
return Node::isScript;
19671957
}
19681958
}
19691959

@@ -1996,17 +1986,10 @@ protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
19961986
return ConformanceResult.CONFORMANCE;
19971987
}
19981988

1999-
private static final Precondition IS_SCRIPT_NODE =
2000-
new Precondition() {
2001-
@Override
2002-
public boolean shouldCheck(Node n) {
2003-
return n.isScript();
2004-
}
2005-
};
20061989

20071990
@Override
20081991
public final Precondition getPrecondition() {
2009-
return IS_SCRIPT_NODE;
1992+
return Node::isScript;
20101993
}
20111994
}
20121995

src/com/google/javascript/jscomp/Es6RewriteBlockScopedDeclaration.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,7 @@ private boolean inLoop(Node n) {
129129
}
130130

131131
private static final Predicate<Node> isLoopOrFunction =
132-
new Predicate<Node>() {
133-
@Override
134-
public boolean apply(Node n) {
135-
return n.isFunction() || NodeUtil.isLoopStructure(n);
136-
}
137-
};
132+
(n) -> n.isFunction() || NodeUtil.isLoopStructure(n);
138133

139134
private static void extractInlineJSDoc(Node srcDeclaration, Node srcName, Node destDeclaration) {
140135
JSDocInfo existingInfo = srcDeclaration.getJSDocInfo();

src/com/google/javascript/jscomp/RenameVars.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,17 @@ void incCount(String name) {
292292
}
293293

294294
/**
295-
* Sorts Assignment objects by their count, breaking ties by their order of
296-
* occurrence in the source to ensure a deterministic total ordering.
295+
* Sorts Assignment objects by their count, breaking ties by their order of occurrence in the
296+
* source to ensure a deterministic total ordering.
297297
*/
298-
private static final Comparator<Assignment> FREQUENCY_COMPARATOR =
299-
new Comparator<Assignment>() {
300-
@Override
301-
public int compare(Assignment a1, Assignment a2) {
302-
if (a1.count != a2.count) {
303-
return a2.count - a1.count;
304-
}
305-
// Break a tie using the order in which the variable first appears in
306-
// the source.
307-
return ORDER_OF_OCCURRENCE_COMPARATOR.compare(a1, a2);
298+
private static int frequencyComparator(Assignment a1, Assignment a2) {
299+
if (a1.count != a2.count) {
300+
return a2.count - a1.count;
308301
}
309-
};
302+
// Break a tie using the order in which the variable first appears in
303+
// the source.
304+
return ORDER_OF_OCCURRENCE_COMPARATOR.compare(a1, a2);
305+
}
310306

311307
/** Sorts Assignment objects by the order the variable name first appears in the source. */
312308
private static final Comparator<Assignment> ORDER_OF_OCCURRENCE_COMPARATOR =
@@ -325,7 +321,7 @@ public void process(Node externs, Node root) {
325321
reservedNames.addAll(externNames);
326322

327323
// Rename vars, sorted by frequency of occurrence to minimize code size.
328-
SortedSet<Assignment> varsByFrequency = new TreeSet<>(FREQUENCY_COMPARATOR);
324+
SortedSet<Assignment> varsByFrequency = new TreeSet<>(RenameVars::frequencyComparator);
329325
varsByFrequency.addAll(assignments.values());
330326

331327
// First try to reuse names from an earlier compilation.

src/com/google/javascript/jscomp/Xid.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,8 @@ public interface HashFunction {
5151
int hashCode(String value);
5252
}
5353

54-
private static final HashFunction DEFAULT = new HashFunction() {
55-
@Override public int hashCode(String value) {
56-
return value.hashCode();
57-
}
58-
};
59-
6054
public Xid() {
61-
this.hasher = DEFAULT;
55+
this.hasher = String::hashCode;
6256
}
6357

6458
public Xid(HashFunction hasher) {

0 commit comments

Comments
 (0)