Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.

Commit 9b27eb9

Browse files
authored
Merge pull request #28 from cdlib/develop
16088_defensive
2 parents 215dbab + a34fbfb commit 9b27eb9

4 files changed

Lines changed: 52 additions & 10 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>org.cdlib</groupId>
55
<artifactId>d2d-commons</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.5.6</version>
7+
<version>1.5.7</version>
88
<name>D2D Commons</name>
99

1010
<!-- comment -->
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>com.fasterxml.jackson.core</groupId>
3030
<artifactId>jackson-databind</artifactId>
31-
<version> 2.9.10.4</version>
31+
<version>2.9.10.5</version>
3232
</dependency>
3333
<dependency>
3434
<groupId>javax.validation</groupId>

src/main/java/org/cdlib/cache/CacheService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Map;
55
import java.util.Optional;
66

7-
public class CacheService<T, S> {
7+
public abstract class CacheService<T, S> {
88

99
protected Map<T, S> cache;
1010

@@ -15,9 +15,7 @@ public void init() {
1515
cache = Collections.synchronizedMap(new Cache<T, S>(cacheSize));
1616
}
1717

18-
public void insertInto(T key, S result) {
19-
cache.put(key, result);
20-
}
18+
public abstract void insertInto(T key, S result);
2119

2220
public Optional<S> getFrom(T key) {
2321
return Optional.ofNullable(cache.get(key));

src/main/java/org/cdlib/domain/objects/link/Link.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@ public class Link {
2020
private String mimeType;
2121

2222
@NotNull
23-
private Map<String, Object> properties;
23+
private Map<String, String> properties;
2424

2525
public Link() {}
2626

2727
public Link(Link source) {
2828
this.href = source.href;
2929
this.mimeType = source.mimeType;
30+
this.properties = deepCopy(source.properties);
31+
}
32+
33+
@SuppressWarnings("unchecked")
34+
private static Map<String, String> deepCopy(Map<String, String> properties) {
35+
String intermediate = JSON.serialize(properties);
36+
return JSON.deserialize(intermediate, properties.getClass());
3037
}
3138

3239
public String getHref() {
@@ -37,7 +44,7 @@ public String getMimeType() {
3744
return mimeType;
3845
}
3946

40-
public Map<String, Object> getProperties() {
47+
public Map<String, String> getProperties() {
4148
return properties;
4249
}
4350

@@ -49,7 +56,7 @@ public void setMimeType(String mimeType) {
4956
this.mimeType = mimeType;
5057
}
5158

52-
public void setProperties(Map<String, Object> properties) {
59+
public void setProperties(Map<String, String> properties) {
5360
this.properties = properties;
5461
}
5562

@@ -70,7 +77,9 @@ public boolean equals(Object obj) {
7077
return false;
7178
}
7279
Link other = (Link) obj;
73-
return Objects.equals(href, other.href) && Objects.equals(mimeType, other.mimeType) && Objects.equals(properties, other.properties);
80+
return Objects.equals(href, other.href)
81+
&& Objects.equals(mimeType, other.mimeType)
82+
&& Objects.equals(properties, other.properties);
7483
}
7584

7685
@Override
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.cdlib.domain.objects.link;
2+
3+
import static org.junit.Assert.*;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import org.cdlib.domain.objects.bib.Carrier;
7+
import org.junit.Test;
8+
9+
public class LinkTest {
10+
11+
@Test
12+
public void testDeepCopy() {
13+
Link original = testLink();
14+
Link copy = new Link(original);
15+
assertFalse(copy == original);
16+
assertFalse(copy.getProperties() == original.getProperties());
17+
assertTrue(copy.getProperties().equals(original.getProperties()));
18+
assertTrue("result".equals(copy.getProperties().get("test")));
19+
assertFalse(copy.getProperties().get("test") == original.getProperties().get("test"));
20+
assertEquals(copy.getProperties().get("carrier"), original.getProperties().get("carrier"));
21+
assertEquals("BRAILLE", copy.getProperties().get("carrier"));
22+
}
23+
24+
private Link testLink() {
25+
Link testLink = new Link();
26+
testLink.setHref("href");
27+
testLink.setMimeType("mimeType");
28+
Map<String, String> testProperties = new HashMap<>();
29+
testProperties.put("test", "result");
30+
testProperties.put("carrier", Carrier.BRAILLE.name());
31+
testLink.setProperties(testProperties);
32+
return testLink;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)