Skip to content

Commit fb84f6d

Browse files
Java 8 improvements: (#106)
* Replace Anonymous with lambda. * Use Comparator
1 parent 27c6dde commit fb84f6d

2 files changed

Lines changed: 27 additions & 83 deletions

File tree

src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,7 @@ public String toString() {
232232
// sort RULES by pattern length in descending order
233233
for (final Map.Entry<Character, List<Rule>> rule : RULES.entrySet()) {
234234
final List<Rule> ruleList = rule.getValue();
235-
ruleList.sort(new Comparator<Rule>() {
236-
@Override
237-
public int compare(final Rule rule1, final Rule rule2) {
238-
return rule2.getPatternLength() - rule1.getPatternLength();
239-
}
240-
});
235+
ruleList.sort((rule1, rule2) -> rule2.getPatternLength() - rule1.getPatternLength());
241236
}
242237
}
243238

@@ -305,11 +300,7 @@ private static void parseRules(final Scanner scanner, final String location,
305300

306301
final Rule r = new Rule(pattern, replacement1, replacement2, replacement3);
307302
final char patternKey = r.pattern.charAt(0);
308-
List<Rule> rules = ruleMapping.get(patternKey);
309-
if (rules == null) {
310-
rules = new ArrayList<>();
311-
ruleMapping.put(patternKey, rules);
312-
}
303+
final List<Rule> rules = ruleMapping.computeIfAbsent(patternKey, k -> new ArrayList<>());
313304
rules.add(r);
314305
} catch (final IllegalArgumentException e) {
315306
throw new IllegalStateException(
@@ -500,7 +491,7 @@ private String[] soundex(final String source, final boolean branching) {
500491
}
501492

502493
// use an EMPTY_LIST to avoid false positive warnings wrt potential null pointer access
503-
final List<Branch> nextBranches = branching ? new ArrayList<Branch>() : Collections.<Branch>emptyList();
494+
final List<Branch> nextBranches = branching ? new ArrayList<>() : Collections.emptyList();
504495

505496
for (final Rule rule : rules) {
506497
if (rule.matches(inputContext)) {

src/main/java/org/apache/commons/codec/language/bm/Rule.java

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,24 @@
8181
public class Rule {
8282

8383
public static final class Phoneme implements PhonemeExpr {
84-
public static final Comparator<Phoneme> COMPARATOR = new Comparator<Phoneme>() {
85-
@Override
86-
public int compare(final Phoneme o1, final Phoneme o2) {
87-
final int o1Length = o1.phonemeText.length();
88-
final int o2Length = o2.phonemeText.length();
89-
for (int i = 0; i < o1Length; i++) {
90-
if (i >= o2Length) {
91-
return +1;
92-
}
93-
final int c = o1.phonemeText.charAt(i) - o2.phonemeText.charAt(i);
94-
if (c != 0) {
95-
return c;
96-
}
84+
public static final Comparator<Phoneme> COMPARATOR = (o1, o2) -> {
85+
final int o1Length = o1.phonemeText.length();
86+
final int o2Length = o2.phonemeText.length();
87+
for (int i = 0; i < o1Length; i++) {
88+
if (i >= o2Length) {
89+
return +1;
9790
}
98-
99-
if (o1Length < o2Length) {
100-
return -1;
91+
final int c = o1.phonemeText.charAt(i) - o2.phonemeText.charAt(i);
92+
if (c != 0) {
93+
return c;
10194
}
95+
}
10296

103-
return 0;
97+
if (o1Length < o2Length) {
98+
return -1;
10499
}
100+
101+
return 0;
105102
};
106103

107104
private final StringBuilder phonemeText;
@@ -194,12 +191,7 @@ public interface RPattern {
194191
boolean isMatch(CharSequence input);
195192
}
196193

197-
public static final RPattern ALL_STRINGS_RMATCHER = new RPattern() {
198-
@Override
199-
public boolean isMatch(final CharSequence input) {
200-
return true;
201-
}
202-
};
194+
public static final RPattern ALL_STRINGS_RMATCHER = input -> true;
203195

204196
public static final String ALL = "ALL";
205197

@@ -465,11 +457,7 @@ public String toString() {
465457
}
466458
};
467459
final String patternKey = r.pattern.substring(0,1);
468-
List<Rule> rules = lines.get(patternKey);
469-
if (rules == null) {
470-
rules = new ArrayList<>();
471-
lines.put(patternKey, rules);
472-
}
460+
final List<Rule> rules = lines.computeIfAbsent(patternKey, k -> new ArrayList<>());
473461
rules.add(r);
474462
} catch (final IllegalArgumentException e) {
475463
throw new IllegalStateException("Problem parsing line '" + currentLine + "' in " +
@@ -500,41 +488,21 @@ private static RPattern pattern(final String regex) {
500488
// exact match
501489
if (content.isEmpty()) {
502490
// empty
503-
return new RPattern() {
504-
@Override
505-
public boolean isMatch(final CharSequence input) {
506-
return input.length() == 0;
507-
}
508-
};
491+
return input -> input.length() == 0;
509492
}
510-
return new RPattern() {
511-
@Override
512-
public boolean isMatch(final CharSequence input) {
513-
return input.equals(content);
514-
}
515-
};
493+
return input -> input.equals(content);
516494
}
517495
if ((startsWith || endsWith) && content.isEmpty()) {
518496
// matches every string
519497
return ALL_STRINGS_RMATCHER;
520498
}
521499
if (startsWith) {
522500
// matches from start
523-
return new RPattern() {
524-
@Override
525-
public boolean isMatch(final CharSequence input) {
526-
return startsWith(input, content);
527-
}
528-
};
501+
return input -> startsWith(input, content);
529502
}
530503
if (endsWith) {
531504
// matches from start
532-
return new RPattern() {
533-
@Override
534-
public boolean isMatch(final CharSequence input) {
535-
return endsWith(input, content);
536-
}
537-
};
505+
return input -> endsWith(input, content);
538506
}
539507
} else {
540508
final boolean startsWithBox = content.startsWith("[");
@@ -553,31 +521,16 @@ public boolean isMatch(final CharSequence input) {
553521

554522
if (startsWith && endsWith) {
555523
// exact match
556-
return new RPattern() {
557-
@Override
558-
public boolean isMatch(final CharSequence input) {
559-
return input.length() == 1 && contains(bContent, input.charAt(0)) == shouldMatch;
560-
}
561-
};
524+
return input -> input.length() == 1 && contains(bContent, input.charAt(0)) == shouldMatch;
562525
}
563526
if (startsWith) {
564527
// first char
565-
return new RPattern() {
566-
@Override
567-
public boolean isMatch(final CharSequence input) {
568-
return input.length() > 0 && contains(bContent, input.charAt(0)) == shouldMatch;
569-
}
570-
};
528+
return input -> input.length() > 0 && contains(bContent, input.charAt(0)) == shouldMatch;
571529
}
572530
if (endsWith) {
573531
// last char
574-
return new RPattern() {
575-
@Override
576-
public boolean isMatch(final CharSequence input) {
577-
return input.length() > 0 &&
578-
contains(bContent, input.charAt(input.length() - 1)) == shouldMatch;
579-
}
580-
};
532+
return input -> input.length() > 0 &&
533+
contains(bContent, input.charAt(input.length() - 1)) == shouldMatch;
581534
}
582535
}
583536
}

0 commit comments

Comments
 (0)