Skip to content

Commit 23460ad

Browse files
committed
Add IOIntSupplier
- Add Uncheck.getAsInt(IOIntSupplier) - Add IOLongSupplier - Add Uncheck.getAsLong(IOLongSupplier)
1 parent 44876fc commit 23460ad

13 files changed

Lines changed: 390 additions & 69 deletions

File tree

src/changes/changes.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ The <action> type attribute can be add,update,fix,remove.
6161
<action dev="ggregory" type="add" due-to="Gary Gregory">
6262
Add FilesUncheck.find(Path, int, BiPredicate%lt;Path, BasicFileAttributes&gt;, FileVisitOption...)
6363
</action>
64+
<action dev="ggregory" type="add" due-to="Gary Gregory">
65+
Add IOIntSupplier.
66+
</action>
67+
<action dev="ggregory" type="add" due-to="Gary Gregory">
68+
Add Uncheck.getAsInt(IOIntSupplier).
69+
</action>
70+
<action dev="ggregory" type="add" due-to="Gary Gregory">
71+
Add IOLongSupplier.
72+
</action>
73+
<action dev="ggregory" type="add" due-to="Gary Gregory">
74+
Add Uncheck.getAsLong(IOLongSupplier).
75+
</action>
6476
<!-- FIX -->
6577
<action dev="ggregory" type="fix" issue="IO-799" due-to="Jeroen van der Vegt, Gary Gregory">
6678
ReaderInputStream.read() throws an exception instead of returning -1 when called again after returning -1.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.function;
19+
20+
import java.io.IOException;
21+
import java.io.UncheckedIOException;
22+
import java.util.function.IntSupplier;
23+
import java.util.function.Supplier;
24+
25+
/**
26+
* Like {@link IntSupplier} but throws {@link IOException}.
27+
*
28+
* @since 2.14.0
29+
*/
30+
@FunctionalInterface
31+
public interface IOIntSupplier {
32+
33+
/**
34+
* Creates a {@link Supplier} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
35+
*
36+
* @return an UncheckedIOException Supplier.
37+
*/
38+
default IntSupplier asIntSupplier() {
39+
return () -> Uncheck.getAsInt(this);
40+
}
41+
42+
/**
43+
* Gets a result.
44+
*
45+
* @return a result
46+
* @throws IOException if an I/O error occurs.
47+
*/
48+
int getAsInt() throws IOException;
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.function;
19+
20+
import java.io.IOException;
21+
import java.io.UncheckedIOException;
22+
import java.util.function.LongSupplier;
23+
import java.util.function.Supplier;
24+
25+
/**
26+
* Like {@link IOLongSupplier} but throws {@link IOException}.
27+
*
28+
* @since 2.14.0
29+
*/
30+
@FunctionalInterface
31+
public interface IOLongSupplier {
32+
33+
/**
34+
* Creates a {@link Supplier} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
35+
*
36+
* @return an UncheckedIOException Supplier.
37+
*/
38+
default LongSupplier asSupplier() {
39+
return () -> Uncheck.getAsLong(this);
40+
}
41+
42+
/**
43+
* Gets a result.
44+
*
45+
* @return a result
46+
* @throws IOException if an I/O error occurs.
47+
*/
48+
long getAsLong() throws IOException;
49+
}

src/main/java/org/apache/commons/io/function/Uncheck.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,38 @@ public static <T> T get(final IOSupplier<T> supplier) {
199199
}
200200
}
201201

