Skip to content

Commit 2825002

Browse files
committed
Read me
1 parent fbb2d03 commit 2825002

1 file changed

Lines changed: 167 additions & 0 deletions

File tree

readme.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# ZoftWhere Mutable Library
2+
![Language](https://img.shields.io/github/languages/top/ZoftWhere/mutable-library) [![License](https://img.shields.io/github/license/ZoftWhere/mutable-library)](https://github.com/ZoftWhere/mutable-library/blob/master/license.txt)
3+
![GitHub last commit (branch)](https://img.shields.io/github/last-commit/ZoftWhere/mutable-library/master?label=master%20updated)
4+
5+
A library for mutable values.
6+
7+
8+
## Overview
9+
10+
The ZoftWhere Mutable Library provides mutable classes for scopes where final are preferred, or required for lambdas and closures.
11+
12+
13+
## Compiling and Installing the Library
14+
15+
The source code can be compiled with Java language version 8. It has been tested with Oracle JDK8, JDK11 and JDK12.
16+
17+
The project is Maven based, so executing the ```mvn install``` should install the library to the local repository. It has been tested with Apache Maven v3.6.1
18+
19+
20+
## Examples
21+
22+
### Mutable Value
23+
24+
When you need a final field in your lambda, but you need the value to change later on, using a MutableValue instance
25+
may be the solution.
26+
27+
``` kotlin
28+
29+
/**
30+
*
31+
*/
32+
public class MutableValueExample {
33+
34+
private static final MutableValue<String> publicKey = new MutableValue<>(null);
35+
36+
public static void main(String[] args) {
37+
Supplier<Map<String, String>> configSupplier = () -> {
38+
var map = new LinkedHashMap<String, String>();
39+
map.put("security.key.public", publicKey.get());
40+
return map;
41+
};
42+
43+
new MutableValueExample(configSupplier).run();
44+
}
45+
46+
private final Map<String, String> configMap;
47+
48+
private MutableValueExample(Supplier<Map<String, String>> supplier) {
49+
// Load from file and/or database;
50+
publicKey.set("#public");
51+
configMap = supplier.get();
52+
}
53+
54+
private void run() {
55+
var path = publicKey.get();
56+
if (path == null) {
57+
throw new NullPointerException();
58+
}
59+
if (path.equals(configMap.get("base.path"))) {
60+
throw new IllegalStateException();
61+
}
62+
}
63+
}
64+
65+
```
66+
67+
### Transformer with Zero Argument Consumer
68+
69+
A simple counter example.
70+
71+
``` kotlin
72+
public class TransformerExample0 {
73+
74+
public static void main(String[] args) {
75+
76+
// Simple counter.
77+
var counter = new MutableTransformer0<>(TransformerExample::newLongCounter);
78+
counter.set(0L);
79+
counter.accept();
80+
var count = counter.get();
81+
}
82+
83+
private static Consumer0 newLongCounter(MutableTransformer0<Long> internal) {
84+
return () -> internal.set(internal.get() + 1L);
85+
}
86+
87+
}
88+
```
89+
90+
### Transformer with One Argument Consumer
91+
92+
A simple single input hash function example.
93+
94+
``` kotlin
95+
public class TransformerExample1 {
96+
97+
public static void main(String[] args) {
98+
99+
// Simple single input hash function.
100+
var chainHash = new MutableTransformer1<>(TransformerExample::newHashFunction);
101+
chainHash.set(System.currentTimeMillis());
102+
chainHash.accept(new Random().nextLong());
103+
var hash = chainHash.get();
104+
}
105+
106+
private static Consumer1<Long> newHashFunction(MutableTransformer1<Long, Long> internal) {
107+
return (t1) -> {
108+
var current = internal.get();
109+
if (current == null) {
110+
internal.set(t1);
111+
}
112+
else if (t1 == null) {
113+
internal.set(internal.get() ^ 0xffffffffL + 1L);
114+
}
115+
else {
116+
internal.set(internal.get() ^ t1 + 1L);
117+
}
118+
};
119+
}
120+
121+
}
122+
```
123+
124+
125+
### Transformer with Two Argument Consumer
126+
127+
A simple 2-dimension route distance total example.
128+
129+
``` kotlin
130+
public class TransformerExample2 {
131+
132+
public static void main(String[] args) {
133+
134+
// Simple 2-dimension route distance total.
135+
var routeDistance = new MutableTransformer2<>(TransformerExample::newRouteFunction);
136+
routeDistance.accept(1.0, 1.0);
137+
routeDistance.accept(4.0, 6.0);
138+
var distance = routeDistance.get();
139+
}
140+
141+
private static Consumer2<Double, Double> newRouteFunction(MutableTransformer2<Double, Double, Double> internal) {
142+
final MutableValue<Double> lastX = new MutableValue<>(0.0d);
143+
final MutableValue<Double> lastY = new MutableValue<>(0.0d);
144+
return (x, y) -> {
145+
if (internal.isEmpty()) {
146+
lastX.set(x);
147+
lastY.set(y);
148+
internal.set(0.0d);
149+
}
150+
else {
151+
var current = internal.get();
152+
var dx = lastX.get() - x;
153+
var dy = lastY.get() - y;
154+
internal.set(current + Math.sqrt(dx * dx + dy * dy));
155+
}
156+
};
157+
}
158+
159+
}
160+
```
161+
162+
163+
## License
164+
165+
Copyright (C) 2019 ZoftWhere
166+
167+
Licensed under the MIT License

0 commit comments

Comments
 (0)