Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Family authored and Family committed Oct 3, 2020
1 parent a7cb0ad commit 0756ad6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
15 changes: 7 additions & 8 deletions src/main/java/org/apache/commons/collections4/MapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
* 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;
Expand All @@ -44,7 +43,7 @@ public MapBuilder setComparator(Comparator comparator) {
}

/*
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;
Expand Down Expand Up @@ -79,7 +78,7 @@ public MapBuilder setData(Map data) {
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:
Expand Down Expand Up @@ -110,6 +109,6 @@ public Map build() {

enum KeyOrder
{
RANDOM, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER;
UNORDERED, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER;
}
}
22 changes: 10 additions & 12 deletions src/test/java/org/apache/commons/collections4/MapBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

/**
* Test Cases for Map Builder
*/
Expand All @@ -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
Expand All @@ -35,13 +33,13 @@ void setComparator() {

@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
Expand Down Expand Up @@ -91,10 +89,10 @@ void setIterationOrder() {

@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);
Expand All @@ -107,16 +105,16 @@ void setImmutable() {

@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);
}
}

0 comments on commit 0756ad6

Please sign in to comment.