From 38d5c15d707b57220926a953c36ed40937e957c4 Mon Sep 17 00:00:00 2001 From: Lee Crabtree Date: Sun, 10 Jan 2016 02:35:56 -0600 Subject: [PATCH 1/5] adding project scaffolding --- .gitignore | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ build.gradle | 11 +++++++++ settings.gradle | 1 + 3 files changed, 77 insertions(+) create mode 100644 .gitignore create mode 100644 build.gradle create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..088a9c2 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..6861eb5 --- /dev/null +++ b/build.gradle @@ -0,0 +1,11 @@ +group 'com.patterns' + +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + compile 'org.slf4j:slf4j-log4j12:1.7.12' +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..9a5fffe --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = "highbar" From 607da378bfc68f04a5cabc677395116f29aee080 Mon Sep 17 00:00:00 2001 From: Lee Crabtree Date: Sun, 10 Jan 2016 02:42:31 -0600 Subject: [PATCH 2/5] adding .editorconfig --- .editorconfig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7d2b28e --- /dev/null +++ b/.editorconfig @@ -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 From 0d2697711d2f409d21fed1516add7b35887e7f52 Mon Sep 17 00:00:00 2001 From: Lee Crabtree Date: Sun, 10 Jan 2016 02:47:50 -0600 Subject: [PATCH 3/5] adding Checked* implementation --- .../java/com/highbar/function/Checked.java | 37 ++++++++++ .../com/highbar/function/CheckedConsumer.java | 37 ++++++++++ .../com/highbar/function/CheckedFunction.java | 67 +++++++++++++++++++ .../com/highbar/function/CheckedRunnable.java | 13 ++++ 4 files changed, 154 insertions(+) create mode 100644 src/main/java/com/highbar/function/Checked.java create mode 100644 src/main/java/com/highbar/function/CheckedConsumer.java create mode 100644 src/main/java/com/highbar/function/CheckedFunction.java create mode 100644 src/main/java/com/highbar/function/CheckedRunnable.java diff --git a/src/main/java/com/highbar/function/Checked.java b/src/main/java/com/highbar/function/Checked.java new file mode 100644 index 0000000..ed4912a --- /dev/null +++ b/src/main/java/com/highbar/function/Checked.java @@ -0,0 +1,37 @@ +package com.highbar.function; + +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * Provides checked function wrappers. + */ +public class Checked { + public static Runnable runnable(CheckedRunnable runnable) { + return () -> safeRun(input -> { + runnable.run(); + return null; + }, null); + } + + public static Consumer consumer(CheckedConsumer consumer) { + return data -> safeRun(input -> { + consumer.accept(input); + return null; + }, data); + } + + public static Function function(CheckedFunction function) { + return input -> safeRun(function, input); + } + + private static O safeRun(CheckedFunction function, I input) { + try { + return function.apply(input); + } catch (Error | RuntimeException error) { + throw error; + } catch (Throwable error) { + throw new RuntimeException(error); + } + } +} diff --git a/src/main/java/com/highbar/function/CheckedConsumer.java b/src/main/java/com/highbar/function/CheckedConsumer.java new file mode 100644 index 0000000..3c1c521 --- /dev/null +++ b/src/main/java/com/highbar/function/CheckedConsumer.java @@ -0,0 +1,37 @@ +package com.highbar.function; + +import java.util.Objects; + +/** + * Defines a consumer that checks an exception. + */ +@FunctionalInterface +public interface CheckedConsumer { + /** + * 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 andThen(CheckedConsumer after) { + Objects.requireNonNull(after); + + return (T t) -> { + accept(t); + after.accept(t); + }; + } +} diff --git a/src/main/java/com/highbar/function/CheckedFunction.java b/src/main/java/com/highbar/function/CheckedFunction.java new file mode 100644 index 0000000..ca5f963 --- /dev/null +++ b/src/main/java/com/highbar/function/CheckedFunction.java @@ -0,0 +1,67 @@ +package com.highbar.function; + +import java.util.Objects; + +/** + * Defines a function that checks an exception. + */ +@FunctionalInterface +public interface CheckedFunction { + /** + * 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 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 CheckedFunction compose(CheckedFunction 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 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 CheckedFunction andThen(CheckedFunction after) { + Objects.requireNonNull(after); + return (I input) -> after.apply(apply(input)); + } + + /** + * Returns a function that always returns its input argument. + * + * @param the type of the input and output objects to the function + * @return a function that always returns its input argument + */ + static CheckedFunction identity() { + return t -> t; + } +} diff --git a/src/main/java/com/highbar/function/CheckedRunnable.java b/src/main/java/com/highbar/function/CheckedRunnable.java new file mode 100644 index 0000000..17b3c5e --- /dev/null +++ b/src/main/java/com/highbar/function/CheckedRunnable.java @@ -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; +} From 831411ac6915c2104819804b6084a3d01116501a Mon Sep 17 00:00:00 2001 From: Lee Crabtree Date: Sun, 10 Jan 2016 03:06:53 -0600 Subject: [PATCH 4/5] adding CheckedPredicate --- src/main/java/com/highbar/function/Checked.java | 7 +++++++ src/main/java/com/highbar/function/CheckedPredicate.java | 9 +++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/main/java/com/highbar/function/CheckedPredicate.java diff --git a/src/main/java/com/highbar/function/Checked.java b/src/main/java/com/highbar/function/Checked.java index ed4912a..fb29605 100644 --- a/src/main/java/com/highbar/function/Checked.java +++ b/src/main/java/com/highbar/function/Checked.java @@ -2,6 +2,7 @@ import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; /** * Provides checked function wrappers. @@ -25,6 +26,12 @@ public static Function function(CheckedFunction function) { return input -> safeRun(function, input); } + public static Predicate predicate(CheckedPredicate predicate) { + return input -> safeRun(i -> { + return predicate.test(i); + }, input); + } + private static O safeRun(CheckedFunction function, I input) { try { return function.apply(input); diff --git a/src/main/java/com/highbar/function/CheckedPredicate.java b/src/main/java/com/highbar/function/CheckedPredicate.java new file mode 100644 index 0000000..f17c911 --- /dev/null +++ b/src/main/java/com/highbar/function/CheckedPredicate.java @@ -0,0 +1,9 @@ +package com.highbar.function; + +/** + * Defines a predicate that checks a function. + */ +@FunctionalInterface +public interface CheckedPredicate { + boolean test(T t) throws Exception; +} From 84b989b9f4e1a53107862fe24b028998db223775 Mon Sep 17 00:00:00 2001 From: Lee Crabtree Date: Mon, 11 Jan 2016 16:30:28 -0600 Subject: [PATCH 5/5] adding CheckedSupplier --- src/main/java/com/highbar/function/Checked.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/highbar/function/Checked.java b/src/main/java/com/highbar/function/Checked.java index fb29605..643370b 100644 --- a/src/main/java/com/highbar/function/Checked.java +++ b/src/main/java/com/highbar/function/Checked.java @@ -3,6 +3,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; /** * Provides checked function wrappers. @@ -32,6 +33,12 @@ public static Predicate predicate(CheckedPredicate predicate) { }, input); } + public static Supplier supplier(CheckedSupplier supplier) { + return () -> safeRun(input -> { + return supplier.get(); + }, null); + } + private static O safeRun(CheckedFunction function, I input) { try { return function.apply(input);