Addressed comments

This commit is contained in:
Family 2020-10-03 21:12:36 +05:30
parent a7cb0ad3c7
commit 0756ad6c7f
2 changed files with 17 additions and 20 deletions

View File

@ -17,19 +17,18 @@ import java.util.*;
* builderMap.put("Y", 26);
* }</pre>
*
* @author Amita Pradhan
*/
public class MapBuilder {
public class MapBuilder<K,V> {
private Comparator comparator;
private Comparator<? super K> comparator;
private KeyOrder iterationOrder;
private boolean synchronizedMap;
private boolean immutable;
private Map data;
private Map<K,V> data;
public MapBuilder() {
comparator = null;
iterationOrder = KeyOrder.RANDOM;
iterationOrder = KeyOrder.UNORDERED;
synchronizedMap = false;
immutable = false;
data = null;
@ -44,7 +43,7 @@ public class MapBuilder {
}
/*
Sets the Iteration order to be used from [RANDOM, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER]
Sets the Iteration order to be used from [UNORDERED, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER]
*/
public MapBuilder setIterationOrder(KeyOrder iterationOrder) {
this.iterationOrder = iterationOrder;
@ -79,7 +78,7 @@ public class MapBuilder {
Builder Method which takes care of all the conditions and returns the required Map.
*/
public Map build() {
Map map;
Map<K,V> map;
switch (iterationOrder) {
case NATURAL_ORDER :
case COMPARATOR_ORDER:
@ -110,6 +109,6 @@ public class MapBuilder {
enum KeyOrder
{
RANDOM, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER;
UNORDERED, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER;
}
}

View File

@ -7,8 +7,6 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test Cases for Map Builder
*/
@ -17,12 +15,12 @@ class MapBuilderTest {
@Test
void setComparator() {
// Null Comparator
Map myMap = new HashMap();
Map<String, Integer> myMap = new HashMap();
myMap.put("A", 1);
myMap.put("X", 24);
myMap.put("B", 2);
myMap.put("Y", 26);
Map builderMap = new MapBuilder().setData(myMap).setComparator(null).build();
Map<String, Integer> builderMap = new MapBuilder().setData(myMap).setComparator(null).build();
Assert.assertEquals(myMap, builderMap);
// Reverse comparator
@ -35,13 +33,13 @@ class MapBuilderTest {
@Test
void setIterationOrder() {
//Key Order = RANDOM
Map myMap = new HashMap();
//Key Order = UNORDERED
Map<String, Integer> myMap = new HashMap();
myMap.put("A", 1);
myMap.put("X", 24);
myMap.put("B", 2);
myMap.put("Y", 26);
Map builderMap = new MapBuilder().setData(myMap).setIterationOrder(MapBuilder.KeyOrder.RANDOM).build();
Map<String, Integer> builderMap = new MapBuilder().setData(myMap).setIterationOrder(MapBuilder.KeyOrder.UNORDERED).build();
Assert.assertEquals(myMap, builderMap);
//Key Order = INSERTION ORDER
@ -91,10 +89,10 @@ class MapBuilderTest {
@Test
void setImmutable() {
Map myMap = new HashMap();
Map<String, Integer> myMap = new HashMap();
myMap.put("A", 1);
myMap.put("B", 2);
Map builderMap = new MapBuilder().setData(myMap).setImmutable(true).build();
Map<String, Integer> builderMap = new MapBuilder().setData(myMap).setImmutable(true).build();
boolean exceptionThrown = false;
try {
builderMap.put("C", 3);
@ -107,16 +105,16 @@ class MapBuilderTest {
@Test
void setData() {
Map myMap = new HashMap();
Map<String, Integer> myMap = new HashMap();
myMap.put("A", 1);
myMap.put("B", 2);
Map builderMap = new MapBuilder().setData(myMap).build();
Map<String, Integer> builderMap = new MapBuilder().setData(myMap).build();
Assert.assertEquals(myMap, builderMap);
}
@Test
void build() {
Map builderMap = new MapBuilder().build();
Map<String, Integer> builderMap = new MapBuilder().build();
Assert.assertTrue(builderMap.size() == 0);
}
}