diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml new file mode 100644 index 0000000..ebe0352 --- /dev/null +++ b/.github/workflows/pull-request.yaml @@ -0,0 +1,20 @@ +on: + pull_request: + branches: + - master + +name: PullRequest +jobs: + checkstyle: + name: checkstyle + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v2 + - name: Set up JDK + uses: actions/setup-java@v2 + with: + distribution: 'temurin' + java-version: '17' + - name: Run Checkstyle + run: mvn checkstyle:check diff --git a/.gitignore b/.gitignore index 744289d..7b98e63 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Project exclude paths -/target/ \ No newline at end of file +/target/ +.idea \ No newline at end of file diff --git a/README.md b/README.md index cfc0c75..67a745f 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Currently, the major features of Functjonal are: import de.variantsync.functjonal.category.Monoid; public record PassedHours(int hours, int minutes) { - public static final Monoid MONOID = Monoid.From( + public static final Monoid MONOID = Monoid.from( () -> new PassedHours(0, 0), (a, b) -> new PassedHours( a.hours() + b.hours() + ((a.minutes() + b.minutes()) / 60), diff --git a/checks/checkstyle.xml b/checks/checkstyle.xml new file mode 100644 index 0000000..95b15b3 --- /dev/null +++ b/checks/checkstyle.xml @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 09d8266..2a298b1 100644 --- a/pom.xml +++ b/pom.xml @@ -14,4 +14,24 @@ UTF-8 + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.2 + + ${basedir}/checks/checkstyle.xml + + + + com.puppycrawl.tools + checkstyle + 9.2.1 + + + + + + \ No newline at end of file diff --git a/src/main/java/de/variantsync/functjonal/Cast.java b/src/main/java/de/variantsync/functjonal/Cast.java index 997b14b..525e8d5 100644 --- a/src/main/java/de/variantsync/functjonal/Cast.java +++ b/src/main/java/de/variantsync/functjonal/Cast.java @@ -1,7 +1,8 @@ package de.variantsync.functjonal; public abstract class Cast { - private Cast() {} + private Cast() { + } @SuppressWarnings("unchecked") public static To unchecked(final From b) { diff --git a/src/main/java/de/variantsync/functjonal/Functjonal.java b/src/main/java/de/variantsync/functjonal/Functjonal.java index 3d85bca..8d170d0 100644 --- a/src/main/java/de/variantsync/functjonal/Functjonal.java +++ b/src/main/java/de/variantsync/functjonal/Functjonal.java @@ -23,17 +23,17 @@ public class Functjonal { /// Containers public static Map bimap( - Map m, - Function key, - Function val) { + final Map m, + final Function key, + final Function val) { return bimap(m, key, val, HashMap::new); } public static > M bimap( - Map m, - Function key, - Function val, - Supplier mapFactory) { + final Map m, + final Function key, + final Function val, + final Supplier mapFactory) { final M result = mapFactory.get(); for (final Map.Entry e : m.entrySet()) { result.put(key.apply(e.getKey()), val.apply(e.getValue())); @@ -47,7 +47,11 @@ public static List map(final List a, final Function /// Pattern matching - public static B match(final Optional ma, final Function just, final Supplier nothing) { + public static B match( + final Optional ma, + final Function just, + final Supplier nothing + ) { final Optional x = ma.map(just); return x.orElseGet(nothing); } @@ -55,16 +59,25 @@ public static B match(final Optional ma, final Function Function, B> match(final Function just, final Supplier nothing) { + public static Function, B> match( + final Function just, + final Supplier nothing + ) { return ma -> match(ma, just, nothing); } - public static Function, C> match(final Function success, final Function failure) { + public static Function, C> match( + final Function success, + final Function failure + ) { return ma -> ma.match(success, failure); } - public static Consumer when(Predicate condition, Consumer task) { + public static Consumer when( + final Predicate condition, + final Consumer task + ) { return t -> { if (condition.test(t)) { task.accept(t); @@ -74,12 +87,18 @@ public static Consumer when(Predicate condition, Consumer t /** * Creates a branching function for given condition, then and else case. + * * @param condition The condition choosing whether to run 'then' or 'otherwise'. - * @param then The function to apply when the given condition is met for a given a. + * @param then The function to apply when the given condition is met for a given a. * @param otherwise The function to apply when the given condition is not met for a given a. - * @return A function that for a given a, returns then(a) if the given condition is met, and otherwise returns otherwise(a). + * @return A function that for a given a, returns then(a) if the given condition is met, + * and otherwise returns otherwise(a). */ - public static Function when(final Predicate condition, final Function then, final Function otherwise) { + public static Function when( + final Predicate condition, + final Function then, + final Function otherwise + ) { return a -> condition.test(a) ? then.apply(a) : otherwise.apply(a); } @@ -99,51 +118,57 @@ public static Function when(final Supplier then, final Suppli /// Java to FP - public static Function Lift(final Consumer f) { + public static Function lift(final Consumer f) { return a -> { f.accept(a); - return Unit.Instance(); + return Unit.instance(); }; } - public static Supplier Lift(final Procedure f) { + public static Supplier lift(final Procedure f) { return () -> { f.run(); - return Unit.Instance(); + return Unit.instance(); }; } - public static FragileSupplier LiftFragile(final FragileProcedure f) { + public static FragileSupplier liftFragile(final FragileProcedure f) { return () -> { f.run(); - return Unit.Instance(); + return Unit.instance(); }; } /** * Maps the given function f onto the given value a if a is not null. * - * @param n A nullable value that should be converted to a value of type B via f. - * @param f A function that should be mapped onto a. f can safely assume that any arguments passed to it are not null. + * @param n A nullable value that should be converted to a value of type B via f. + * @param f A function that should be mapped onto a. + * f can safely assume that any arguments passed to it are not null. * @param errorMessage Creates an error message in case f threw an exception of type E. - * @param The type of the nullable value a. - * @param The result type. - * @param The type of an exception that might be thrown by f. + * @param The type of the nullable value a. + * @param The result type. + * @param The type of an exception that might be thrown by f. * @return Returns the result of f(a) if a is not null and f(a) did not throw an exception of type E. - * Returns Optional.empty() if a is null or f(a) threw an exception of type E. + * Returns Optional.empty() if a is null or f(a) threw an exception of type E. */ - public static Optional mapFragile(final Nullable n, final FragileFunction f, final Supplier errorMessage) { + public static Optional mapFragile( + final Nullable n, + final FragileFunction f, + final Supplier errorMessage + ) { return Optional.ofNullable(n).flatMap(a -> Result.Try(() -> f.run(a)).match( Optional::ofNullable, // actually the returned B can also be null, thus ofNullable here - exception -> { -// Logger.error(errorMessage.get(), exception); - return Optional.empty(); - }) + exception -> Optional.empty() + ) ); } - public static Lazy> mapFragileLazily(final A a, final FragileFunction f, final Supplier errorMessage) { + public static Lazy> mapFragileLazily( + final A a, final FragileFunction f, + final Supplier errorMessage + ) { return Lazy.of(() -> mapFragile(a, f, errorMessage)); } } diff --git a/src/main/java/de/variantsync/functjonal/Lazy.java b/src/main/java/de/variantsync/functjonal/Lazy.java index 3e7d4a6..d6dcc4f 100644 --- a/src/main/java/de/variantsync/functjonal/Lazy.java +++ b/src/main/java/de/variantsync/functjonal/Lazy.java @@ -14,33 +14,35 @@ * Using lazy allows to compose such computations without ever running the actual computation. * This way, writing a program is separated from evaluating the program. * (This lazy monad is actually the reader monad with an empty environment + a cache.) - * + *

* Use Lazy to make explicit the points in computation when we interact with the environment. * In particular, in that moment, when you access the lazy's content, all necessary computations will run. * * @param The return type of this lazy computation. */ @SuppressWarnings("rawtypes") -public class Lazy implements Functor, CachedValue { +public final class Lazy implements Functor, CachedValue { /** * Lazy is a semigroup if the lazy values form a semigroup. + * * @param s Semigroup over values. * @return A semigroup for lazy values of type A. */ - public static Semigroup> SEMIGROUP(final Semigroup s) { + public static Semigroup> semigroup(final Semigroup s) { return (a, b) -> Lazy.of(() -> s.append(a.run(), b.run())); } /** * Lazy is a monoid if the lazy values are monoidal. * Creates a Monoid for Lazy from the monoid of the value type A. + * * @param m Monoid over values. * @return a Monoid for Lazy. */ - public static Monoid> MONOID(final Monoid m) { - return Monoid.From( + public static Monoid> monoid(final Monoid m) { + return Monoid.from( () -> Lazy.pure(m.neutral()), - SEMIGROUP(m) + semigroup(m) ); } @@ -60,6 +62,7 @@ private Lazy(final Supplier get) { /** * Creates a new Lazy encapsulating the given (expensive) computation. + * * @param f The computation that produces the value of the lazy when accessed. * @return A lazy object encapsulating the given computation. */ @@ -75,6 +78,7 @@ public static Lazy of(final Supplier f) { * an expensive computation of the value (b) instead of just wrapping that value. * However, pure allows to lift a value to a lazy such that it can be combined with other Lazys * (e.g., with map, then, or bind). + * * @param b The value to cache. * @return A lazy caching the given value. */ @@ -84,6 +88,7 @@ public static Lazy pure(final B b) { /** * Run the lazy computation and obtain the result. + * * @return The result of this lazy computation. */ public A run() { @@ -98,6 +103,7 @@ public A run() { /** * Run the lazy computation, obtain the result, and immediately forget it. * This method first calls {@link #run()} and then {@link #forget()}. + * * @return The result of this lazy computation. */ public A take() { @@ -115,6 +121,7 @@ public void forget() { /** * Lazy is a functor. + * * @param f Function to apply to the result of this Lazy when it is computed. * @return Composed Lazy that applies f to the result of this Lazy after computation. */ @@ -128,6 +135,7 @@ public Lazy map(final Function f) { * then discards the result * and returns s.get(). * "l.then(s)" is equivalent to "l.map(x -> s.get())" + * * @param s The new computation to run after this one. * @return A new Lazy that runs this Lazy but returns the result of s. */ @@ -141,6 +149,7 @@ public Lazy then(final Supplier s) { /** * Lazy is an applicative functor. + * * @param lf Lazy that holds a function to apply to this Lazy's result after computation (similar to map). * @return Composed Lazy that applies the function computed by lf to the result of this Lazy after computation. */ @@ -150,8 +159,10 @@ public Lazy splat(final Lazy> lf) { /** * Lazy is a monad. - * Chains the given lazy computation with this one (i.e., applies the given lazy computation to the result of this Lazy once its computed). + * Chains the given lazy computation with this one + * (i.e., applies the given lazy computation to the result of this Lazy once its computed). * Another common name for bind is flatMap. + * * @param f A lazy computation to chain to this one. * @return Returns a new lazy computation composed of this and the given Lazy. */ @@ -162,6 +173,7 @@ public Lazy bind(final Function> f) { /** * Flattens a nested Lazy. + * * @param l A nested Lazy that should be flattened to a single Lazy. * @return A new Lazy that returns the result of the innermost Lazy. */ @@ -171,6 +183,7 @@ public static Lazy join(final Lazy> l) { /** * Combines two lazy computation to a single one that returns both their results. + * * @param other The lazy to run together with this Lazy. * @return A new Lazy running "this" and "other" and returning the results in a pair. */ diff --git a/src/main/java/de/variantsync/functjonal/Product.java b/src/main/java/de/variantsync/functjonal/Product.java index 2ee0468..cf399ee 100644 --- a/src/main/java/de/variantsync/functjonal/Product.java +++ b/src/main/java/de/variantsync/functjonal/Product.java @@ -8,12 +8,13 @@ public record Product(A first, B second) { /** * Products are monoids over monoidal values. * Creates a Monoid for Product from monoids of for A and B. + * * @param ma Monoid over values of type A. * @param mb Monoid over values of type B. * @return a Monoid for Pair. */ - public static Monoid> MONOID(final Monoid ma, final Monoid mb) { - return Monoid.From( + public static Monoid> monoid(final Monoid ma, final Monoid mb) { + return Monoid.from( () -> new Product<>(ma.neutral(), mb.neutral()), (a, b) -> new Product<>(ma.append(a.first, b.first), mb.append(a.second, b.second)) ); diff --git a/src/main/java/de/variantsync/functjonal/Result.java b/src/main/java/de/variantsync/functjonal/Result.java index 5cefa79..9a0493b 100644 --- a/src/main/java/de/variantsync/functjonal/Result.java +++ b/src/main/java/de/variantsync/functjonal/Result.java @@ -12,6 +12,7 @@ /** * Type to capture results of computations that might fail. * A result reflects either the return value of a successful computation or a failure state. + * * @param Type for values in case of success. * @param Type for values in case of failure. */ @@ -20,7 +21,7 @@ public class Result { * Combines two results. * Returns failure if at least one of the given results is a failure. */ - public static Semigroup> SEMIGROUP(final Semigroup sg, final Semigroup fg) { + public static Semigroup> semigroup(final Semigroup sg, final Semigroup fg) { return (a, b) -> { final Result resultWithPotentialFailure = a.isFailure() ? a : b; final Result other = a.isFailure() ? b : a; @@ -36,10 +37,10 @@ public static Semigroup> SEMIGROUP(final Semigroup sg, fi * Returns failure if at least one of the given results is a failure. * The neutral value is success with the neutral value of sm. */ - public static Monoid> MONOID(final Monoid sm, final Semigroup fm) { - return Monoid.From( - () -> Result.Success(sm.neutral()), - SEMIGROUP(sm, fm) + public static Monoid> monoid(final Monoid sm, final Semigroup fm) { + return Monoid.from( + () -> Result.success(sm.neutral()), + semigroup(sm, fm) ); } @@ -50,7 +51,8 @@ public static Monoid> MONOID(final Monoid sm, final Semig /** * Creates a new Result where exactly one of the arguments is excepted to be null. - * @param result Success value or null + * + * @param result Success value or null * @param failure Failure value or null */ protected Result(final SuccessType result, final FailureType failure) { @@ -62,20 +64,22 @@ protected Result(final SuccessType result, final FailureType failure) { /** * Creates a successful result with the given value. + * * @param s Return value of the result. * @return Success value. */ - public static Result Success(final S s) { + public static Result success(final S s) { return new Result<>(s, null); } /** * Creates a failed result with the given error value. + * * @param f Value indicating failure. * @return Failure result. */ - public static Result Failure(final F f) { + public static Result failure(final F f) { return new Result<>(null, f); } @@ -84,15 +88,16 @@ public static Result Failure(final F f) { * Running f will be interpreted as a success iff f returns true. * Running f will be interpreted as a failure iff f returns false. * In case of failure, a failure value will be produced with the given failure supplier. - * @param f Computation to run that indicates success with a boolean return value. + * + * @param f Computation to run that indicates success with a boolean return value. * @param failure Factory for failure message in case f returned false. * @return Success iff f returned true, Failure otherwise. */ - public static Result FromFlag(final Supplier f, final Supplier failure) { + public static Result fromFlag(final Supplier f, final Supplier failure) { if (f.get()) { - return Success(Unit.Instance()); + return success(Unit.instance()); } else { - return Failure(failure.get()); + return failure(failure.get()); } } @@ -100,38 +105,44 @@ public static Result FromFlag(final Supplier f, final Supp * Runs the given computation that indicates success by returning a boolean and that may throw an exception. * Running f will be interpreted as a success iff f returns true and throws no exception. * Running f will be interpreted as a failure iff f returns false or throws an exception. - * If f did not throw an exception but returned false, a failure value will be produced with the given failure supplier. - * @param f Computation to run that indicates success with a boolean return value or an exception. + * If f did not throw an exception but returned false, + * a failure value will be produced with the given failure supplier. + * + * @param f Computation to run that indicates success with a boolean return value or an exception. * @param failure Factory for failure message in case f returned false. * @return Success iff f returned true and did not throw an exception, Failure otherwise. */ - public static Result FromFlag(final FragileSupplier f, final Supplier failure) { + public static Result fromFlag( + final FragileSupplier f, + final Supplier failure + ) { return Try(f).bibind( Functjonal.when( - () -> Success(Unit.Instance()), - () -> Failure(failure.get()) + () -> success(Unit.instance()), + () -> failure(failure.get()) ), - Result::Failure + Result::failure ); } /** * Runs the given computation that may throw an exception. - * @param s Computation to run. + * + * @param s Computation to run. * @param The type of exception that may be thrown by s. * @return A result containing the result of the given computation or the exception in case it was thrown. */ - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "checkstyle:TodoComment", "checkstyle:MethodName"}) public static Result Try(final FragileSupplier s) { try { final S result = s.get(); - return Result.Success(result); + return Result.success(result); } catch (final Exception e) { // We cannot catch E directly. if (HARD_CRASH_ON_TRY) { throw new RuntimeException(e); } else { // TODO: This cast might be impossible! - return Result.Failure((E) e); + return Result.failure((E) e); } } } @@ -139,12 +150,14 @@ public static Result Try(final FragileSupplier The type of exception that may be thrown by s. * @return A result containing the result of the given computation or the exception in case it was thrown. */ + @SuppressWarnings("checkstyle:MethodName") public static Result Try(final FragileProcedure s) { - return Try(Functjonal.LiftFragile(s)); + return Try(Functjonal.liftFragile(s)); } /// Operations @@ -166,11 +179,14 @@ public Result mapFail(final Function fail /** * Result is a bifunctor. */ - public Result bimap(final Function successCase, final Function failureCase) { + public Result bimap( + final Function successCase, + final Function failureCase + ) { if (isSuccess()) { - return Success(successCase.apply(result)); + return success(successCase.apply(result)); } else { - return Failure(failureCase.apply(failure)); + return failure(failureCase.apply(failure)); } } @@ -186,7 +202,7 @@ public Result bind(final Function Result bindFail(final Function Result bibind(final Function> successCase, final Function> failureCase) { + public Result bibind( + final Function> successCase, + final Function> failureCase + ) { if (isSuccess()) { return successCase.apply(result); } else { @@ -208,6 +227,7 @@ public Result bibind(final Function /** * Flattens a nested Result. + * * @param r A nested Result that should be flattened to a single Result. * @return Flattened result. */ @@ -215,7 +235,7 @@ public static Result join(final Result, B> r) { if (r.isSuccess()) { return r.getSuccess(); } else { - return Failure(r.getFailure()); + return failure(r.getFailure()); } } @@ -235,6 +255,7 @@ public FailureType getFailure() { return failure; } + @SuppressWarnings("checkstyle:EmptyBlock") public void assertSuccess() { if (isFailure()) { // Logger.error(getFailure().toString()); @@ -251,7 +272,9 @@ public SuccessType expect(final String message) { } public static S expect(final Result result) { - return result.match(Function.identity(), e -> {throw new RuntimeException(e);}); + return result.match(Function.identity(), e -> { + throw new RuntimeException(e); + }); } public void ifSuccess(final Consumer f) { diff --git a/src/main/java/de/variantsync/functjonal/Unit.java b/src/main/java/de/variantsync/functjonal/Unit.java index ea3781e..61943a6 100644 --- a/src/main/java/de/variantsync/functjonal/Unit.java +++ b/src/main/java/de/variantsync/functjonal/Unit.java @@ -1,19 +1,19 @@ package de.variantsync.functjonal; import de.variantsync.functjonal.category.Monoid; -import de.variantsync.functjonal.category.Semigroup; /** * Unit represents a type that has exactly one value (Instance()). */ -public class Unit { - private static final Unit instance = new Unit(); - public static final Monoid MONOID = Monoid.From(() -> instance, (a, b) -> instance); +public final class Unit { + private static final Unit INSTANCE = new Unit(); + public static final Monoid MONOID = Monoid.from(() -> INSTANCE, (a, b) -> INSTANCE); - private Unit() {} + private Unit() { + } - public static Unit Instance() { - return instance; + public static Unit instance() { + return INSTANCE; } @Override diff --git a/src/main/java/de/variantsync/functjonal/category/InplaceMonoid.java b/src/main/java/de/variantsync/functjonal/category/InplaceMonoid.java index a64e7e4..81d0a41 100644 --- a/src/main/java/de/variantsync/functjonal/category/InplaceMonoid.java +++ b/src/main/java/de/variantsync/functjonal/category/InplaceMonoid.java @@ -7,7 +7,7 @@ * @see InplaceSemigroup */ public interface InplaceMonoid extends Monoid, InplaceSemigroup { - static InplaceMonoid From(final Supplier empty, final InplaceSemigroup compose) { + static InplaceMonoid from(final Supplier empty, final InplaceSemigroup compose) { return new LambdaInplaceMonoid<>(empty, compose); } } diff --git a/src/main/java/de/variantsync/functjonal/category/InplaceSemigroup.java b/src/main/java/de/variantsync/functjonal/category/InplaceSemigroup.java index af00688..b418452 100644 --- a/src/main/java/de/variantsync/functjonal/category/InplaceSemigroup.java +++ b/src/main/java/de/variantsync/functjonal/category/InplaceSemigroup.java @@ -12,7 +12,7 @@ */ @FunctionalInterface public interface InplaceSemigroup extends Semigroup { - void appendToFirst(final T a, final T b); + void appendToFirst(T a, T b); default T append(final T a, final T b) { appendToFirst(a, b); diff --git a/src/main/java/de/variantsync/functjonal/category/LambdaMonoid.java b/src/main/java/de/variantsync/functjonal/category/LambdaMonoid.java index 06d9cfa..bf03008 100644 --- a/src/main/java/de/variantsync/functjonal/category/LambdaMonoid.java +++ b/src/main/java/de/variantsync/functjonal/category/LambdaMonoid.java @@ -9,7 +9,7 @@ public M neutral() { } @Override - public M append(M a, M b) { + public M append(final M a, final M b) { return compose.append(a, b); } } diff --git a/src/main/java/de/variantsync/functjonal/category/MonadTransformer.java b/src/main/java/de/variantsync/functjonal/category/MonadTransformer.java index efd01cf..7667e31 100644 --- a/src/main/java/de/variantsync/functjonal/category/MonadTransformer.java +++ b/src/main/java/de/variantsync/functjonal/category/MonadTransformer.java @@ -12,8 +12,9 @@ * (such as MaybeT or StateT from haskell) without a decent amount of janky hacks. * Thus, this class contains the implementations of monad transformers fixed in the input monad type. */ -public class MonadTransformer { - private MonadTransformer() {} +public final class MonadTransformer { + private MonadTransformer() { + } /// Lazy> @@ -21,7 +22,7 @@ public static Lazy> bind(final Lazy> m, final Fun return m.bind(Functjonal.match( /* Just a */ f, /* Nothing */ () -> Lazy.of(Optional::empty) - )); + )); } public static Lazy> pure(final A a) { diff --git a/src/main/java/de/variantsync/functjonal/category/Monoid.java b/src/main/java/de/variantsync/functjonal/category/Monoid.java index a2039c3..e70d7d4 100644 --- a/src/main/java/de/variantsync/functjonal/category/Monoid.java +++ b/src/main/java/de/variantsync/functjonal/category/Monoid.java @@ -4,12 +4,13 @@ /** * A semigroup with a neutral element w.r.t. composition. + * * @param The type that forms a monoid. */ public interface Monoid extends Semigroup { M neutral(); - static Monoid From(final Supplier empty, final Semigroup compose) { + static Monoid from(final Supplier empty, final Semigroup compose) { return new LambdaMonoid<>(empty, compose); } } diff --git a/src/main/java/de/variantsync/functjonal/category/Semigroup.java b/src/main/java/de/variantsync/functjonal/category/Semigroup.java index b6837cf..f9b81b3 100644 --- a/src/main/java/de/variantsync/functjonal/category/Semigroup.java +++ b/src/main/java/de/variantsync/functjonal/category/Semigroup.java @@ -2,6 +2,7 @@ /** * A binary operator over values T. + * * @param Type that forms a semigroup. */ @FunctionalInterface @@ -9,18 +10,21 @@ public interface Semigroup { /** * Composes the two values a and b and returns the result. */ - T append(final T a, final T b); + T append(T a, T b); /** * Asserts that any two values to combine are in fact equal and thus always picks the first element. * If two values to combine are not equals, throws a SemiGroupCannotAppend exception. + * * @param Type of group. - * @return A semigroup which can only append equal elements and throws an error when invoked with two different elements. + * @return A semigroup which can only append equal elements and + * throws an error when invoked with two different elements. */ static Semigroup assertEquals() { return (a, b) -> { if (!a.equals(b)) { - throw new SemigroupCannotAppend("Assertion failed. The following objects where assumed to be equal but are not: \"" + a + "\"; \"" + b + "\"!"); + throw new SemigroupCannotAppend("Assertion failed. " + + "The following objects where assumed to be equal but are not: \"" + a + "\"; \"" + b + "\"!"); } return a; }; diff --git a/src/main/java/de/variantsync/functjonal/category/SemigroupCannotAppend.java b/src/main/java/de/variantsync/functjonal/category/SemigroupCannotAppend.java index 91ff0d2..9f06a37 100644 --- a/src/main/java/de/variantsync/functjonal/category/SemigroupCannotAppend.java +++ b/src/main/java/de/variantsync/functjonal/category/SemigroupCannotAppend.java @@ -1,7 +1,7 @@ package de.variantsync.functjonal.category; public class SemigroupCannotAppend extends RuntimeException { - public SemigroupCannotAppend(String message) { + public SemigroupCannotAppend(final String message) { super(message); } } diff --git a/src/main/java/de/variantsync/functjonal/category/Traversable.java b/src/main/java/de/variantsync/functjonal/category/Traversable.java index c04fd31..59b9f37 100644 --- a/src/main/java/de/variantsync/functjonal/category/Traversable.java +++ b/src/main/java/de/variantsync/functjonal/category/Traversable.java @@ -9,6 +9,6 @@ public class Traversable { public static Result, F> sequence(final Optional> o) { return Functjonal.match(o, just -> just.map(Optional::of), - () -> Result.Success(Optional.empty())); + () -> Result.success(Optional.empty())); } } diff --git a/src/main/java/de/variantsync/functjonal/error/CompositeException.java b/src/main/java/de/variantsync/functjonal/error/CompositeException.java index 626575f..bb13d58 100644 --- a/src/main/java/de/variantsync/functjonal/error/CompositeException.java +++ b/src/main/java/de/variantsync/functjonal/error/CompositeException.java @@ -12,7 +12,7 @@ * Exception that can group exceptions as a list. */ public class CompositeException extends Exception { - public static final Monoid MONOID = Monoid.From( + public static final Monoid MONOID = Monoid.from( CompositeException::new, CompositeException::new ); diff --git a/src/main/java/de/variantsync/functjonal/functions/FragileFunction.java b/src/main/java/de/variantsync/functjonal/functions/FragileFunction.java index 603fdb9..2184887 100644 --- a/src/main/java/de/variantsync/functjonal/functions/FragileFunction.java +++ b/src/main/java/de/variantsync/functjonal/functions/FragileFunction.java @@ -3,12 +3,16 @@ @FunctionalInterface public interface FragileFunction { B run(A a) throws E; - - default FragileFunction compose(final FragileFunction before) { - return (Input input) -> this.run(before.run(input)); + + default FragileFunction compose( + final FragileFunction before + ) { + return (Input input) -> this.run(before.run(input)); } - - default FragileFunction andThen(final FragileFunction after) { + + default FragileFunction andThen( + final FragileFunction after + ) { return (A input) -> after.run(this.run(input)); } } diff --git a/src/main/java/de/variantsync/functjonal/iteration/ClusteredIterator.java b/src/main/java/de/variantsync/functjonal/iteration/ClusteredIterator.java index 0f6be99..6ec5b69 100644 --- a/src/main/java/de/variantsync/functjonal/iteration/ClusteredIterator.java +++ b/src/main/java/de/variantsync/functjonal/iteration/ClusteredIterator.java @@ -9,6 +9,7 @@ * Instead of iterating over elements individually, a ClusteredIterator collects the next n elements in a list * that will be returned, where n is a positive integer. * The last returned list might be shorter than n iff there were less than n elements remaining to visit. + * * @param */ public class ClusteredIterator implements Iterator> { @@ -18,7 +19,7 @@ public class ClusteredIterator implements Iterator> { /** * Clusters the elements returned by the inner iterator to chunks of the given size. */ - public ClusteredIterator(final Iterator inner, int clusterSize) { + public ClusteredIterator(final Iterator inner, final int clusterSize) { this.inner = inner; this.clusterSize = clusterSize; } diff --git a/src/main/java/de/variantsync/functjonal/iteration/Yield.java b/src/main/java/de/variantsync/functjonal/iteration/Yield.java index 84e04d5..3252ce8 100644 --- a/src/main/java/de/variantsync/functjonal/iteration/Yield.java +++ b/src/main/java/de/variantsync/functjonal/iteration/Yield.java @@ -74,4 +74,4 @@ public List toList() { } return l; } -} \ No newline at end of file +} diff --git a/src/main/java/de/variantsync/functjonal/list/ImmutableList.java b/src/main/java/de/variantsync/functjonal/list/ImmutableList.java index efc464e..af1630a 100644 --- a/src/main/java/de/variantsync/functjonal/list/ImmutableList.java +++ b/src/main/java/de/variantsync/functjonal/list/ImmutableList.java @@ -4,7 +4,7 @@ import java.util.List; public class ImmutableList extends ListDecorator { - private final static String ERROR_MESSAGE = "List is immutable."; + private static final String ERROR_MESSAGE = "List is immutable."; public ImmutableList(final List list) { super(list); diff --git a/src/main/java/de/variantsync/functjonal/list/ListDecorator.java b/src/main/java/de/variantsync/functjonal/list/ListDecorator.java index dfba586..287c700 100644 --- a/src/main/java/de/variantsync/functjonal/list/ListDecorator.java +++ b/src/main/java/de/variantsync/functjonal/list/ListDecorator.java @@ -51,7 +51,7 @@ public Object[] toArray() { } @Override - public T1[] toArray(final T1 [] a) { + public T1[] toArray(final T1[] a) { return wrappee.toArray(a); } diff --git a/src/main/java/de/variantsync/functjonal/list/NonEmptyList.java b/src/main/java/de/variantsync/functjonal/list/NonEmptyList.java index 1caeb29..50a919a 100644 --- a/src/main/java/de/variantsync/functjonal/list/NonEmptyList.java +++ b/src/main/java/de/variantsync/functjonal/list/NonEmptyList.java @@ -10,7 +10,7 @@ * @param Type of elements that are contained in this list. */ public class NonEmptyList extends ListDecorator { - private final static String ERROR_MESSAGE = "Operation disallowed as it could make this list become empty!"; + private static final String ERROR_MESSAGE = "Operation disallowed as it could make this list become empty!"; public NonEmptyList(final List list) { super(list); diff --git a/src/main/java/de/variantsync/functjonal/map/MapDecorator.java b/src/main/java/de/variantsync/functjonal/map/MapDecorator.java index 16e138c..5e707db 100644 --- a/src/main/java/de/variantsync/functjonal/map/MapDecorator.java +++ b/src/main/java/de/variantsync/functjonal/map/MapDecorator.java @@ -26,32 +26,32 @@ public boolean isEmpty() { } @Override - public boolean containsKey(Object key) { + public boolean containsKey(final Object key) { return inner.containsKey(key); } @Override - public boolean containsValue(Object value) { + public boolean containsValue(final Object value) { return inner.containsValue(value); } @Override - public V get(Object key) { + public V get(final Object key) { return inner.get(key); } @Override - public V put(K key, V value) { + public V put(final K key, final V value) { return inner.put(key, value); } @Override - public V remove(Object key) { + public V remove(final Object key) { return inner.remove(key); } @Override - public void putAll(Map m) { + public void putAll(final Map m) { inner.putAll(m); } diff --git a/src/main/java/de/variantsync/functjonal/map/MergeMap.java b/src/main/java/de/variantsync/functjonal/map/MergeMap.java index 14f5745..5155ff2 100644 --- a/src/main/java/de/variantsync/functjonal/map/MergeMap.java +++ b/src/main/java/de/variantsync/functjonal/map/MergeMap.java @@ -10,17 +10,19 @@ * A map whose values are merged instead of overwritten upon put with a duplicate key. * In particular, when invoking put(k, v) and k is already associated to a value w, then * v will be appended to w with a semigroup for v. + * * @param key type * @param value type */ public class MergeMap extends MapDecorator { - public final InplaceSemigroup> ISEMIGROUP = MergeMap::append; + public final InplaceSemigroup> isemigroup = MergeMap::append; private final Function> semigroupFactory; /** * Creates a merge map for the given map. - * @param inner The map on which to merge the values of duplicate keys. + * + * @param inner The map on which to merge the values of duplicate keys. * @param semigroupFactory A factory returning a semigroup for a value v that * is used to merge other values with v. * In particular, when a new value v' is put into the map for key k for which @@ -34,7 +36,8 @@ public MergeMap(final Map inner, final Function> semigroup /** * Creates a merge map for the given map. - * @param inner The map on which to merge the values of duplicate keys. + * + * @param inner The map on which to merge the values of duplicate keys. * @param semigroup A semigroup that is used to merge values. * In particular, when a new value v' is put into the map for key k for which * already an entry v is present, then v and v' will be merged via @@ -57,14 +60,16 @@ public V put(final K key, final V value) { } @Override - public void putAll(Map m) { - if (m.isEmpty()) return; + public void putAll(final Map m) { + if (m.isEmpty()) { + return; + } for (var entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } } - public void append(Map other) { + public void append(final Map other) { putAll(other); } @@ -72,20 +77,22 @@ public void append(Map other) { * Appends the given value to the value currently registered in the given map for the given key. * If the given map does not contain an entry for the given key, the given value is stored as * the keys value instead. - * @param key type - * @param value type - * @param map The map to which the value should be added. - * @param key The key whose value should be appended by the given value. If the key is not contained -* in the given map, a new entry is created with this key and the given value. + * + * @param key type + * @param value type + * @param map The map to which the value should be added. + * @param key The key whose value should be appended by the given value. If the key is not contained + * in the given map, a new entry is created with this key and the given value. * @param valueToAppend The value to append to map.get(key). If the map does not contain an entry for -* the given key, a new entry with the given key value pair is made (i.e., map.put(key, valueToAppend)). + * the given key, a new entry with the given key value pair is made + * (i.e., map.put(key, valueToAppend)). */ public static void putValue( final Map map, final K key, final V valueToAppend, - Semigroup semigroup) - { + final Semigroup semigroup + ) { V result = valueToAppend; if (map.containsKey(key)) { result = semigroup.append(map.get(key), valueToAppend); @@ -96,8 +103,8 @@ public static void putValue( public static void putAllValues( final Map map, final Map other, - Semigroup semigroup) - { + final Semigroup semigroup + ) { for (final Map.Entry otherEntry : other.entrySet()) { putValue(map, otherEntry.getKey(), otherEntry.getValue(), semigroup); }