Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

adding an map builder used in autogenerated JAX-RS endpoints #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.justice.services.adapter.rest.builder;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* Builder class that builds an immutable map
*
*/
public class UnmodifiableMapBuilder<K, T> {
private final Map<K, T> map = new HashMap<>();

/**
* @param key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Public method, javadoc

* @param value
* @return - builder instance with key and value set
*/
public UnmodifiableMapBuilder<K, T> with(final K key, final T value) {
map.put(key, value);
return this;
}

/**
* @return an immutable map with entries set by invocations of the with
* method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

odd formatting, wrapped around onto next line? or it this just github?

*/
public Map<K, T> build() {
return Collections.unmodifiableMap(map);
}
}
4 changes: 4 additions & 0 deletions rest-adapter/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package uk.gov.justice.services.adapter.rest.builder;

import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.junit.Assert.assertThat;

import java.util.Map;

import org.junit.Test;

public class UnmodifiableMapBuilderTest {

@Test
public void shouldBuildEmptyMap() {
Map<String, String> map = new UnmodifiableMapBuilder<String, String>().build();
assertThat(map.entrySet(), iterableWithSize(0));
}

@Test
public void shouldBuildMapWithOneEntry() {
Map<String, String> map = new UnmodifiableMapBuilder<String, String>().with("key123", "valueABC").build();
assertThat(map.entrySet(), iterableWithSize(1));
assertThat(map, hasEntry("key123", "valueABC"));
}

@Test
public void shouldBuildMapWithThreeEntry() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreeEntries method name

Map<String, String> map = new UnmodifiableMapBuilder<String, String>().with("keyA", "v1").with("keyB", "v2")
.with("keyC", "v3").build();
assertThat(map.entrySet(), iterableWithSize(3));
assertThat(map, hasEntry("keyA", "v1"));
assertThat(map, hasEntry("keyB", "v2"));
assertThat(map, hasEntry("keyC", "v3"));
}

@Test(expected=UnsupportedOperationException.class)
public void shouldThrowExceptionOnModificationAttempt() {
Map<String, String> map = new UnmodifiableMapBuilder<String, String>().build();
map.put("k1", "v1");
}

}