Skip to content

Commit 343dfde

Browse files
committed
feat: Add DuplicateFinder
1 parent e5cf929 commit 343dfde

14 files changed

Lines changed: 263 additions & 16 deletions

src/main/java/org/hisp/dhis/util/CollectionUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
import java.util.HashSet;
3535
import java.util.List;
3636
import java.util.Set;
37+
import java.util.function.Function;
3738
import java.util.function.Predicate;
3839
import java.util.stream.Collectors;
3940
import lombok.AccessLevel;
4041
import lombok.NoArgsConstructor;
4142

43+
/**
44+
* Utilities for collections.
45+
*/
4246
@NoArgsConstructor(access = AccessLevel.PRIVATE)
4347
public class CollectionUtils {
4448
/**
@@ -100,6 +104,21 @@ public static <T> List<T> mutableList(T... items) {
100104

101105
return list;
102106
}
107+
108+
/**
109+
* Maps the given list of objects of type <U> to a list of objects of type <T>. Null objects are not allowed.
110+
*
111+
* @param <T> type.
112+
* @param <U> type.
113+
* @param objects the objects of type <U>.
114+
* @param mapper the mapping function.
115+
* @return a list of objects of type <T>.
116+
*/
117+
public static <T, U> List<T> mapToList(List<U> objects, Function<U, T> mapper) {
118+
return objects.stream()
119+
.map(mapper)
120+
.toList();
121+
}
103122

104123
/**
105124
* Converts the given array to an {@link ArrayList}.

src/main/java/org/hisp/dhis/util/ConfigUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
import java.util.Arrays;
3232
import java.util.List;
3333
import java.util.stream.Collectors;
34-
import lombok.AccessLevel;
35-
import lombok.NoArgsConstructor;
3634
import org.apache.commons.lang3.ObjectUtils;
3735
import org.apache.commons.lang3.StringUtils;
36+
import lombok.AccessLevel;
37+
import lombok.NoArgsConstructor;
3838

39+
/**
40+
* Utilities for configuration.
41+
*/
3942
@NoArgsConstructor(access = AccessLevel.PRIVATE)
4043
public class ConfigUtils {
4144
/**

src/main/java/org/hisp/dhis/util/DateTimeUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@
3535
import java.time.ZoneId;
3636
import java.time.format.DateTimeFormatter;
3737
import java.util.Date;
38+
import org.apache.commons.lang3.StringUtils;
3839
import lombok.AccessLevel;
3940
import lombok.NoArgsConstructor;
40-
import org.apache.commons.lang3.StringUtils;
4141

42+
/**
43+
* Utilities for date and time.
44+
*/
4245
@NoArgsConstructor(access = AccessLevel.PRIVATE)
4346
public class DateTimeUtils {
4447
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.hisp.dhis.util;
2+
3+
import java.util.Collection;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Duplicate value finder.
9+
*/
10+
public class DuplicateFinder {
11+
12+
private Map<String, Integer> valueCounts;
13+
14+
/**
15+
* Constructor.
16+
*/
17+
public DuplicateFinder() {
18+
this.valueCounts = new HashMap<>();
19+
}
20+
21+
/**
22+
* Constructor.
23+
*
24+
* @param values the collection of values.
25+
*/
26+
public DuplicateFinder(Collection<String> values) {
27+
this();
28+
addAll(values);
29+
}
30+
31+
/**
32+
* Adds a value.
33+
*
34+
* @param value the value.
35+
*/
36+
public void add(String value) {
37+
valueCounts.put(value, valueCounts.getOrDefault(value, 0) + 1);
38+
}
39+
40+
/**
41+
* Adds a collection of values.
42+
*
43+
* @param values the values.
44+
*/
45+
public void addAll(Collection<String> values) {
46+
values.forEach(value -> add(value));
47+
}
48+
49+
/**
50+
* Returns the duplicates as a map, where the key is the duplicate value and the value is the
51+
* number of occurrences.
52+
*
53+
* @return the duplicates as a map.
54+
*/
55+
public Map<String, Integer> getDuplicates() {
56+
Map<String, Integer> duplicates = new HashMap<>();
57+
for (Map.Entry<String, Integer> entry : valueCounts.entrySet()) {
58+
if (entry.getValue() > 1) {
59+
duplicates.put(entry.getKey(), entry.getValue());
60+
}
61+
}
62+
return duplicates;
63+
}
64+
}

src/main/java/org/hisp/dhis/util/GeoUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
*/
2828
package org.hisp.dhis.util;
2929

30-
import lombok.AccessLevel;
31-
import lombok.NoArgsConstructor;
3230
import org.locationtech.jts.geom.Coordinate;
3331
import org.locationtech.jts.geom.GeometryFactory;
3432
import org.locationtech.jts.geom.Point;
33+
import lombok.AccessLevel;
34+
import lombok.NoArgsConstructor;
3535

36+
/**
37+
* Utilities for geospatial objects.
38+
*/
3639
@NoArgsConstructor(access = AccessLevel.PRIVATE)
3740
public class GeoUtils {
3841
private static final GeometryFactory GEO_FACTORY = new GeometryFactory();

src/main/java/org/hisp/dhis/util/HttpUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
3232
import java.net.URISyntaxException;
3333
import java.net.URLDecoder;
3434
import java.nio.charset.StandardCharsets;
35-
import lombok.AccessLevel;
36-
import lombok.NoArgsConstructor;
3735
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
3836
import org.apache.hc.core5.http.message.BasicHttpRequest;
3937
import org.apache.hc.core5.net.URIBuilder;
4038
import org.hisp.dhis.Dhis2Config;
4139
import org.hisp.dhis.auth.Authentication;
40+
import lombok.AccessLevel;
41+
import lombok.NoArgsConstructor;
4242

43+
/**
44+
* Utilities for HTTP communication.
45+
*/
4346
@NoArgsConstructor(access = AccessLevel.PRIVATE)
4447
public class HttpUtils {
4548
/**

src/main/java/org/hisp/dhis/util/IdentifiableObjectUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import java.util.List;
3232
import java.util.Objects;
3333
import java.util.stream.Collectors;
34+
import org.hisp.dhis.model.IdentifiableObject;
3435
import lombok.AccessLevel;
3536
import lombok.NoArgsConstructor;
36-
import org.hisp.dhis.model.IdentifiableObject;
3737

38+
/**
39+
* Utilities for {@link IdentifiableObject}.
40+
*/
3841
@NoArgsConstructor(access = AccessLevel.PRIVATE)
3942
public class IdentifiableObjectUtils {
4043
/**

src/main/java/org/hisp/dhis/util/JacksonUtils.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@
2727
*/
2828
package org.hisp.dhis.util;
2929

30+
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.io.UncheckedIOException;
33+
import java.text.SimpleDateFormat;
3034
import com.bedatadriven.jackson.datatype.jts.JtsModule;
3135
import com.fasterxml.jackson.annotation.JsonInclude.Include;
3236
import com.fasterxml.jackson.databind.DeserializationFeature;
3337
import com.fasterxml.jackson.databind.ObjectMapper;
3438
import com.fasterxml.jackson.databind.SerializationFeature;
35-
import java.io.IOException;
36-
import java.io.InputStream;
37-
import java.io.UncheckedIOException;
38-
import java.text.SimpleDateFormat;
3939
import lombok.AccessLevel;
4040
import lombok.NoArgsConstructor;
4141

42+
/**
43+
* Utilities for JSON parsing and serialization.
44+
*/
4245
@NoArgsConstructor(access = AccessLevel.PRIVATE)
4346
public class JacksonUtils {
4447
/** Default date format. */

src/main/java/org/hisp/dhis/util/ListBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@
3232
import java.util.Collections;
3333
import java.util.List;
3434

35+
/**
36+
* Builder of lists.
37+
*
38+
* <p>Example usage:
39+
*
40+
* <pre>{@code
41+
* List<T> = new ListBuilder<T>()
42+
* .add(valueA)
43+
* .add(valueB, valueC)
44+
* .addAll(List.of(valueD, valueE))
45+
* .build();
46+
* }</pre>
47+
*/
3548
public class ListBuilder<T> {
3649
private final List<T> list;
3750

src/main/java/org/hisp/dhis/util/UidUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
import lombok.AccessLevel;
3333
import lombok.NoArgsConstructor;
3434

35+
/**
36+
* Utilities for UID.
37+
*/
3538
@NoArgsConstructor(access = AccessLevel.PRIVATE)
3639
public class UidUtils {
3740
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

0 commit comments

Comments
 (0)