Skip to content

Commit 288be7c

Browse files
committed
test clang format
1 parent 6a84e68 commit 288be7c

File tree

2 files changed

+179
-177
lines changed

2 files changed

+179
-177
lines changed

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

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -14,94 +14,94 @@
1414
*
1515
* @param <T> The type of the elements in the LWWElementSet.
1616
* @author <a href="https://github.com/itakurah">itakurah (GitHub)</a>, <a
17-
* href="https://www.linkedin.com/in/niklashoefflin/">Niklas Hoefflin (LinkedIn)</a>
17+
* href="https://www.linkedin.com/in/niklashoefflin/">Niklas Hoefflin (LinkedIn)</a>
1818
* @see <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type">Conflict free
19-
* replicated data type (Wikipedia)</a>
19+
* replicated data type (Wikipedia)</a>
2020
* @see <a href="https://inria.hal.science/inria-00555588v1/document">A comprehensive study of
21-
* Convergent and Commutative Replicated Data Types</a>
21+
* Convergent and Commutative Replicated Data Types</a>
2222
*/
2323
class LWWElementSet<T> {
24-
final Map<T, Element<T>> addSet;
25-
final Map<T, Element<T>> removeSet;
24+
final Map<T, Element<T>> addSet;
25+
final Map<T, Element<T>> removeSet;
2626

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-
}
36-
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-
}
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+
}
4736

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-
}
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+
}
5747

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);
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+
}
7157

72-
return inAddSet != null
73-
&& (inRemoveSet == null || inAddSet.timestamp.isAfter(inRemoveSet.timestamp));
74-
}
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);
7571

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);
72+
return inAddSet != null
73+
&& (inRemoveSet == null || inAddSet.timestamp.isAfter(inRemoveSet.timestamp));
8774
}
88-
for (Map.Entry<T, Element<T>> entry : other.removeSet.entrySet()) {
89-
removeSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict);
75+
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);
87+
}
88+
for (Map.Entry<T, Element<T>> entry : other.removeSet.entrySet()) {
89+
removeSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict);
90+
}
9091
}
91-
}
9292

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-
}
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+
}
105105
}
106106

107107
/**
@@ -111,17 +111,17 @@ private Element<T> resolveConflict(Element<T> e1, Element<T> e2) {
111111
* @param <T> The type of the key associated with the element.
112112
*/
113113
class Element<T> {
114-
T key;
115-
Instant timestamp;
114+
T key;
115+
Instant timestamp;
116116

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-
}
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+
}
127127
}
Lines changed: 91 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,99 @@
11
package com.thealgorithms.datastructures.crdt;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import org.junit.jupiter.api.Test;
44

55
import java.time.Instant;
6-
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
79

