Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Jul 3, 2023
2 parents e8673d8 + d37b63a commit d8752a9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 43 deletions.
9 changes: 9 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="1.6.4" date="2023-07-03">
<action type="fix" dev="sseifert">
ImmutableValueMap.of: Retain order of items as stated in JavaDoc.
</action>
<action type="fix" dev="sseifert">
ImmutableValueMap.copyOf: Ensure map is immutable.
</action>
</release>

<release version="1.6.2" date="2023-04-19">
<action type="update" dev="sseifert">
Switch to Java 11 as minimum version.
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.wcm</groupId>
<artifactId>io.wcm.sling.commons</artifactId>
<version>1.6.2</version>
<version>1.6.4</version>
<packaging>jar</packaging>

<name>Sling Commons</name>
Expand All @@ -49,7 +49,7 @@
<site.url.module.prefix>sling/commons</site.url.module.prefix>

<!-- Enable reproducible builds -->
<project.build.outputTimestamp>2023-04-19T11:06:55Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2023-07-03T09:55:00Z</project.build.outputTimestamp>
</properties>

<dependencies>
Expand Down
48 changes: 30 additions & 18 deletions src/main/java/io/wcm/sling/commons/resource/ImmutableValueMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -185,9 +185,10 @@ public void clear() {
* @param v1 Value 1
* @return ImmutableValueMap
*/
@SuppressWarnings("null")
public static @NotNull ImmutableValueMap of(@NotNull String k1, @NotNull Object v1) {
return new ImmutableValueMap(Map.of(k1, v1));
Map<String, Object> map = new LinkedHashMap<>();
map.put(k1, v1);
return new ImmutableValueMap(Collections.unmodifiableMap(map));
}

/**
Expand All @@ -199,10 +200,12 @@ public void clear() {
* @return ImmutableValueMap
* @throws IllegalArgumentException if duplicate keys are provided
*/
@SuppressWarnings("null")
public static @NotNull ImmutableValueMap of(@NotNull String k1, @NotNull Object v1,
@NotNull String k2, @NotNull Object v2) {
return new ImmutableValueMap(Map.of(k1, v1, k2, v2));
Map<String, Object> map = new LinkedHashMap<>();
map.put(k1, v1);
map.put(k2, v2);
return new ImmutableValueMap(Collections.unmodifiableMap(map));
}

/**
Expand All @@ -216,12 +219,15 @@ public void clear() {
* @return ImmutableValueMap
* @throws IllegalArgumentException if duplicate keys are provided
*/
@SuppressWarnings("null")
public static @NotNull ImmutableValueMap of(
@NotNull String k1, @NotNull Object v1,
@NotNull String k2, @NotNull Object v2,
@NotNull String k3, @NotNull Object v3) {
return new ImmutableValueMap(Map.of(k1, v1, k2, v2, k3, v3));
Map<String, Object> map = new LinkedHashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
return new ImmutableValueMap(Collections.unmodifiableMap(map));
}

/**
Expand All @@ -237,13 +243,18 @@ public void clear() {
* @return ImmutableValueMap
* @throws IllegalArgumentException if duplicate keys are provided
*/
@SuppressWarnings({ "null", "java:S107", "PMD.UseObjectForClearerAPI" })
@SuppressWarnings({ "java:S107", "PMD.UseObjectForClearerAPI" })
public static @NotNull ImmutableValueMap of(
@NotNull String k1, @NotNull Object v1,
@NotNull String k2, @NotNull Object v2,
@NotNull String k3, @NotNull Object v3,
@NotNull String k4, @NotNull Object v4) {
return new ImmutableValueMap(Map.of(k1, v1, k2, v2, k3, v3, k4, v4));
Map<String, Object> map = new LinkedHashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
return new ImmutableValueMap(Collections.unmodifiableMap(map));
}

/**
Expand All @@ -261,14 +272,20 @@ public void clear() {
* @return ImmutableValueMap
* @throws IllegalArgumentException if duplicate keys are provided
*/
@SuppressWarnings({ "null", "java:S107", "PMD.UseObjectForClearerAPI" })
@SuppressWarnings({ "java:S107", "PMD.UseObjectForClearerAPI" })
public static ImmutableValueMap of(
@NotNull String k1, @NotNull Object v1,
@NotNull String k2, @NotNull Object v2,
@NotNull String k3, @NotNull Object v3,
@NotNull String k4, @NotNull Object v4,
@NotNull String k5, @NotNull Object v5) {
return new ImmutableValueMap(Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5));
Map<String, Object> map = new LinkedHashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
return new ImmutableValueMap(Collections.unmodifiableMap(map));
}

