-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/org/apache/commons/io/input/IntPredicateFilterReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.commons.io.input; | ||
|
||
import java.io.Reader; | ||
import java.util.function.IntPredicate; | ||
|
||
/** | ||
* A filter reader that removes characters represented as {@code int} code points that match a predicate, | ||
* handy to remove white space characters for example. This class is the most efficient way to filter out | ||
* multiple characters using a simple predicate. | ||
* | ||
* @since 2.9.0 | ||
*/ | ||
public class IntPredicateFilterReader extends AbstractCharacterFilterReader { | ||
|
||
private static final IntPredicate SKIP_NONE = ch -> false; | ||
|
||
private final IntPredicate skip; | ||
|
||
/** | ||
* Constructs a new reader. | ||
* | ||
* @param reader | ||
* the reader to filter. | ||
* @param skip | ||
* the predicates that determines which characters are filtered out. | ||
*/ | ||
public IntPredicateFilterReader(final Reader reader, final IntPredicate skip) { | ||
super(reader); | ||
this.skip = skip == null ? SKIP_NONE : skip; | ||
} | ||
|
||
@Override | ||
protected boolean filter(final int ch) { | ||
return skip.test(ch); | ||
} | ||
|
||
} |
124 changes: 124 additions & 0 deletions
124
src/test/java/org/apache/commons/io/input/IntPredicateFilterReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.commons.io.input; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.io.output.StringBuilderWriter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class IntPredicateFilterReaderTest { | ||
|
||
@Test | ||
public void testInputSize0FilterAll() throws IOException { | ||
final StringReader input = new StringReader(""); | ||
try (IntPredicateFilterReader reader = new IntPredicateFilterReader(input, ch -> true)) { | ||
assertEquals(-1, reader.read()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testInputSize1FilterAll() throws IOException { | ||
try (StringReader input = new StringReader("a"); | ||
IntPredicateFilterReader reader = new IntPredicateFilterReader(input, ch -> true)) { | ||
assertEquals(-1, reader.read()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testInputSize2FilterAll() throws IOException { | ||
final StringReader input = new StringReader("aa"); | ||
try (IntPredicateFilterReader reader = new IntPredicateFilterReader(input, ch -> true)) { | ||
assertEquals(-1, reader.read()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testInputSize2FilterFirst() throws IOException { | ||
final StringReader input = new StringReader("ab"); | ||
try (IntPredicateFilterReader reader = new IntPredicateFilterReader(input, ch -> ch == 'a')) { | ||
assertEquals('b', reader.read()); | ||
assertEquals(-1, reader.read()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testInputSize2FilterLast() throws IOException { | ||
final StringReader input = new StringReader("ab"); | ||
try (IntPredicateFilterReader reader = new IntPredicateFilterReader(input, ch -> ch == 'b')) { | ||
assertEquals('a', reader.read()); | ||
assertEquals(-1, reader.read()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testInputSize5FilterWhitespace() throws IOException { | ||
final StringReader input = new StringReader(" a b "); | ||
try (IntPredicateFilterReader reader = new IntPredicateFilterReader(input, Character::isWhitespace)) { | ||
assertEquals('a', reader.read()); | ||
assertEquals('b', reader.read()); | ||
assertEquals(-1, reader.read()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testReadIntoBuffer() throws IOException { | ||
final StringReader input = new StringReader("ababcabcd"); | ||
try (IntPredicateFilterReader reader = new IntPredicateFilterReader(input, ch -> ch == 'b')) { | ||
final char[] buff = new char[9]; | ||
final int charCount = reader.read(buff); | ||
assertEquals(6, charCount); | ||
assertEquals("aacacd", new String(buff, 0, charCount)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testReadIntoBufferFilterWhitespace() throws IOException { | ||
final StringReader input = new StringReader(" a b a b c a b c d "); | ||
try (IntPredicateFilterReader reader = new IntPredicateFilterReader(input, Character::isWhitespace)) { | ||
final char[] buff = new char[19]; | ||
final int charCount = reader.read(buff); | ||
assertEquals(9, charCount); | ||
assertEquals("ababcabcd", new String(buff, 0, charCount)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testReadUsingReader() throws IOException { | ||
final StringReader input = new StringReader("ababcabcd"); | ||
try (StringBuilderWriter output = new StringBuilderWriter(); | ||
IntPredicateFilterReader reader = new IntPredicateFilterReader(input, ch -> ch == 'b')) { | ||
IOUtils.copy(reader, output); | ||
assertEquals("aacacd", output.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testReadUsingReaderFilterWhitespace() throws IOException { | ||
final StringReader input = new StringReader(" a b a b c a b c d "); | ||
try (StringBuilderWriter output = new StringBuilderWriter(); | ||
IntPredicateFilterReader reader = new IntPredicateFilterReader(input, Character::isWhitespace)) { | ||
IOUtils.copy(reader, output); | ||
assertEquals("ababcabcd", output.toString()); | ||
} | ||
} | ||
|
||
} |