Skip to content

Commit 90ef814

Browse files
authored
Remove commons-collections and add Multimaps (#795)
1 parent 6dfb4e9 commit 90ef814

File tree

31 files changed

+608
-125
lines changed

31 files changed

+608
-125
lines changed

.github/workflows/integrationTests.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: Integration Tests
22
on:
33
push:
4-
branches:
5-
- master
64
# Triggers the workflow on labeled PRs only.
75
pull_request_target:
86
types: [ labeled ]

build-info-api/src/main/java/org/jfrog/build/api/Issues.java

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.io.Serializable;
77
import java.util.HashSet;
88
import java.util.Set;
9-
import java.util.stream.Collectors;
109

1110
/**
1211
* @author Noam Y. Tenne
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.jfrog.build.api.multiMap;
2+
3+
import java.util.Collection;
4+
import java.util.LinkedList;
5+
import java.util.Map;
6+
7+
/**
8+
* A multimap that uses a {@link LinkedList} to store the values.
9+
*/
10+
public class ListMultimap<Key, Value> extends Multimap<Key, Value> {
11+
12+
/**
13+
* Default constructor.
14+
*/
15+
public ListMultimap() {
16+
super();
17+
}
18+
19+
/**
20+
* Constructor that accepts a map.
21+
*
22+
* @param map the map
23+
*/
24+
public ListMultimap(Map<Key, Value> map) {
25+
super(map);
26+
}
27+
28+
/**
29+
* Put a key-value pair into the multimap.
30+
*
31+
* @param key the key
32+
* @param value the value
33+
*/
34+
public void put(Key key, Value value) {
35+
Collection<Value> currentValue = multiMap.getOrDefault(key, new LinkedList<>());
36+
currentValue.add(value);
37+
multiMap.put(key, currentValue);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package org.jfrog.build.api.multiMap;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
6+
import java.io.Serializable;
7+
import java.util.*;
8+
9+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
10+
@JsonSubTypes({
11+
@JsonSubTypes.Type(value = ListMultimap.class, name = "list"),
12+
@JsonSubTypes.Type(value = SetMultimap.class, name = "set"),
13+
})
14+
public abstract class Multimap<Key, Value> implements Serializable {
15+
private static final long serialVersionUID = 1L;
16+
Map<Key, Collection<Value>> multiMap = new HashMap<>();
17+
18+
/**
19+
* Default constructor.
20+
*/
21+
Multimap() {
22+
}
23+
24+
/**
25+
* Constructor that accepts a map.
26+
*
27+
* @param map the map
28+
*/
29+
Multimap(Map<Key, Value> map) {
30+
map.forEach(this::put);
31+
}
32+
33+
/**
34+
* Put a key-value pair into the multimap.
35+
*
36+
* @param key the key
37+
* @param value the value
38+
*/
39+
public abstract void put(Key key, Value value);
40+
41+
/**
42+
* Get all values for a key.
43+
*
44+
* @param key the key
45+
* @return a collection of values for the key
46+
*/
47+
public Collection<Value> get(Key key) {
48+
return multiMap.get(key);
49+
}
50+
51+
/**
52+
* Put all key-value pairs from a map into the multimap.
53+
*
54+
* @param map the map
55+
*/
56+
public void putAll(Map<Key, Value> map) {
57+
for (Map.Entry<Key, Value> entry : map.entrySet()) {
58+
put(entry.getKey(), entry.getValue());
59+
}
60+
}
61+
62+
/**
63+
* Put all key-value pairs from a multimap into the multimap.
64+
*
65+
* @param multimap the multimap
66+
*/
67+
public void putAll(Multimap<Key, Value> multimap) {
68+
for (Map.Entry<Key, Collection<Value>> entry : multimap.multiMap.entrySet()) {
69+
for (Value value : entry.getValue()) {
70+
put(entry.getKey(), value);
71+
}
72+
}
73+
}
74+
75+
/**
76+
* Put all values for a key into the multimap.
77+
*
78+
* @param key the key
79+
* @param values the values
80+
*/
81+
public void putAll(Key key, Collection<Value> values) {
82+
for (Value value : values) {
83+
put(key, value);
84+
}
85+
}
86+
87+
/**
88+
* Get all key-value pairs in the multimap.
89+
*
90+
* @return a set of key-value pairs
91+
*/
92+
public Set<Map.Entry<Key, Value>> entries() {
93+
Set<Map.Entry<Key, Value>> entries = new HashSet<>();
94+
for (Map.Entry<Key, Collection<Value>> entry : multiMap.entrySet()) {
95+
for (Value value : entry.getValue()) {
96+
entries.add(new AbstractMap.SimpleEntry<>(entry.getKey(), value));
97+
}
98+
}
99+
return entries;
100+
}
101+
102+
/**
103+
* Get the underlying map.
104+
*
105+
* @return the map
106+
*/
107+
public Map<Key, Collection<Value>> asMap() {
108+
return multiMap;
109+
}
110+
111+
/**
112+
* Get all keys in the multimap.
113+
*
114+
* @return a set of keys
115+
*/
116+
public Set<Key> keySet() {
117+
return multiMap.keySet();
118+
}
119+
120+
/**
121+
* Check if the multimap contains a value.
122+
*
123+
* @param value the value
124+
* @return true if the multimap contains the value
125+
*/
126+
public boolean containsValue(Value value) {
127+
for (Collection<Value> values : multiMap.values()) {
128+
if (values.contains(value)) {
129+
return true;
130+
}
131+
}
132+
return false;
133+
}
134+
135+
/**
136+
* Get the number of key-value pairs in the multimap.
137+
*
138+
* @return the number of key-value pairs
139+
*/
140+
public int size() {
141+
int size = 0;
142+
for (Collection<Value> values : multiMap.values()) {
143+
size += values.size();
144+
}
145+
return size;
146+
}
147+
148+
/**
149+
* Check if the multimap is empty.
150+
*
151+
* @return true if the multimap is empty
152+
*/
153+
public boolean isEmpty() {
154+
return multiMap.isEmpty();
155+
}
156+
157+
/**
158+
* Clear the multimap.
159+
*/
160+
public void clear() {
161+
multiMap.clear();
162+
}
163+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.jfrog.build.api.multiMap;
2+
3+
import java.util.Collection;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
7+
/**
8+
* A multimap that uses a {@link HashSet} to store the values.
9+
*/
10+
public class SetMultimap<Key, Value> extends Multimap<Key, Value> {
11+
public SetMultimap() {
12+
super();
13+
}
14+
15+
/**
16+
* Constructor that accepts a map.
17+
*
18+
* @param map the map
19+
*/
20+
public SetMultimap(Map<Key, Value> map) {
21+
super(map);
22+
}
23+
24+
/**
25+
* Put a key-value pair into the multimap.
26+
*
27+
* @param key the key
28+
* @param value the value
29+
*/
30+
public void put(Key key, Value value) {
31+
Collection<Value> currentValue = multiMap.getOrDefault(key, new HashSet<>());
32+
currentValue.add(value);
33+
multiMap.put(key, currentValue);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.jfrog.build.api.multiMap;
2+
3+
import org.testng.annotations.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.testng.Assert.assertEquals;
9+
import static org.testng.Assert.assertTrue;
10+
11+
@Test
12+
public class ListMultimapTest {
13+
14+
public void testDefaultConstructor() {
15+
// Create a new multimap
16+
Multimap<String, String> multimap = new ListMultimap<>();
17+
18+
// Assert that the multimap is empty
19+
assertEquals(multimap.size(), 0);
20+
}
21+
22+
public void testConstructorWithMap() {
23+
// Create a new map
24+
Map<String, String> map = new HashMap<>();
25+
map.put("key", "value");
26+
27+
// Create a new multimap with the map
28+
Multimap<String, String> multimap = new ListMultimap<>(map);
29+
30+
// Assert that the multimap contains the value
31+
assertTrue(multimap.containsValue("value"));
32+
}
33+
34+
public void testPutDuplicated() {
35+
// Populate multimap with duplicated values
36+
Multimap<String, String> multimap = new ListMultimap<>();
37+
multimap.put("key", "value");
38+
multimap.put("key", "value");
39+
40+
// Convert the collection to an array
41+
String[] values = multimap.get("key").toArray(new String[0]);
42+
43+
// Assert that the values were added
44+
assertEquals(values.length, 2);
45+
assertEquals(values[0], "value");
46+
assertEquals(values[1], "value");
47+
}
48+
}

0 commit comments

Comments
 (0)