// looking for of() with > 5 entries? Use the builder instead.
Expand Down Expand Up @@ -296,20 +313,15 @@ public static ImmutableValueMap of(
* @throws NullPointerException if any key or value in {@code map} is null
*/
public static @NotNull ImmutableValueMap copyOf(@NotNull Map<String, Object> map) {
if (map instanceof ValueMap) {
return new ImmutableValueMap((ValueMap)map);
}
else {
return new ImmutableValueMap(map);
}
return new ImmutableValueMap(Collections.unmodifiableMap(map));
}

/**
* Builder interface for {@link ImmutableValueMap}.
*/
public static final class Builder {

private final @NotNull Map<String, Object> map = new HashMap<>();
private final @NotNull Map<String, Object> map = new LinkedHashMap<>();

/**
* Associates {@code key} with {@code value} in the built map. Duplicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Iterator;
import java.util.Map;

import org.apache.sling.api.resource.ValueMap;
Expand Down Expand Up @@ -107,46 +108,31 @@ void testOf() {
@Test
void testOfx1() {
ValueMap map = ImmutableValueMap.of("p1", "v1");
assertEquals(1, map.size());
assertEquals("v1", map.get("p1"));
assertWithOrder(map, "p1", "v1");
}

@Test
void testOfx2() {
ValueMap map = ImmutableValueMap.of("p1", "v1", "p2", "v2");
assertEquals(2, map.size());
assertEquals("v1", map.get("p1"));
assertEquals("v2", map.get("p2"));
ValueMap map = ImmutableValueMap.of("p2", "v2", "p1", "v1");
assertWithOrder(map, "p2", "v2", "p1", "v1");
}

@Test
void testOfx3() {
ValueMap map = ImmutableValueMap.of("p1", "v1", "p2", "v2", "p3", "v3");
assertEquals(3, map.size());
assertEquals("v1", map.get("p1"));
assertEquals("v2", map.get("p2"));
assertEquals("v3", map.get("p3"));
assertWithOrder(map, "p1", "v1", "p2", "v2", "p3", "v3");
}

@Test
void testOfx4() {
ValueMap map = ImmutableValueMap.of("p1", "v1", "p2", "v2", "p3", "v3", "p4", "v4");
assertEquals(4, map.size());
assertEquals("v1", map.get("p1"));
assertEquals("v2", map.get("p2"));
assertEquals("v3", map.get("p3"));
assertEquals("v4", map.get("p4"));
ValueMap map = ImmutableValueMap.of("p3", "v3", "p2", "v2", "p1", "v1", "p4", "v4");
assertWithOrder(map, "p3", "v3", "p2", "v2", "p1", "v1", "p4", "v4");
}

@Test
void testOfx5() {
ValueMap map = ImmutableValueMap.of("p1", "v1", "p2", "v2", "p3", "v3", "p4", "v4", "p5", "v5");
assertEquals(5, map.size());
assertEquals("v1", map.get("p1"));
assertEquals("v2", map.get("p2"));
assertEquals("v3", map.get("p3"));
assertEquals("v4", map.get("p4"));
assertEquals("v5", map.get("p5"));
ValueMap map = ImmutableValueMap.of("p1", "v1", "p5", "v5", "p3", "v3", "p4", "v4", "p2", "v2");
assertWithOrder(map, "p1", "v1", "p5", "v5", "p3", "v3", "p4", "v4", "p2", "v2");
}

@Test
Expand Down Expand Up @@ -211,4 +197,17 @@ void testToString() {
assertEquals("{}", ImmutableValueMap.of().toString());
}

private void assertWithOrder(Map<String, Object> map, Object... items) {
int count = items.length / 2;
assertEquals(count, map.size(), "map size");
Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
for (int i = 0; i < items.length - 1; i = i + 2) {
Object key = items[i];
Object value = items[i + 1];
Map.Entry<String, Object> entry = entries.next();
assertEquals(key, entry.getKey(), "Key Entry #" + (i / 2));
assertEquals(value, entry.getValue(), "Value Entry #" + (i / 2));
}
}

}

0 comments on commit d8752a9

Please sign in to comment.