Skip to content

Commit 471c712

Browse files
committed
Massive cleanup!
1 parent 6b02113 commit 471c712

39 files changed

Lines changed: 2320 additions & 960 deletions
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.intisy.utils.collections;
2+
3+
import java.util.Collection;
4+
import java.util.HashMap;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* A specialized HashMap that stores a key mapped to two values using the Doublet class.
9+
* This class extends HashMap and provides additional methods to work with the two values
10+
* associated with each key.
11+
*
12+
* @param <K> the type of keys maintained by this map
13+
* @param <V1> the type of the first value
14+
* @param <V2> the type of the second value
15+
* @author Finn Birich
16+
*/
17+
18+
@SuppressWarnings("unused")
19+
public class DoubleHashMap<K, V1, V2> extends HashMap<K, Doublet<V1, V2>> {
20+
/**
21+
* Associates the specified key with the specified values in this map.
22+
* If the map previously contained a mapping for the key, the old value is replaced.
23+
*
24+
* @param key the key with which the specified values are to be associated
25+
* @param value1 the first value to be associated with the specified key
26+
* @param value2 the second value to be associated with the specified key
27+
*/
28+
public void put(K key, V1 value1, V2 value2) {
29+
super.put(key, new Doublet<>(value1, value2));
30+
}
31+
32+
/**
33+
* Returns the first value to which the specified key is mapped,
34+
* or null if this map contains no mapping for the key.
35+
*
36+
* @param key the key whose associated first value is to be returned
37+
* @return the first value to which the specified key is mapped, or
38+
* null if this map contains no mapping for the key
39+
* @throws NullPointerException if the mapping for the key is null
40+
*/
41+
public V1 getValue1(K key) {
42+
return get(key).getKey();
43+
}
44+
45+
/**
46+
* Returns the second value to which the specified key is mapped,
47+
* or null if this map contains no mapping for the key.
48+
*
49+
* @param key the key whose associated second value is to be returned
50+
* @return the second value to which the specified key is mapped, or
51+
* null if this map contains no mapping for the key
52+
* @throws NullPointerException if the mapping for the key is null
53+
*/
54+
public V2 getValue2(K key) {
55+
return get(key).getValue();
56+
}
57+
58+
/**
59+
* Returns a collection view of the first values contained in this map.
60+
* The collection is backed by the map, so changes to the map are reflected in the collection.
61+
*
62+
* @return a collection view of the first values contained in this map
63+
*/
64+
public Collection<V1> values1() {
65+
return values().stream().map(Doublet::getKey).collect(Collectors.toList());
66+
}
67+
68+
/**
69+
* Returns a collection view of the second values contained in this map.
70+
* The collection is backed by the map, so changes to the map are reflected in the collection.
71+
*
72+
* @return a collection view of the second values contained in this map
73+
*/
74+
public Collection<V2> values2() {
75+
return values().stream().map(Doublet::getValue).collect(Collectors.toList());
76+
}
77+
}
78+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.intisy.utils.collections;
2+
3+
/**
4+
* A generic class representing a key-value pair.
5+
* This class is used to store two related objects together, typically as a key and its associated value.
6+
* It provides methods to access and modify both the key and value.
7+
*
8+
* @param <K> the type of the key
9+
* @param <V> the type of the value
10+
* @author Finn Birich
11+
*/
12+
@SuppressWarnings("unused")
13+
public class Doublet<K, V> {
14+
private K key;
15+
private V value;
16+
17+
/**
18+
* Constructs a new Doublet with the specified key and value.
19+
*
20+
* @param key the key of this doublet
21+
* @param value the value of this doublet
22+
*/
23+
public Doublet(K key, V value) {
24+
this.key = key;
25+
this.value = value;
26+
}
27+
28+
/**
29+
* Returns the key of this doublet.
30+
*
31+
* @return the key
32+
*/
33+
public K getKey() {
34+
return key;
35+
}
36+
37+
/**
38+
* Sets the key of this doublet.
39+
*
40+
* @param key the new key to set
41+
*/
42+
public void setKey(K key) {
43+
this.key = key;
44+
}
45+
46+
/**
47+
* Returns the value of this doublet.
48+
*
49+
* @return the value
50+
*/
51+
public V getValue() {
52+
return value;
53+
}
54+
55+
/**
56+
* Sets the value of this doublet.
57+
*
58+
* @param value1 the new value to set
59+
*/
60+
public void setValue1(V value1) {
61+
this.value = value1;
62+
}
63+
64+
/**
65+
* Returns a string representation of this doublet.
66+
* The string representation consists of the key and value separated by commas
67+
* and enclosed in brackets.
68+
*
69+
* @return a string representation of this doublet
70+
*/
71+
@Override
72+
public String toString() {
73+
return "Triplet{" +
74+
"key=" + key +
75+
", value=" + value +
76+
'}';
77+
}
78+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.github.intisy.utils.collections;
2+
3+
/**
4+
* A generic class representing a triplet of objects: a key and two associated values.
5+
* This class is used to store three related objects together, providing methods to
6+
* access and modify each component.
7+
*
8+
* @param <K> the type of the key
9+
* @param <V1> the type of the first value
10+
* @param <V2> the type of the second value
11+
* @author Finn Birich
12+
*/
13+
@SuppressWarnings("unused")
14+
public class Triplet<K, V1, V2> {
15+
private K key;
16+
private V1 value1;
17+
private V2 value2;
18+
19+
/**
20+
* Constructs a new Triplet with the specified key and two values.
21+
*
22+
* @param key the key of this triplet
23+
* @param value1 the first value of this triplet
24+
* @param value2 the second value of this triplet
25+
*/
26+
public Triplet(K key, V1 value1, V2 value2) {
27+
this.key = key;
28+
this.value1 = value1;
29+
this.value2 = value2;
30+
}
31+
32+
/**
33+
* Returns the key of this triplet.
34+
*
35+
* @return the key
36+
*/
37+
public K getKey() {
38+
return key;
39+
}
40+
41+
/**
42+
* Sets the key of this triplet.
43+
*
44+
* @param key the new key to set
45+
*/
46+
public void setKey(K key) {
47+
this.key = key;
48+
}
49+
50+
/**
51+
* Returns the first value of this triplet.
52+
*
53+
* @return the first value
54+
*/
55+
public V1 getValue1() {
56+
return value1;
57+
}
58+
59+
/**
60+
* Sets the first value of this triplet.
61+
*
62+
* @param value1 the new first value to set
63+
*/
64+
public void setValue1(V1 value1) {
65+
this.value1 = value1;
66+
}
67+
68+
/**
69+
* Returns the second value of this triplet.
70+
*
71+
* @return the second value
72+
*/
73+
public V2 getValue2() {
74+
return value2;
75+
}
76+
77+
/**
78+
* Sets the second value of this triplet.
79+
*
80+
* @param value2 the new second value to set
81+
*/
82+
public void setValue2(V2 value2) {
83+
this.value2 = value2;
84+
}
85+
86+
/**
87+
* Returns a string representation of this triplet.
88+
* The string representation consists of the key and both values separated by commas
89+
* and enclosed in brackets.
90+
*
91+
* @return a string representation of this triplet
92+
*/
93+
@Override
94+
public String toString() {
95+
return "Triplet{" +
96+
"key=" + key +
97+
", value1=" + value1 +
98+
", value2=" + value2 +
99+
'}';
100+
}
101+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.intisy.utils.concurrency;
2+
3+
/**
4+
* Utility class providing functionality for retrying operations that might fail.
5+
* This class allows operations to be retried indefinitely until they succeed,
6+
* with optional wait periods between retry attempts.
7+
*
8+
* @author Finn Birich
9+
*/
10+
@SuppressWarnings("unused")
11+
public class RetryUtils {
12+
/**
13+
* Executes the provided operation and retries indefinitely until it succeeds.
14+
* If the operation fails, it will be retried immediately without any delay.
15+
*
16+
* @param retryable the operation to execute and potentially retry
17+
*/
18+
public static void run(Retryable retryable) {
19+
run(0, retryable);
20+
}
21+
22+
/**
23+
* Executes the provided operation and retries indefinitely until it succeeds.
24+
* If the operation fails, it will be retried after waiting for the specified
25+
* number of milliseconds. If wait is 0 or negative, retries will happen immediately.
26+
*
27+
* @param wait the number of milliseconds to wait between retry attempts
28+
* @param retryable the operation to execute and potentially retry
29+
*/
30+
public static void run(int wait, Retryable retryable) {
31+
while (true) {
32+
try {
33+
retryable.run();
34+
break;
35+
} catch (Exception e) {
36+
if (wait > 0)
37+
ThreadUtils.sleep(wait);
38+
}
39+
}
40+
}
41+
42+
/**
43+
* Interface for operations that can be retried.
44+
* Implementations of this interface represent operations that might fail
45+
* and need to be retried until they succeed.
46+
*/
47+
public interface Retryable {
48+
/**
49+
* Executes the retryable operation.
50+
*
51+
* @throws Exception if the operation fails and should be retried
52+
*/
53+
void run() throws Exception;
54+
}
55+
}

0 commit comments

Comments
 (0)