Skip to content

Commit

Permalink
Revise method and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgxbslgx committed Jan 5, 2021
1 parent 26b1589 commit f55e8a3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
21 changes: 18 additions & 3 deletions test/langtools/tools/javac/T8241187/T8241187.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,32 @@ public static void main(String[] args) {

String regex1 = ".*ana.*";
List<String> expected1 = Arrays.asList("apple", "cat", "dog", "end", "ending");
List<String> output1 = tb.grepNotMatch(regex1, input);
List<String> output1 = tb.grep(regex1, input, true);
tb.checkEqual(expected1, output1);

String regex2 = ".*nd.*";
List<String> expected2 = Arrays.asList("apple", "banana", "cat", "dog");
List<String> output2 = tb.grepNotMatch(regex2, input);
List<String> output2 = tb.grep(regex2, input, true);
tb.checkEqual(expected2, output2);

String regex3 = "apple";
List<String> expected3 = Arrays.asList("banana", "cat", "dog", "end", "ending");
List<String> output3 = tb.grepNotMatch(regex3, input);
List<String> output3 = tb.grep(regex3, input, true);
tb.checkEqual(expected3, output3);

String regex4 = ".*ana.*";
List<String> expected4 = Arrays.asList("banana");
List<String> output4 = tb.grep(regex4, input, false);
tb.checkEqual(expected4, output4);

String regex5 = ".*nd.*";
List<String> expected5 = Arrays.asList("end", "ending");
List<String> output5 = tb.grep(regex5, input, false);
tb.checkEqual(expected5, output5);

String regex6 = "apple";
List<String> expected6 = Arrays.asList("apple");
List<String> output6 = tb.grep(regex6, input, false);
tb.checkEqual(expected6, output6);
}
}
42 changes: 26 additions & 16 deletions test/langtools/tools/lib/toolbox/ToolBox.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -170,7 +170,7 @@ public void checkEqual(List<String> l1, List<String> l2) throws Error {
* @return the strings matching the regular expression
*/
public List<String> grep(String regex, List<String> lines) {
return grep(Pattern.compile(regex), lines);
return grep(Pattern.compile(regex), lines, false);
}

/**
Expand All @@ -180,31 +180,41 @@ public List<String> grep(String regex, List<String> lines) {
* @return the strings matching the regular expression
*/
public List<String> grep(Pattern pattern, List<String> lines) {
return lines.stream()
.filter(s -> pattern.matcher(s).find())
.collect(Collectors.toList());
return grep(pattern, lines, false);
}

/**
* Filters a list of strings which don't match the given regular expression.
* Filters a list of strings according to the given regular expression.
* @param regex the regular expression
* @param lines the strings to be filtered
* @return the strings not matching the regular expression
* @param invert identify positive or negative filtering
* true: negative filtering, return the unmatched strings
* false: positive filtering, return the matched strings
* @return the strings matching(or not matching) the regular expression
*/
public List<String> grepNotMatch(String regex, List<String> lines) {
return grepNotMatch(Pattern.compile(regex), lines);
public List<String> grep(String regex, List<String> lines, boolean invert) {
return grep(Pattern.compile(regex), lines, invert);
}

/**
* Filters a list of strings which don't match the given regular expression.
* Filters a list of strings according to the given regular expression.
* @param pattern the regular expression
* @param lines the strings to be filtered
* @return the strings not matching the regular expression
*/
public List<String> grepNotMatch(Pattern pattern, List<String> lines) {
return lines.stream()
.filter(s -> !pattern.matcher(s).find())
.collect(Collectors.toList());
* @param invert identify positive or negative filtering
* true: negative filtering, return the unmatched strings
* false: positive filtering, return the matched strings
* @return the strings matching(or not matching) the regular expression
*/
public List<String> grep(Pattern pattern, List<String> lines, boolean invert) {
if (invert) {
return lines.stream()
.filter(s -> !pattern.matcher(s).find())
.collect(Collectors.toList());
} else {
return lines.stream()
.filter(s -> pattern.matcher(s).find())
.collect(Collectors.toList());
}
}

/**
Expand Down

0 comments on commit f55e8a3

Please sign in to comment.