Skip to content

Commit 6a84e68

Browse files
committed
test clang format
1 parent 470760a commit 6a84e68

File tree

2 files changed

+188
-184
lines changed

2 files changed

+188
-184
lines changed

src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java

Lines changed: 98 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,120 +5,123 @@
55
import java.util.Map;
66

77
/**
8-
* Last-Write-Wins Element Set (LWWElementSet) is a state-based CRDT (Conflict-free Replicated Data Type)
9-
* designed for managing sets in a distributed and concurrent environment. It supports the addition and removal
10-
* of elements, using timestamps to determine the order of operations. The set is split into two subsets:
11-
* the add set for elements to be added and the remove set for elements to be removed. The LWWElementSet ensures
12-
* that the most recent operation (based on the timestamp) wins in the case of concurrent operations.
8+
* Last-Write-Wins Element Set (LWWElementSet) is a state-based CRDT (Conflict-free Replicated Data
9+
* Type) designed for managing sets in a distributed and concurrent environment. It supports the
10+
* addition and removal of elements, using timestamps to determine the order of operations. The set
11+
* is split into two subsets: the add set for elements to be added and the remove set for elements
12+
* to be removed. The LWWElementSet ensures that the most recent operation (based on the timestamp)
13+
* wins in the case of concurrent operations.
1314
*
1415
* @param <T> The type of the elements in the LWWElementSet.
15-
* @author <a href="https://github.com/itakurah">itakurah (GitHub)</a>,
16-
* <a href="https://www.linkedin.com/in/niklashoefflin/"> Niklas Hoefflin (LinkedIn)</a>
17-
* @see <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type">Conflict free replicated data type (Wikipedia)</a>
16+
* @author <a href="https://github.com/itakurah">itakurah (GitHub)</a>, <a
17+
* href="https://www.linkedin.com/in/niklashoefflin/">Niklas Hoefflin (LinkedIn)</a>
18+
* @see <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type">Conflict free
19+
* replicated data type (Wikipedia)</a>
1820
* @see <a href="https://inria.hal.science/inria-00555588v1/document">A comprehensive study of
19-
* Convergent and Commutative Replicated Data Types</a>
21+
* Convergent and Commutative Replicated Data Types</a>
2022
*/
21-
2223
class LWWElementSet<T> {
23-
final Map<T, Element<T>> addSet;
24-
final Map<T, Element<T>> removeSet;
24+
final Map<T, Element<T>> addSet;
25+
final Map<T, Element<T>> removeSet;
2526

26-
/**
27-
* Constructs an empty LWWElementSet.
28-
* This constructor initializes the addSet and removeSet as empty HashMaps.
29-
* The addSet stores elements that are added, and the removeSet stores elements that are removed.
30-
*/
31-
LWWElementSet() {
32-
this.addSet = new HashMap<>();
33-
this.removeSet = new HashMap<>();
34-
}
27+
/**
28+
* Constructs an empty LWWElementSet. This constructor initializes the addSet and removeSet as
29+
* empty HashMaps. The addSet stores elements that are added, and the removeSet stores elements
30+
* that are removed.
31+
*/
32+
LWWElementSet() {
33+
this.addSet = new HashMap<>();
34+
this.removeSet = new HashMap<>();
35+
}
3536

36-
/**
37-
* Adds an element to the addSet with the current timestamp.
38-
* This method stores the element in the addSet, ensuring that the element is added to the set
39-
* with an associated timestamp that represents the time of the addition.
40-
*
41-
* @param key The key of the element to be added.
42-
*/
43-
public void add(T key) {
44-
addSet.put(key, new Element<>(key, Instant.now()));
45-
}
37+
/**
38+
* Adds an element to the addSet with the current timestamp. This method stores the element in the
39+
* addSet, ensuring that the element is added to the set with an associated timestamp that
40+
* represents the time of the addition.
41+
*
42+
* @param key The key of the element to be added.
43+
*/
44+
public void add(T key) {
45+
addSet.put(key, new Element<>(key, Instant.now()));
46+
}
4647

47-
/**
48-
* Removes an element by adding it to the removeSet with the current timestamp.
49-
* This method adds the element to the removeSet, marking it as removed with the current timestamp.
50-
*
51-
* @param key The key of the element to be removed.
52-
*/
53-
public void remove(T key) {
54-
removeSet.put(key, new Element<>(key, Instant.now()));
55-
}
48+
/**
49+
* Removes an element by adding it to the removeSet with the current timestamp. This method adds
50+
* the element to the removeSet, marking it as removed with the current timestamp.
51+
*
52+
* @param key The key of the element to be removed.
53+
*/
54+
public void remove(T key) {
55+
removeSet.put(key, new Element<>(key, Instant.now()));
56+
}
5657

57-
/**
58-
* Checks if an element is in the LWWElementSet.
59-
* An element is considered present if it exists in the addSet and either does not exist in the removeSet,
60-
* or its add timestamp is later than any corresponding remove timestamp.
61-
*
62-
* @param key The key of the element to be checked.
63-
* @return {@code true} if the element is present in the set (i.e., its add timestamp is later than its remove timestamp, or it is not in the remove set),
64-
* {@code false} otherwise (i.e., the element has been removed or its remove timestamp is later than its add timestamp).
65-
*/
66-
public boolean lookup(T key) {
67-
Element<T> inAddSet = addSet.get(key);
68-
Element<T> inRemoveSet = removeSet.get(key);
58+
/**
59+
* Checks if an element is in the LWWElementSet. An element is considered present if it exists in
60+
* the addSet and either does not exist in the removeSet, or its add timestamp is later than any
61+
* corresponding remove timestamp.
62+
*
63+
* @param key The key of the element to be checked.
64+
* @return {@code true} if the element is present in the set (i.e., its add timestamp is later
65+
* than its remove timestamp, or it is not in the remove set), {@code false} otherwise (i.e.,
66+
* the element has been removed or its remove timestamp is later than its add timestamp).
67+
*/
68+
public boolean lookup(T key) {
69+
Element<T> inAddSet = addSet.get(key);
70+
Element<T> inRemoveSet = removeSet.get(key);
6971

70-
return inAddSet != null && (inRemoveSet == null || inAddSet.timestamp.isAfter(inRemoveSet.timestamp));
71-
}
72+
return inAddSet != null
73+
&& (inRemoveSet == null || inAddSet.timestamp.isAfter(inRemoveSet.timestamp));
74+
}
7275

73-
/**
74-
* Merges another LWWElementSet into this set.
75-
* This method takes the union of both the add-sets and remove-sets from the two sets, resolving conflicts by
76-
* keeping the element with the latest timestamp. If an element appears in both the add-set and remove-set of both sets,
77-
* the one with the later timestamp will be retained.
78-
*
79-
* @param other The LWWElementSet to merge with the current set.
80-
*/
81-
public void merge(LWWElementSet<T> other) {
82-
for (Map.Entry<T, Element<T>> entry : other.addSet.entrySet()) {
83-
addSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict);
84-
}
85-
for (Map.Entry<T, Element<T>> entry : other.removeSet.entrySet()) {
86-
removeSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict);
87-
}
76+
/**
77+
* Merges another LWWElementSet into this set. This method takes the union of both the add-sets
78+
* and remove-sets from the two sets, resolving conflicts by keeping the element with the latest
79+
* timestamp. If an element appears in both the add-set and remove-set of both sets, the one with
80+
* the later timestamp will be retained.
81+
*
82+
* @param other The LWWElementSet to merge with the current set.
83+
*/
84+
public void merge(LWWElementSet<T> other) {
85+
for (Map.Entry<T, Element<T>> entry : other.addSet.entrySet()) {
86+
addSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict);
8887
}
89-
90-
/**
91-
* Resolves conflicts between two elements by selecting the one with the later timestamp.
92-
* This method is used when merging two LWWElementSets to ensure that the most recent operation (based on timestamps) is kept.
93-
*
94-
* @param e1 The first element.
95-
* @param e2 The second element.
96-
* @return The element with the later timestamp.
97-
*/
98-
private Element<T> resolveConflict(Element<T> e1, Element<T> e2) {
99-
return e1.timestamp.isAfter(e2.timestamp) ? e1 : e2;
88+
for (Map.Entry<T, Element<T>> entry : other.removeSet.entrySet()) {
89+
removeSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict);
10090
}
91+
}
92+
93+
/**
94+
* Resolves conflicts between two elements by selecting the one with the later timestamp. This
95+
* method is used when merging two LWWElementSets to ensure that the most recent operation (based
96+
* on timestamps) is kept.
97+
*
98+
* @param e1 The first element.
99+
* @param e2 The second element.
100+
* @return The element with the later timestamp.
101+
*/
102+
private Element<T> resolveConflict(Element<T> e1, Element<T> e2) {
103+
return e1.timestamp.isAfter(e2.timestamp) ? e1 : e2;
104+
}
101105
}
102106

103107
/**
104-
* Represents an element in the LWWElementSet, consisting of a key and a timestamp.
105-
* This class is used to store the elements in both the add and remove sets with their respective timestamps.
108+
* Represents an element in the LWWElementSet, consisting of a key and a timestamp. This class is
109+
* used to store the elements in both the add and remove sets with their respective timestamps.
106110
*
107111
* @param <T> The type of the key associated with the element.
108112
*/
109113
class Element<T> {
110-
T key;
111-
Instant timestamp;
114+
T key;
115+
Instant timestamp;
112116

113-
/**
114-
* Constructs a new Element with the specified key and timestamp.
115-
*
116-
* @param key The key of the element.
117-
* @param timestamp The timestamp associated with the element.
118-
*/
119-
Element(T key, Instant timestamp) {
120-
this.key = key;
121-
this.timestamp = timestamp;
122-
}
117+
/**
118+
* Constructs a new Element with the specified key and timestamp.
119+
*
120+
* @param key The key of the element.
121+
* @param timestamp The timestamp associated with the element.
122+
*/
123+
Element(T key, Instant timestamp) {
124+
this.key = key;
125+
this.timestamp = timestamp;
126+
}
123127
}
124-

0 commit comments

Comments
 (0)