Skip to content

Features/checked #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
##
# Default project editor config. Any changes to this in a given project should be offered as a pull request back into
# this project.
# See http://editorconfig.org
##

##
# Special property that should be specified at the top of the file outside of any sections. Set to true to stop
# .editorconfig files search on current file.
##
root = true

##
# Configuration for all files.
##
[*]
charset = utf-8

# Line length
max_line_length = 120

# Tab indentation
indent_size = 2
indent_style = space

# Line endings
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
##
# Gradle
##
build/
.gradle

# Ignore Gradle GUI config
gradle-app.setting

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

##
# Java
##
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files
*.ear
*.jar
*.war

# Virtual machine crash logs
spot.xml
hs_err_pid*

# Logs
logs
*.log
*.log.lck

##
# Security
##
*.cer
*.cert
*.crl
*.crt
*.csr
*.der
*.jks
*key
*.p12
*.p7b
*.pem
*.pfx
*.spc
*.sst
*.stl

##
# Project generic
##

# Dependency and Jar output location
lib/classpath

# Project configuration
config/log4j-local.xml

# Runtime data
*.outq
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
group 'com.patterns'

apply plugin: 'java'

repositories {
mavenCentral()
}

dependencies {
compile 'org.slf4j:slf4j-log4j12:1.7.12'
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "highbar"
51 changes: 51 additions & 0 deletions src/main/java/com/highbar/function/Checked.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.highbar.function;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
* Provides checked function wrappers.
*/
public class Checked {
public static Runnable runnable(CheckedRunnable runnable) {
return () -> safeRun(input -> {
runnable.run();
return null;
}, null);
}

public static <T> Consumer<T> consumer(CheckedConsumer<T> consumer) {
return data -> safeRun(input -> {
consumer.accept(input);
return null;
}, data);
}

public static <I, O> Function<I, O> function(CheckedFunction<I, O> function) {
return input -> safeRun(function, input);
}

public static <T> Predicate<T> predicate(CheckedPredicate<T> predicate) {
return input -> safeRun(i -> {
return predicate.test(i);
}, input);
}

public static <T> Supplier<T> supplier(CheckedSupplier<T> supplier) {
return () -> safeRun(input -> {
return supplier.get();
}, null);
}

private static <I,O> O safeRun(CheckedFunction<I,O> function, I input) {
try {
return function.apply(input);
} catch (Error | RuntimeException error) {
throw error;
} catch (Throwable error) {
throw new RuntimeException(error);
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/highbar/function/CheckedConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.highbar.function;

import java.util.Objects;

/**
* Defines a consumer that checks an exception.
*/
@FunctionalInterface
public interface CheckedConsumer<T> {
/**
* Accepts the provided data.
* @param data The data to accept.
* @throws Exception
*/
void accept(T data) throws Exception;

/**
* Returns a composed {@code CheckedConsumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code CheckedConsumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default CheckedConsumer<T> andThen(CheckedConsumer<? super T> after) {
Objects.requireNonNull(after);

return (T t) -> {
accept(t);
after.accept(t);
};
}
}
67 changes: 67 additions & 0 deletions src/main/java/com/highbar/function/CheckedFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.highbar.function;

import java.util.Objects;

/**
* Defines a function that checks an exception.
*/
@FunctionalInterface
public interface CheckedFunction<I, O> {
/**
* Applies this function to the given argument.
*
* @param input the function argument
* @return the function result
*/
O apply(I input) throws Exception;

/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(CheckedFunction)
*/
default <V> CheckedFunction<V, O> compose(CheckedFunction<? super V, ? extends I> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}

/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(CheckedFunction)
*/
default <V> CheckedFunction<I, V> andThen(CheckedFunction<? super O, ? extends V> after) {
Objects.requireNonNull(after);
return (I input) -> after.apply(apply(input));
}

/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> CheckedFunction<T, T> identity() {
return t -> t;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/highbar/function/CheckedPredicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.highbar.function;

/**
* Defines a predicate that checks a function.
*/
@FunctionalInterface
public interface CheckedPredicate<T> {
boolean test(T t) throws Exception;
}
13 changes: 13 additions & 0 deletions src/main/java/com/highbar/function/CheckedRunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.highbar.function;

/**
* Defines a runnable that checks an exception.
*/
@FunctionalInterface
public interface CheckedRunnable {
/**
* Runs this function.
* @throws Exception
*/
void run() throws Exception;
}