202+
/**
203+
* Gets the result from an IO int supplier.
204+
*
205+
* @param supplier Supplies the return value.
206+
* @return result from the supplier.
207+
* @throws UncheckedIOException if an I/O error occurs.
208+
* @since 2.14.0
209+
*/
210+
public static int getAsInt(final IOIntSupplier supplier) {
211+
try {
212+
return supplier.getAsInt();
213+
} catch (final IOException e) {
214+
throw wrap(e);
215+
}
216+
}
217+
218+
/**
219+
* Gets the result from an IO long supplier.
220+
*
221+
* @param supplier Supplies the return value.
222+
* @return result from the supplier.
223+
* @throws UncheckedIOException if an I/O error occurs.
224+
* @since 2.14.0
225+
*/
226+
public static long getAsLong(final IOLongSupplier supplier) {
227+
try {
228+
return supplier.getAsLong();
229+
} catch (final IOException e) {
230+
throw wrap(e);
231+
}
232+
}
233+
202234
/**
203235
* Runs an IO runnable.
204236
*

src/main/java/org/apache/commons/io/input/ReaderInputStream.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,6 @@ public Builder setCharsetEncoder(final CharsetEncoder newEncoder) {
148148

149149
}
150150

151-
private static CharsetEncoder newEncoder(final Charset charset) {
152-
// @formatter:off
153-
return Charsets.toCharset(charset).newEncoder()
154-
.onMalformedInput(CodingErrorAction.REPLACE)
155-
.onUnmappableCharacter(CodingErrorAction.REPLACE);
156-
// @formatter:on
157-
}
158-
159151
/**
160152
* Constructs a new {@link Builder}.
161153
*
@@ -179,6 +171,14 @@ static float minBufferSize(final CharsetEncoder charsetEncoder) {
179171
return charsetEncoder.maxBytesPerChar() * 2;
180172
}
181173

174+
private static CharsetEncoder newEncoder(final Charset charset) {
175+
// @formatter:off
176+
return Charsets.toCharset(charset).newEncoder()
177+
.onMalformedInput(CodingErrorAction.REPLACE)
178+
.onUnmappableCharacter(CodingErrorAction.REPLACE);
179+
// @formatter:on
180+
}
181+
182182
private final Reader reader;
183183

184184
private final CharsetEncoder charsetEncoder;

src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -111,46 +111,6 @@ public void tearDown() {
111111
theInstance = null;
112112
}
113113

114-
@Test
115-
public void testFileCleanerDirectoryFileSource() throws Exception {
116-
TestUtils.createFile(testFile, 100);
117-
assertTrue(testFile.exists());
118-
assertTrue(tempDirFile.exists());
119-
120-
Object obj = new Object();
121-
assertEquals(0, theInstance.getTrackCount());
122-
theInstance.track(tempDirFile, obj);
123-
assertEquals(1, theInstance.getTrackCount());
124-
125-
obj = null;
126-
127-
waitUntilTrackCount();
128-
129-
assertEquals(0, theInstance.getTrackCount());
130-
assertTrue(testFile.exists()); // not deleted, as dir not empty
131-
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
132-
}
133-
134-
@Test
135-
public void testFileCleanerDirectoryPathSource() throws Exception {
136-
TestUtils.createFile(testPath, 100);
137-
assertTrue(Files.exists(testPath));
138-
assertTrue(Files.exists(tempDirPath));
139-
140-
Object obj = new Object();
141-
assertEquals(0, theInstance.getTrackCount());
142-
theInstance.track(tempDirPath, obj);
143-
assertEquals(1, theInstance.getTrackCount());
144-
145-
obj = null;
146-
147-
waitUntilTrackCount();
148-
149-
assertEquals(0, theInstance.getTrackCount());
150-
assertTrue(Files.exists(testPath)); // not deleted, as dir not empty
151-
assertTrue(Files.exists(testPath.getParent())); // not deleted, as dir not empty
152-
}
153-
154114
@Test
155115
public void testFileCleanerDirectory_ForceStrategy_FileSource() throws Exception {
156116
if (!testFile.getParentFile().exists()) {
@@ -227,6 +187,46 @@ public void testFileCleanerDirectory_NullStrategy() throws Exception {
227187
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
228188
}
229189

190+
@Test
191+
public void testFileCleanerDirectoryFileSource() throws Exception {
192+
TestUtils.createFile(testFile, 100);
193+
assertTrue(testFile.exists());
194+
assertTrue(tempDirFile.exists());
195+
196+
Object obj = new Object();
197+
assertEquals(0, theInstance.getTrackCount());
198+
theInstance.track(tempDirFile, obj);
199+
assertEquals(1, theInstance.getTrackCount());
200+
201+
obj = null;
202+
203+
waitUntilTrackCount();
204+
205+
assertEquals(0, theInstance.getTrackCount());
206+
assertTrue(testFile.exists()); // not deleted, as dir not empty
207+
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
208+
}
209+
210+
@Test
211+
public void testFileCleanerDirectoryPathSource() throws Exception {
212+
TestUtils.createFile(testPath, 100);
213+
assertTrue(Files.exists(testPath));
214+
assertTrue(Files.exists(tempDirPath));
215+
216+
Object obj = new Object();
217+
assertEquals(0, theInstance.getTrackCount());
218+
theInstance.track(tempDirPath, obj);
219+
assertEquals(1, theInstance.getTrackCount());
220+
221+
obj = null;
222+
223+
waitUntilTrackCount();
224+
225+
assertEquals(0, theInstance.getTrackCount());
226+
assertTrue(Files.exists(testPath)); // not deleted, as dir not empty
227+
assertTrue(Files.exists(testPath.getParent())); // not deleted, as dir not empty
228+
}
229+
230230
@Test
231231
public void testFileCleanerExitWhenFinished_NoTrackAfter() {
232232
assertFalse(theInstance.exitWhenFinished);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.function;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import java.io.IOException;
25+
import java.io.UncheckedIOException;
26+
import java.util.concurrent.atomic.AtomicInteger;
27+
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
/**
32+
* Tests {@link IOIntSupplier}.
33+
*/
34+
public class IOIntSupplierTest {
35+
36+
private AtomicInteger atomicInt;
37+
38+
private int getThrowsIO(final IOIntSupplier supplier) throws IOException {
39+
return supplier.getAsInt();
40+
}
41+
42+
private int getThrowsNone(final IOIntSupplier supplier) {
43+
return supplier.asIntSupplier().getAsInt();
44+
}
45+
46+
@BeforeEach
47+
public void initEach() {
48+
atomicInt = new AtomicInteger();
49+
}
50+
51+
@Test
52+
public void testAsSupplier() {
53+
assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier().getAsInt());
54+
assertEquals(1, getThrowsNone(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
55+
assertEquals(1, atomicInt.get());
56+
assertNotEquals(TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier(), TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier());
57+
}
58+
59+
@Test
60+
public void testGet() throws IOException {
61+
assertThrows(IOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.getAsInt());
62+
assertThrows(IOException.class, () -> {
63+
throw new IOException();
64+
});
65+
assertEquals(1, getThrowsIO(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
66+
assertEquals(1, atomicInt.get());
67+
}
68+
69+
}

0 commit comments

Comments
 (0)