Skip to content

Commit

Permalink
Add IOIntSupplier
Browse files Browse the repository at this point in the history
- Add Uncheck.getAsInt(IOIntSupplier)
- Add IOLongSupplier
- Add Uncheck.getAsLong(IOLongSupplier)
  • Loading branch information
garydgregory committed Jun 23, 2023
1 parent 44876fc commit 23460ad
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 69 deletions.
12 changes: 12 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add FilesUncheck.find(Path, int, BiPredicate%lt;Path, BasicFileAttributes&gt;, FileVisitOption...)
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add IOIntSupplier.
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add Uncheck.getAsInt(IOIntSupplier).
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add IOLongSupplier.
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add Uncheck.getAsLong(IOLongSupplier).
</action>
<!-- FIX -->
<action dev="ggregory" type="fix" issue="IO-799" due-to="Jeroen van der Vegt, Gary Gregory">
ReaderInputStream.read() throws an exception instead of returning -1 when called again after returning -1.
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/org/apache/commons/io/function/IOIntSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.function;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

/**
* Like {@link IntSupplier} but throws {@link IOException}.
*
* @since 2.14.0
*/
@FunctionalInterface
public interface IOIntSupplier {

/**
* Creates a {@link Supplier} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
*
* @return an UncheckedIOException Supplier.
*/
default IntSupplier asIntSupplier() {
return () -> Uncheck.getAsInt(this);
}

/**
* Gets a result.
*
* @return a result
* @throws IOException if an I/O error occurs.
*/
int getAsInt() throws IOException;
}
49 changes: 49 additions & 0 deletions src/main/java/org/apache/commons/io/function/IOLongSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.function;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.LongSupplier;
import java.util.function.Supplier;

/**
* Like {@link IOLongSupplier} but throws {@link IOException}.
*
* @since 2.14.0
*/
@FunctionalInterface
public interface IOLongSupplier {

/**
* Creates a {@link Supplier} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
*
* @return an UncheckedIOException Supplier.
*/
default LongSupplier asSupplier() {
return () -> Uncheck.getAsLong(this);
}

/**
* Gets a result.
*
* @return a result
* @throws IOException if an I/O error occurs.
*/
long getAsLong() throws IOException;
}
32 changes: 32 additions & 0 deletions src/main/java/org/apache/commons/io/function/Uncheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,38 @@ public static <T> T get(final IOSupplier<T> supplier) {
}
}

/**
* Gets the result from an IO int supplier.
*
* @param supplier Supplies the return value.
* @return result from the supplier.
* @throws UncheckedIOException if an I/O error occurs.
* @since 2.14.0
*/
public static int getAsInt(final IOIntSupplier supplier) {
try {
return supplier.getAsInt();
} catch (final IOException e) {
throw wrap(e);
}
}

/**
* Gets the result from an IO long supplier.
*
* @param supplier Supplies the return value.
* @return result from the supplier.
* @throws UncheckedIOException if an I/O error occurs.
* @since 2.14.0
*/
public static long getAsLong(final IOLongSupplier supplier) {
try {
return supplier.getAsLong();
} catch (final IOException e) {
throw wrap(e);
}
}

