All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mitre.caasd.commons.lambda.CheckedSupplier Maven / Gradle / Ivy

The newest version!
package org.mitre.caasd.commons.lambda;

import java.util.function.Supplier;

/**
 * A CheckedSupplier is similar to a {@link Supplier} EXCEPT it throws a checked exception.
 * 

* Unfortunately, a CheckedSupplier obfuscates code because they require using try-catch blocks. * This class and the convenience functions in {@link Uncheck}, allow you to improve the readability * of code (assuming you are willing to demote all checked exceptions to RuntimeExceptions). *

* For example: * *

{@code
 *     //code WITHOUT these utilities -- is harder to read and write
 *     Stream fileLines = null;
 *     try {
 *         fileLines = java.nio.lines(pathThFile);
 *     } catch (IOException ex) {
 *         throw DemotedException.demote(ex);
 *     }
 *     List subset = fileLines.stream()
 *         .filter(str -> str.contains("abcde"))
 *         .toList();
 *
 *
 *     //code WITH these utilities -- is easier to read and write
 *     Stream fileLines = Uncheck.supplier(() -> java.nio.lines(pathThFile));
 *     List subset = fileLines.stream()
 *         .filter(str -> str.contains("abcde"))
 *         .toList();
 * }
*/ @FunctionalInterface public interface CheckedSupplier { T get() throws Exception; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy