Skip to content
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

Checkstyle pipeline #3

Open
wants to merge 3 commits into
base: checkstyle
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
20 changes: 20 additions & 0 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Project exclude paths
/target/
/target/
.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<PassedHours> MONOID = Monoid.From(
public static final Monoid<PassedHours> MONOID = Monoid.from(
() -> new PassedHours(0, 0),
(a, b) -> new PassedHours(
a.hours() + b.hours() + ((a.minutes() + b.minutes()) / 60),
Expand Down
155 changes: 155 additions & 0 deletions checks/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">
<property name="charset" value="UTF-8"/>

<!--
If you set the basedir property below, then all reported file
names will be relative to the specified directory. See
http://checkstyle.sourceforge.net/5.x/config.html#Checker
<property name="basedir" value="${basedir}"/>
-->
<property name="severity" value="error"/>

<property name="fileExtensions" value="java, properties, xml"/>


<module name="LineLength">
<property name="max" value="120"/>
</module>

<!-- Checks whether files end with a new line. -->
<!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/>
</module>

<!-- Checks that property files contain the same keys. -->
<!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
<module name="Translation"/>

<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="FileLength">
<property name="id" value="FileLengthRegular"/>
<property name="max" value="400"/>
</module>

<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="FileTabCharacter"/>

<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="RegexpSingleline">
<property name="format" value="\s+$"/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
<property name="message" value="Line has trailing spaces."/>
</module>

<module name="SuppressWarningsFilter"/>

<module name="SuppressWithPlainTextCommentFilter"/>

<module name="TreeWalker">

<!-- Suppression Filter -->
<module name="SuppressWarningsHolder"/>
<module name="SuppressionCommentFilter"/>

<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
<module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>

<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports">
<property name="processJavadoc" value="false"/>
</module>

<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="MethodLength">
<property name="id" value="MethodLengthRegular"/>
<property name="max" value="25"/>
<property name="countEmpty" value="false"/>
</module>
<module name="ParameterNumber">
<property name="max" value="11"/>
</module>

<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="EmptyForIteratorPad"/>
<module name="GenericWhitespace"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>

<!-- Modifier Checks -->
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>

<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>

<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
<!--module name="AvoidInlineConditionals"/-->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<!--<module name="HiddenField"/>-->
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<module name="MissingSwitchDefault"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>

<!-- Checks for class design -->
<!-- See http://checkstyle.sf.net/config_design.html -->
<!--<module name="DesignForExtension"/>-->
<module name="FinalClass"/>
<!--<module name="HideUtilityClassConstructor"/>-->
<module name="InterfaceIsType"/>
<!--<module name="VisibilityModifier"/>-->

<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="ArrayTypeStyle"/>
<module name="FinalParameters"/>
<module name="TodoComment"/>
<module name="UpperEll"/>

<!-- Checks for cyclomatic complexity -->
<module name="CyclomaticComplexity">
<property name="max" value="7"/>
</module>
</module>

</module>
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>${basedir}/checks/checkstyle.xml</configLocation>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>9.2.1</version>
</dependency>
</dependencies>

</plugin>
</plugins>
</build>
</project>
3 changes: 2 additions & 1 deletion src/main/java/de/variantsync/functjonal/Cast.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package de.variantsync.functjonal;

public abstract class Cast {
private Cast() {}
private Cast() {
}

@SuppressWarnings("unchecked")
public static <From, To> To unchecked(final From b) {
Expand Down
89 changes: 57 additions & 32 deletions src/main/java/de/variantsync/functjonal/Functjonal.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
public class Functjonal {
/// Containers
public static <K1, K2, V1, V2> Map<K2, V2> bimap(
Map<K1, V1> m,
Function<? super K1, ? extends K2> key,
Function<? super V1, ? extends V2> val) {
final Map<K1, V1> m,
final Function<? super K1, ? extends K2> key,
final Function<? super V1, ? extends V2> val) {
return bimap(m, key, val, HashMap::new);
}

public static <K1, K2, V1, V2, M extends Map<K2, V2>> M bimap(
Map<K1, V1> m,
Function<? super K1, ? extends K2> key,
Function<? super V1, ? extends V2> val,
Supplier<M> mapFactory) {
final Map<K1, V1> m,
final Function<? super K1, ? extends K2> key,
final Function<? super V1, ? extends V2> val,
final Supplier<M> mapFactory) {
final M result = mapFactory.get();
for (final Map.Entry<K1, V1> e : m.entrySet()) {
result.put(key.apply(e.getKey()), val.apply(e.getValue()));
Expand All @@ -47,24 +47,37 @@ public static <T, U> List<U> map(final List<? extends T> a, final Function<T, U>

/// Pattern matching

public static <A, B> B match(final Optional<A> ma, final Function<A, ? extends B> just, final Supplier<? extends B> nothing) {
public static <A, B> B match(
final Optional<A> ma,
final Function<A, ? extends B> just,
final Supplier<? extends B> nothing
) {
final Optional<B> x = ma.map(just);
return x.orElseGet(nothing);
}

/**
* Curried version of the above.
*/
public static <A, B> Function<Optional<A>, B> match(final Function<A, B> just, final Supplier<? extends B> nothing) {
public static <A, B> Function<Optional<A>, B> match(
final Function<A, B> just,
final Supplier<? extends B> nothing
) {
return ma -> match(ma, just, nothing);
}

public static <A, B, C> Function<Result<A, B>, C> match(final Function<A, C> success, final Function<B, C> failure) {
public static <A, B, C> Function<Result<A, B>, C> match(
final Function<A, C> success,
final Function<B, C> failure
) {
return ma -> ma.match(success, failure);
}


public static <T> Consumer<T> when(Predicate<T> condition, Consumer<? super T> task) {
public static <T> Consumer<T> when(
final Predicate<T> condition,
final Consumer<? super T> task
) {
return t -> {
if (condition.test(t)) {
task.accept(t);
Expand All @@ -74,12 +87,18 @@ public static <T> Consumer<T> when(Predicate<T> condition, Consumer<? super T> 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 <A, B> Function<A, B> when(final Predicate<A> condition, final Function<A, B> then, final Function<A, B> otherwise) {
public static <A, B> Function<A, B> when(
final Predicate<A> condition,
final Function<A, B> then,
final Function<A, B> otherwise
) {
return a -> condition.test(a) ? then.apply(a) : otherwise.apply(a);
}

Expand All @@ -99,51 +118,57 @@ public static <B> Function<Boolean, B> when(final Supplier<B> then, final Suppli

/// Java to FP

public static <A> Function<A, Unit> Lift(final Consumer<A> f) {
public static <A> Function<A, Unit> lift(final Consumer<A> f) {
return a -> {
f.accept(a);
return Unit.Instance();
return Unit.instance();
};
}

public static Supplier<Unit> Lift(final Procedure f) {
public static Supplier<Unit> lift(final Procedure f) {
return () -> {
f.run();
return Unit.Instance();
return Unit.instance();
};
}

public static <E extends Exception> FragileSupplier<Unit, E> LiftFragile(final FragileProcedure<E> f) {
public static <E extends Exception> FragileSupplier<Unit, E> liftFragile(final FragileProcedure<E> 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 <Nullable> The type of the nullable value a.
* @param <B> The result type.
* @param <E> The type of an exception that might be thrown by f.
* @param <Nullable> The type of the nullable value a.
* @param <B> The result type.
* @param <E> 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 <Nullable, B, E extends Exception> Optional<B> mapFragile(final Nullable n, final FragileFunction<Nullable, B, E> f, final Supplier<String> errorMessage) {
public static <Nullable, B, E extends Exception> Optional<B> mapFragile(
final Nullable n,
final FragileFunction<Nullable, B, E> f,
final Supplier<String> 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 <A, B, E extends Exception> Lazy<Optional<B>> mapFragileLazily(final A a, final FragileFunction<A, B, E> f, final Supplier<String> errorMessage) {
public static <A, B, E extends Exception> Lazy<Optional<B>> mapFragileLazily(
final A a, final FragileFunction<A, B, E> f,
final Supplier<String> errorMessage
) {
return Lazy.of(() -> mapFragile(a, f, errorMessage));
}
}
Loading