Skip to content

Commit ea7d6d4

Browse files
authored
Merge pull request #10 from CrissNamon/dev
1.2
2 parents 7a25248 + fcd2a53 commit ea7d6d4

File tree

7 files changed

+97
-19
lines changed

7 files changed

+97
-19
lines changed

LEARN.md

Whitespace-only changes.

README.md

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,84 @@
11
## About
22

33
[![Build](https://github.com/CrissNamon/aide/actions/workflows/maven.yml/badge.svg)](https://github.com/CrissNamon/aide/actions/workflows/maven.yml)
4+
[![Releases](https://img.shields.io/github/v/release/crissnamon/aide?include_prereleases)](https://github.com/CrissNamon/aide/releases)
5+
[![Maven](https://maven-badges.herokuapp.com/maven-central/tech.hiddenproject/aide/badge.svg)](https://central.sonatype.com/artifact/tech.hiddenproject/aide/1.2)
46

5-
Aide is a set of useful utils to simplify your code
7+
Aide is a set of useful utils for fast reflection, extended optionals and conditionals. It can help you with development of some service or your own framework.
68

79
## Content
810

9-
* Reflection
10-
- contains utils for reflection such as fast method invocation, annotation processing and other useful methods
11-
* Optional
12-
- contains extended optional classes for String, Boolean types, IfTrue and When conditionals, Object utils
11+
#### Reflection
12+
13+
Aide reflection contains utils for reflection such as fast method invocation with lambda wrapping, annotation processing and other useful methods.
14+
15+
Reflective method calls with Aide are simple:
16+
17+
```java
18+
// Get LambdaWrapperHolder isntance with default LambdaWrapper interface loaded
19+
LambdaWrapperHolder lambdaWrapperHolder = LambdaWrapperHolder.DEFAULT;
20+
// Find static method
21+
Method staticMethod = ReflectionUtil.getMethod(TestClass.class, "staticMethod", String.class);
22+
// Wrap static method
23+
// LambdaWrapper - default wrapper interface from Aide
24+
// Test class - caller class
25+
// Integer - return type
26+
MethodHolder<LambdaWrapper, TestClass, Integer> staticHolder = lambdaWrapperHolder.wrapSafe(staticMethod);
27+
// Invoke static method without caller
28+
int staticResult = staticHolder.invokeStatic("Hello");
29+
```
30+
31+
#### Optional
32+
33+
Aide optional contains extended optional classes for String, Boolean types, IfTrue and When conditionals, Object utils.
34+
35+
Extended optionals provides new methods for some types:
36+
37+
```java
38+
BooleanOptional.of(Modifier.isPublic(executable.getModifiers()))
39+
.ifFalseThrow(() -> ReflectionException.format("Wrapping is supported for PUBLIC methods only!"));
40+
```
41+
42+
With conditionals you can make your code more functional. Thats how Aide reflection uses them:
43+
44+
```java
45+
AbstractSignature signature = IfTrueConditional.create()
46+
.ifTrue(exact).then(() -> ExactMethodSignature.from(method))
47+
.ifTrue(someObj, Objects::isNull).then(() -> new MethodSignature())
48+
.orElseGet(() -> MethodSignature.from(method));
49+
```
50+
51+
Or WhenConditional:
52+
53+
```java
54+
WhenConditional.create()
55+
.when(someObj, Objects::nonNull).then(MyClass::nonNull)
56+
.when(someObj, Objects::isNull).then(MyClass::isNull)
57+
.orDoNothing();
58+
```
1359

1460
## Use
1561

1662
Artifact ids:
1763

18-
- all -> aide-all
19-
- reflection -> aide-reflection
20-
- optional -> aide-optional
64+
- `tech.hiddenproject:aide-all` - all components
65+
- `tech.hiddenproject:aide-optional` - optionals and conditionals
66+
- `tech.hiddenproject:aide-reflection` - reflection utils
2167

2268
### Maven
2369

2470
```xml
2571
<dependency>
2672
<groupId>tech.hiddenproject</groupId>
2773
<artifactId>aide-all</artifactId>
28-
<version>1.1</version>
74+
<version>1.2</version>
2975
</dependency>
3076
```
3177

3278
### Gradle
3379

3480
```groovy
35-
implementation 'tech:hiddenproject:aide-all:1.1'
81+
implementation 'tech:hiddenproject:aide-all:1.2'
3682
```
3783

3884
## Resources
@@ -48,7 +94,7 @@ ___
4894

4995
___
5096

51-
Aide has no dependencies and use only Java 8.</p>
97+
Aide has no dependencies and use only Java 8.
5298

5399
## Repository info
54100

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tech.hiddenproject.aide.optional;
2+
3+
/**
4+
* @author Danila Rassokhin
5+
*/
6+
public interface SneakyAction {
7+
8+
void make() throws Throwable;
9+
10+
}

aide-optional/src/main/java/tech/hiddenproject/aide/optional/ThrowableOptional.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
public class ThrowableOptional {
1111

1212
/**
13-
* Executes {@link Action} and catches any Throwable, then converts Throwable to RuntimeException
13+
* Executes {@link SneakyAction} and catches any Throwable, then converts Throwable to RuntimeException
1414
* and throws it. Can be used to work with checked exceptions in lambdas.
1515
*
16-
* @param action {@link Action}
16+
* @param action {@link SneakyAction}
1717
*/
18-
public static void sneaky(Action action) {
18+
public static void sneaky(SneakyAction action) {
1919
try {
2020
action.make();
2121
} catch (Throwable throwable) {
@@ -24,14 +24,14 @@ public static void sneaky(Action action) {
2424
}
2525

2626
/**
27-
* Executes {@link Action}. Catches any Throwable if it will be thrown, then converts Throwable to
27+
* Executes {@link SneakyAction}. Catches any Throwable if it will be thrown, then converts Throwable to
2828
* RuntimeException with given mapper function and throws it. Can be used to work with checked
2929
* exceptions in lambdas.
3030
*
31-
* @param action {@link Action}
31+
* @param action {@link SneakyAction}
3232
* @param mapper Mapper function to convert {@link Throwable} to {@link RuntimeException}
3333
*/
34-
public static void sneaky(Action action, Function<Throwable, ? extends RuntimeException> mapper) {
34+
public static void sneaky(SneakyAction action, Function<Throwable, ? extends RuntimeException> mapper) {
3535
try {
3636
action.make();
3737
} catch (Throwable throwable) {

aide-optional/src/test/java/tech/hiddenproject/aide/optional/StringOptionalTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public void mapTest() {
2828

2929
String expected = "MyString";
3030
String actual = stringOptional.mapOnCondition(s -> true, v -> "My" + v).get();
31-
31+
ThrowableOptional.sneaky(() -> exceptional());
3232
Assertions.assertEquals(expected, actual);
3333
}
34+
35+
public void exceptional() throws Throwable {
36+
37+
}
3438
}

aide-optional/src/test/java/tech/hiddenproject/aide/optional/WhenConditionalTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.mockito.ArgumentMatchers.any;
44

5+
import java.util.Objects;
56
import java.util.function.Predicate;
67
import java.util.function.Supplier;
78
import org.junit.jupiter.api.Assertions;
@@ -126,4 +127,21 @@ public void orElseThrowTest() {
126127

127128
Mockito.verifyNoMoreInteractions(predicate1, predicate2, action1, action2, supplier);
128129
}
130+
131+
@Test
132+
public void whenTest() {
133+
134+
WhenConditional.create()
135+
.when(false).then(WhenConditionalTest::action)
136+
.when(new Object(), Objects::nonNull).then(() -> WhenConditionalTest.action())
137+
.orDoNothing();
138+
139+
}
140+
141+
public static int id = 1;
142+
143+
public static void action() {
144+
System.out.println("ACTION: " + id);
145+
id++;
146+
}
129147
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,6 @@
191191
<maven.compiler.target>8</maven.compiler.target>
192192
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
193193
</properties>
194-
<version>1.1</version>
194+
<version>1.2</version>
195195

196196
</project>

0 commit comments

Comments
 (0)