810
class LWWElementSetTest {
911

10-
@Test
11-
void testAddElement() {
12-
LWWElementSet<String> set = new LWWElementSet<>();
13-
set.add("A");
14-
assertTrue(set.lookup("A"));
15-
}
16-
17-
@Test
18-
void testRemoveElement() {
19-
LWWElementSet<String> set = new LWWElementSet<>();
20-
set.add("A");
21-
set.remove("A");
22-
assertFalse(set.lookup("A"));
23-
}
24-
25-
@Test
26-
void testLookupWithoutAdding() {
27-
LWWElementSet<String> set = new LWWElementSet<>();
28-
assertFalse(set.lookup("A"));
29-
}
30-
31-
@Test
32-
void testLookupLaterTimestampsFalse() {
33-
LWWElementSet<String> set = new LWWElementSet<>();
34-
35-
set.addSet.put("A", new Element<>("A", Instant.now()));
36-
set.removeSet.put("A", new Element<>("A", Instant.now().plusSeconds(10)));
37-
38-
assertFalse(set.lookup("A"));
39-
}
40-
41-
@Test
42-
void testLookupEarlierTimestampsTrue() {
43-
LWWElementSet<String> set = new LWWElementSet<>();
44-
45-
set.addSet.put("A", new Element<>("A", Instant.now()));
46-
set.removeSet.put("A", new Element<>("A", Instant.now().minusSeconds(10)));
47-
48-
assertTrue(set.lookup("A"));
49-
}
50-
51-
@Test
52-
void testLookupWithConcurrentTimestamps() {
53-
LWWElementSet<String> set = new LWWElementSet<>();
54-
Instant now = Instant.now();
55-
set.addSet.put("A", new Element<>("A", now));
56-
set.removeSet.put("A", new Element<>("A", now));
57-
assertFalse(set.lookup("A"));
58-
}
59-
60-
@Test
61-
void testMergeTwoSets() {
62-
LWWElementSet<String> set1 = new LWWElementSet<>();
63-
LWWElementSet<String> set2 = new LWWElementSet<>();
64-
65-
set1.add("A");
66-
set2.add("B");
67-
set2.remove("A");
68-
69-
set1.merge(set2);
70-
71-
assertFalse(set1.lookup("A"));
72-
assertTrue(set1.lookup("B"));
73-
}
74-
75-
@Test
76-
void testMergeWithConflictingTimestamps() {
77-
LWWElementSet<String> set1 = new LWWElementSet<>();
78-
LWWElementSet<String> set2 = new LWWElementSet<>();
79-
80-
Instant now = Instant.now();
81-
set1.addSet.put("A", new Element<>("A", now.minusSeconds(10)));
82-
set2.addSet.put("A", new Element<>("A", now));
83-
84-
set1.merge(set2);
85-
86-
assertTrue(set1.lookup("A"));
87-
}
88-
89-
@Test
90-
void testRemoveOlderThanAdd() {
91-
LWWElementSet<String> set = new LWWElementSet<>();
92-
Instant now = Instant.now();
93-
set.addSet.put("A", new Element<>("A", now));
94-
set.removeSet.put("A", new Element<>("A", now.minusSeconds(10)));
95-
assertTrue(set.lookup("A"));
96-
}
12+
@Test
13+
void testAddElement() {
14+
LWWElementSet<String> set = new LWWElementSet<>();
15+
set.add("A");
16+
assertTrue(set.lookup("A"));
17+
}
18+
19+
@Test
20+
void testRemoveElement() {
21+
LWWElementSet<String> set = new LWWElementSet<>();
22+
set.add("A");
23+
set.remove("A");
24+
assertFalse(set.lookup("A"));
25+
}
26+
27+
@Test
28+
void testLookupWithoutAdding() {
29+
LWWElementSet<String> set = new LWWElementSet<>();
30+
assertFalse(set.lookup("A"));
31+
}
32+
33+
@Test
34+
void testLookupLaterTimestampsFalse() {
35+
LWWElementSet<String> set = new LWWElementSet<>();
36+
37+
set.addSet.put("A", new Element<>("A", Instant.now()));
38+
set.removeSet.put("A", new Element<>("A", Instant.now().plusSeconds(10)));
39+
40+
assertFalse(set.lookup("A"));
41+
}
42+
43+
@Test
44+
void testLookupEarlierTimestampsTrue() {
45+
LWWElementSet<String> set = new LWWElementSet<>();
46+
47+
set.addSet.put("A", new Element<>("A", Instant.now()));
48+
set.removeSet.put("A", new Element<>("A", Instant.now().minusSeconds(10)));
49+
50+
assertTrue(set.lookup("A"));
51+
}
52+
53+
@Test
54+
void testLookupWithConcurrentTimestamps() {
55+
LWWElementSet<String> set = new LWWElementSet<>();
56+
Instant now = Instant.now();
57+
set.addSet.put("A", new Element<>("A", now));
58+
set.removeSet.put("A", new Element<>("A", now));
59+
assertFalse(set.lookup("A"));
60+
}
61+
62+
@Test
63+
void testMergeTwoSets() {
64+
LWWElementSet<String> set1 = new LWWElementSet<>();
65+
LWWElementSet<String> set2 = new LWWElementSet<>();
66+
67+
set1.add("A");
68+
set2.add("B");
69+
set2.remove("A");
70+
71+
set1.merge(set2);
72+
73+
assertFalse(set1.lookup("A"));
74+
assertTrue(set1.lookup("B"));
75+
}
76+
77+
@Test
78+
void testMergeWithConflictingTimestamps() {
79+
LWWElementSet<String> set1 = new LWWElementSet<>();
80+
LWWElementSet<String> set2 = new LWWElementSet<>();
81+
82+
Instant now = Instant.now();
83+
set1.addSet.put("A", new Element<>("A", now.minusSeconds(10)));
84+
set2.addSet.put("A", new Element<>("A", now));
85+
86+
set1.merge(set2);
87+
88+
assertTrue(set1.lookup("A"));
89+
}
90+
91+
@Test
92+
void testRemoveOlderThanAdd() {
93+
LWWElementSet<String> set = new LWWElementSet<>();
94+
Instant now = Instant.now();
95+
set.addSet.put("A", new Element<>("A", now));
96+
set.removeSet.put("A", new Element<>("A", now.minusSeconds(10)));
97+
assertTrue(set.lookup("A"));
98+
}
9799
}

0 commit comments

Comments
 (0)