Skip to content

Commit

Permalink
Restoring Java 8 build compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
SuppieRK committed Nov 5, 2023
1 parent b9e21c3 commit ff86a2c
Show file tree
Hide file tree
Showing 52 changed files with 164 additions and 96 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.0.2

### Changed
- Restored Java 8 build compatibility
- Adjusted license year
- Improved examples in README
- Fixed minor Sonar issues in tests

## 1.0.1

### Added
Expand Down
107 changes: 85 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils?ref=badge_shield)

This dependency-less library serves for one simple purpose - allow developer to invoke methods throwing exceptions in Java functional expressions / Stream API
This dependency-less library serves for one simple purpose:
reduce boilerplate try-catch statements during work with Java Stream API.

## How to add

Expand All @@ -11,14 +12,14 @@ This dependency-less library serves for one simple purpose - allow developer to
<dependency>
<groupId>io.github.suppierk</groupId>
<artifactId>java-throwable-utils</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
```

- Gradle
```groovy
dependencies {
implementation 'io.github.suppierk:java-throwable-utils:1.0.1'
implementation 'io.github.suppierk:java-throwable-utils:1.0.2'
}
```

Expand All @@ -30,42 +31,102 @@ If you had to use constructs like:
import java.util.ArrayList;
import java.util.List;

public class Test {
public void throwingMethod(String source) throws Exception {
public class Demo {
public static String throwingMethod(String source) throws Exception {
throw new Exception(source);
}

public void processingMethod() {
public static void main(String[] args) {
List<String> test = new ArrayList<>();

test.forEach(s -> {
try {
throwingMethod(s);
} catch (Exception e) {
e.printStackTrace();
}
});
test.add("sample");

try {
test.stream()
.map(s -> {
try {
return throwingMethod(s);
} catch (Exception e) {
// Here we would have to:
// a) Return some value to filter out later and log exception
// b) Wrap and rethrow an exception to catch it later again
throw new RuntimeException(e);
}
})
.forEach(s -> {
try {
throwingMethod(s);
} catch (Exception e) {
// Here we would have to:
// a) Suppress and log exception
// b) Wrap and rethrow an exception to catch it later again
throw new RuntimeException(e);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
```

with this library, you can simplify this pipeline to:

```java
import io.github.suppierk.java.util.function.ThrowableConsumer;
import io.github.suppierk.java.util.function.*;

import java.util.ArrayList;
import java.util.List;

public class Test {
public void throwingMethod(String source) throws Exception {
public class Demo {
public static String throwingMethod(String source) throws Exception {
throw new Exception(source);
}

public void processingMethod() {
public static void main(String[] args) {
List<String> test = new ArrayList<>();
test.add("sample");

try {
test.stream()
.map((ThrowableFunction<String, String>) Demo::throwingMethod)
.forEach((ThrowableConsumer<String>) Demo::throwingMethod);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```

and you can take it further and define functions explicitly, removing the need to specify function types:

```java
import io.github.suppierk.java.util.function.*;

import java.util.ArrayList;
import java.util.List;

public class Demo {
public static ThrowableFunction<String, String> throwingMap() {
return source -> {
throw new Exception(source);
};
}

public static ThrowableConsumer<String> throwingConsumer() {
return source -> {
throw new Exception(source);
};
}

public static void main(String[] args) {
List<String> test = new ArrayList<>();
test.add("sample");

test.forEach((ThrowableConsumer<String>) this::throwingMethod);
try {
test.stream().map(Demo.throwingMap()).forEach(Demo.throwingConsumer());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
Expand All @@ -74,7 +135,9 @@ All exceptions will be propagated using neat trick similar to Apache Commons `Ex

## Try

This library has simple implementation of `Try` monad, which benefits greatly from presence of these functions and allows us to handle exceptions in functional style much like you deal with nullable values using `Optional`
This library has simple implementation of `Try`,
which benefits greatly from presence of these functions
and allows us to handle exceptions in functional style much like you deal with nullable values using `Optional`.

```java
import io.github.suppierk.java.util.Try;
Expand All @@ -99,7 +162,7 @@ public class Test {
}
```

Same as for `Optional`, `Try` in case of failure will preserve only first exception happened in a call chain and skip further operations.
Same as for `Optional`, `Try` in a case of failure will preserve only first exception happened in a call chain and skip further operations.

## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils?ref=badge_large)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils?ref=badge_large)
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ version = "$VERSION_NAME"
description = "$POM_DESCRIPTION"

// Source code properties
sourceCompatibility = '11'
targetCompatibility = '11'
compileJava.options.encoding = 'UTF-8'
java {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
compileJava.options.encoding = 'UTF-8'
}

// Enable Spotless code formatting rules
spotless {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ SONATYPE_AUTOMATIC_RELEASE=true

GROUP=io.github.suppierk
POM_ARTIFACT_ID=java-throwable-utils
VERSION_NAME=1.0.1
VERSION_NAME=1.0.2

POM_NAME=Java Throwable Utilities
POM_DESCRIPTION=Java 11 compatible extensions allowing to use checked exceptions in functional programming APIs + Try interface
POM_DESCRIPTION=Java 8 compatible extensions allowing to use checked exceptions in functional programming APIs + Try interface
POM_INCEPTION_YEAR=2023
POM_URL=https://github.com/SuppieRK/java-throwable-utils

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/suppierk/java/util/Try.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
* Copyright (c) 2023 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit ff86a2c

Please sign in to comment.