Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

Commit 80b360e

Browse files
authored
Merge pull request #47 from alexengrig/fix/36
Fixes #36
2 parents 7275e14 + 492882f commit 80b360e

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,53 @@ This library contains utility classes with useful lambdas.
2828
* Writing code in [declarative](https://en.wikipedia.org/wiki/Declarative_programming) style.
2929
* Writing code in chaining style.
3030

31+
## Examples
32+
33+
Get the value from `Map` and print it on the console:
34+
35+
```java
36+
Map<Integer, String> map = new HashMap<>();
37+
map.put(1, "one");
38+
Holder<Map<Integer, String>> holder = new Holder<>(map);
39+
// Plain Java
40+
Map<Integer, String> map = holder.get();
41+
String actual = map.get(key);
42+
System.out.println(actual);
43+
// Chaining Style
44+
Optional.of(holder)
45+
.map(Holder::get)
46+
.map(m -> m.get(key))
47+
.ifPresent(System.out::println);
48+
// LambdaX
49+
String actual = Optional.of(holder)
50+
.map(Holder::get)
51+
.map(MapX.get(key))
52+
.ifPresent(System.out::println);
53+
```
54+
55+
Insert the value into `Collection` if it does not contain it:
56+
57+
```java
58+
Collection<Integer> numbers = new ArrayList<>();
59+
Holder<Collection<Integer>> holder = new Holder<>(numbers);
60+
int value = 1;
61+
// Plain Java
62+
Collection<Integer> target = holder.get();
63+
if (!target.contains(value)) {
64+
target.add(value);
65+
}
66+
// Chaining Style
67+
Optional.of(holder)
68+
.map(Holder::get)
69+
.filter(collection -> !collection.contains(value))
70+
.ifPresent(collection -> collection.add(value));
71+
// LambdaX
72+
Optional.of(holder)
73+
.map(Holder::get)
74+
.filter(CollectionX.contains(value).negate())
75+
.ifPresent(CollectionX.onlyAdd(value));
76+
```
77+
3178
## Installation
3279

3380
Releases are available in [Maven Central](https://repo1.maven.org/maven2/io/github/alexengrig/lambdax/).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2019 LambdaX contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.alexengrig.lambdax.example;
18+
19+
import io.github.alexengrig.lambdax.collection.CollectionX;
20+
import io.github.alexengrig.lambdax.collection.MapX;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import java.util.*;
25+
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertTrue;
28+
29+
/**
30+
* Abbreviations:
31+
* <pre>
32+
* PS - Plain Style
33+
* CS - Chaining Style
34+
* LX - LambdaX
35+
* </pre>
36+
*/
37+
public class CollectionExampleTest {
38+
private Holder<Map<Integer, String>> mapHolder;
39+
private Holder<Collection<Integer>> collectionHolder;
40+
41+
@Before
42+
public void before() {
43+
HashMap<Integer, String> numbers = new HashMap<>();
44+
numbers.put(1, "one");
45+
numbers.put(2, "two");
46+
numbers.put(3, "three");
47+
mapHolder = new Holder<>(numbers);
48+
collectionHolder = new Holder<>(new ArrayList<>());
49+
}
50+
51+
@Test
52+
public void checkMapGettingInPS() {
53+
int value = 1;
54+
Map<Integer, String> map = mapHolder.get();
55+
String actual = map.get(value);
56+
assertEquals("one", actual);
57+
}
58+
59+
@Test
60+
public void checkMapGettingInCS() {
61+
int value = 1;
62+
String actual = Optional.of(mapHolder)
63+
.map(Holder::get)
64+
.map(m -> m.get(value))
65+
.orElseThrow(IllegalStateException::new);
66+
assertEquals("one", actual);
67+
}
68+
69+
@Test
70+
public void checkMapGettingInLX() {
71+
int value = 1;
72+
String actual = Optional.of(mapHolder)
73+
.map(Holder::get)
74+
.map(MapX.get(value))
75+
.orElseThrow(IllegalStateException::new);
76+
assertEquals("one", actual);
77+
}
78+
79+
@Test
80+
public void checkInsertItemInPS() {
81+
int value = 2;
82+
Collection<Integer> collection = collectionHolder.get();
83+
if (!collection.contains(value)) {
84+
collection.add(value);
85+
}
86+
assertTrue(collectionHolder.get().contains(value));
87+
}
88+
89+
@Test
90+
public void checkInsertItemInCS() {
91+
int value = 2;
92+
Optional.of(collectionHolder)
93+
.map(Holder::get)
94+
.filter(collection -> !collection.contains(value))
95+
.ifPresent(collection -> collection.add(value));
96+
assertTrue(collectionHolder.get().contains(value));
97+
}
98+
99+
@Test
100+
public void checkInsertItemInLX() {
101+
int value = 2;
102+
Optional.of(collectionHolder)
103+
.map(Holder::get)
104+
.filter(CollectionX.contains(value).negate())
105+
.ifPresent(CollectionX.onlyAdd(value));
106+
assertTrue(collectionHolder.get().contains(value));
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019 LambdaX contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.alexengrig.lambdax.example;
18+
19+
final class Holder<T> {
20+
private T t;
21+
22+
Holder(T t) {
23+
this.t = t;
24+
}
25+
26+
T get() {
27+
return t;
28+
}
29+
30+
void set(T t) {
31+
this.t = t;
32+
}
33+
}

0 commit comments

Comments
 (0)