/**
* Runs an IO runnable.
*
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/apache/commons/io/input/ReaderInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,6 @@ public Builder setCharsetEncoder(final CharsetEncoder newEncoder) {

}

private static CharsetEncoder newEncoder(final Charset charset) {
// @formatter:off
return Charsets.toCharset(charset).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
// @formatter:on
}

/**
* Constructs a new {@link Builder}.
*
Expand All @@ -179,6 +171,14 @@ static float minBufferSize(final CharsetEncoder charsetEncoder) {
return charsetEncoder.maxBytesPerChar() * 2;
}

private static CharsetEncoder newEncoder(final Charset charset) {
// @formatter:off
return Charsets.toCharset(charset).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
// @formatter:on
}

private final Reader reader;

private final CharsetEncoder charsetEncoder;
Expand Down
80 changes: 40 additions & 40 deletions src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,46 +111,6 @@ public void tearDown() {
theInstance = null;
}

@Test
public void testFileCleanerDirectoryFileSource() throws Exception {
TestUtils.createFile(testFile, 100);
assertTrue(testFile.exists());
assertTrue(tempDirFile.exists());

Object obj = new Object();
assertEquals(0, theInstance.getTrackCount());
theInstance.track(tempDirFile, obj);
assertEquals(1, theInstance.getTrackCount());

obj = null;

waitUntilTrackCount();

assertEquals(0, theInstance.getTrackCount());
assertTrue(testFile.exists()); // not deleted, as dir not empty
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
}

@Test
public void testFileCleanerDirectoryPathSource() throws Exception {
TestUtils.createFile(testPath, 100);
assertTrue(Files.exists(testPath));
assertTrue(Files.exists(tempDirPath));

Object obj = new Object();
assertEquals(0, theInstance.getTrackCount());
theInstance.track(tempDirPath, obj);
assertEquals(1, theInstance.getTrackCount());

obj = null;

waitUntilTrackCount();

assertEquals(0, theInstance.getTrackCount());
assertTrue(Files.exists(testPath)); // not deleted, as dir not empty
assertTrue(Files.exists(testPath.getParent())); // not deleted, as dir not empty
}

@Test
public void testFileCleanerDirectory_ForceStrategy_FileSource() throws Exception {
if (!testFile.getParentFile().exists()) {
Expand Down Expand Up @@ -227,6 +187,46 @@ public void testFileCleanerDirectory_NullStrategy() throws Exception {
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
}

@Test
public void testFileCleanerDirectoryFileSource() throws Exception {
TestUtils.createFile(testFile, 100);
assertTrue(testFile.exists());
assertTrue(tempDirFile.exists());

Object obj = new Object();
assertEquals(0, theInstance.getTrackCount());
theInstance.track(tempDirFile, obj);
assertEquals(1, theInstance.getTrackCount());

obj = null;

waitUntilTrackCount();

assertEquals(0, theInstance.getTrackCount());
assertTrue(testFile.exists()); // not deleted, as dir not empty
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
}

@Test
public void testFileCleanerDirectoryPathSource() throws Exception {
TestUtils.createFile(testPath, 100);
assertTrue(Files.exists(testPath));
assertTrue(Files.exists(tempDirPath));

Object obj = new Object();
assertEquals(0, theInstance.getTrackCount());
theInstance.track(tempDirPath, obj);
assertEquals(1, theInstance.getTrackCount());

obj = null;

waitUntilTrackCount();

assertEquals(0, theInstance.getTrackCount());
assertTrue(Files.exists(testPath)); // not deleted, as dir not empty
assertTrue(Files.exists(testPath.getParent())); // not deleted, as dir not empty
}

@Test
public void testFileCleanerExitWhenFinished_NoTrackAfter() {
assertFalse(theInstance.exitWhenFinished);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.function;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Tests {@link IOIntSupplier}.
*/
public class IOIntSupplierTest {

private AtomicInteger atomicInt;

private int getThrowsIO(final IOIntSupplier supplier) throws IOException {
return supplier.getAsInt();
}

private int getThrowsNone(final IOIntSupplier supplier) {
return supplier.asIntSupplier().getAsInt();
}

@BeforeEach
public void initEach() {
atomicInt = new AtomicInteger();
}

@Test
public void testAsSupplier() {
assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier().getAsInt());
assertEquals(1, getThrowsNone(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
assertEquals(1, atomicInt.get());
assertNotEquals(TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier(), TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier());
}

@Test
public void testGet() throws IOException {
assertThrows(IOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.getAsInt());
assertThrows(IOException.class, () -> {
throw new IOException();
});
assertEquals(1, getThrowsIO(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
assertEquals(1, atomicInt.get());
}

}
Loading

0 comments on commit 23460ad

Please sign in to comment.