org.cactoos.func.IoCheckedFunc Java Examples

The following examples show how to use org.cactoos.func.IoCheckedFunc. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XsSheet.java    From excel-io with MIT License 5 votes vote down vote up
@Override
public Sheet attachTo(final Workbook workbook) throws IOException {
    final Sheet sheet = new IoCheckedFunc<>(this.func).apply(workbook);
    if (sheet == null) {
        throw new IOException("Sheet not found on specified index");
    }
    for (final ERow row : this.rows) {
        row.attachTo(sheet);
    }
    return sheet;
}
 
Example #2
Source File: ResourceOf.java    From cactoos with MIT License 5 votes vote down vote up
@Override
public InputStream stream() throws Exception {
    InputStream input = this.loader.getResourceAsStream(
        this.path.asString()
    );
    if (input == null) {
        input = new IoCheckedFunc<>(this.fallback)
            .apply(this.path)
            .stream();
    }
    return input;
}
 
Example #3
Source File: TypedPages.java    From jpeek with MIT License 5 votes vote down vote up
@Override
public Response apply(final String path) throws IOException {
    String type = "text/plain";
    if (path.endsWith(".html")) {
        type = "text/html";
    } else if (path.endsWith(".xml")) {
        type = "application/xml";
    } else if (path.endsWith(".svg")) {
        type = "image/svg+xml";
    }
    return new RsWithType(
        new IoCheckedFunc<>(this.origin).apply(path), type
    );
}
 
Example #4
Source File: Replaced.java    From cactoos with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * <p>
 * The given {@link Pattern regex} is used to produce a
 * {@link Pattern#matcher(java.lang.CharSequence) matcher} that will be
 * transformed by {@code func} into a replacement string to replace each
 * {@link Matcher#find() matching} substring.
 * <p>
 * Example usage:
 * <pre>{@code
 * final String result = new Replaced(
 *      new TextOf("one two THREE four FIVE six"),
 *      () -> Pattern.compile("[a-z]+"),
 *      matcher -> String.valueOf(matcher.group().length())
 * ).asString();  //will return the string "3 3 THREE 4 FIVE 3"
 * }</pre>
 * <p>
 * Note: a {@link PatternSyntaxException} will be thrown if the
 * regular expression's syntax is invalid.
 * @param text The text
 * @param regex The regular expression
 * @param func Transforms the resulting matcher object into a replacement
 *  string. Any exceptions will be wrapped in an {@link IOException}.
 */
public Replaced(
    final Text text,
    final Scalar<Pattern> regex,
    final Func<Matcher, String> func) {
    super((Scalar<String>) () -> {
        final StringBuffer buffer = new StringBuffer();
        final Matcher matcher = new IoChecked<>(regex)
            .value()
            .matcher(text.asString());
        final IoCheckedFunc<Matcher, String> safe =
            new IoCheckedFunc<>(func);
        while (matcher.find()) {
            matcher.appendReplacement(
                buffer,
                safe.apply(matcher)
            );
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    });
}
 
Example #5
Source File: InputWithFallback.java    From cactoos with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * @param input Main input
 * @param alt Alternative
 */
public InputWithFallback(final Input input,
    final IoCheckedFunc<IOException, Input> alt) {
    this.main = input;
    this.alternative = alt;
}
 
Example #6
Source File: InputWithFallback.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param input Main input
 * @param alt Alternative
 */
public InputWithFallback(final Input input,
    final Func<IOException, Input> alt) {
    this(input, new IoCheckedFunc<>(alt));
}