-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add Uncheck.getAsInt(IOIntSupplier) - Add IOLongSupplier - Add Uncheck.getAsLong(IOLongSupplier)
- Loading branch information
1 parent
44876fc
commit 23460ad
Showing
13 changed files
with
390 additions
and
69 deletions.
There are no files selected for viewing
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
49 changes: 49 additions & 0 deletions
49
src/main/java/org/apache/commons/io/function/IOIntSupplier.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,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
49
src/main/java/org/apache/commons/io/function/IOLongSupplier.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,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; | ||
} |
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
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
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
69 changes: 69 additions & 0 deletions
69
src/test/java/org/apache/commons/io/function/IOIntSupplierTest.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,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()); | ||
} | ||
|
||
} |
Oops, something went wrong.