Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COLLECTIONS-767 MapBuilder class to decide among various types of Maps #188

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
115 changes: 115 additions & 0 deletions src/main/java/org/apache/commons/collections4/MapBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.apache.commons.collections4;

import org.apache.commons.collections4.map.HashedMap;

import java.util.*;

/**
* Defines an Helper Builder that generates a {@code Map}
* A Builder class to help decide which type of map to use based on simple requirements.
* Currently It takes care of only basic types of Maps and can be extended to different types of Maps available in the ecosystem.
*
* <pre>{@code
* Map builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.INSERTION_ORDER).build();
* builderMap.put("A", 1);
* builderMap.put("X", 24);
* builderMap.put("B", 2);
* builderMap.put("Y", 26);
* }</pre>
*
* @author Amita Pradhan
Amita-Pradhan marked this conversation as resolved.
Show resolved Hide resolved
*/
public class MapBuilder {

private Comparator comparator;
Amita-Pradhan marked this conversation as resolved.
Show resolved Hide resolved
private KeyOrder iterationOrder;
private boolean synchronizedMap;
private boolean immutable;
private Map data;
Amita-Pradhan marked this conversation as resolved.
Show resolved Hide resolved

public MapBuilder() {
Amita-Pradhan marked this conversation as resolved.
Show resolved Hide resolved
comparator = null;
iterationOrder = KeyOrder.RANDOM;
synchronizedMap = false;
immutable = false;
data = null;
}

/*
Sets the comparator to be used to decide the Iteration order in case of iterationOrder = COMPARATOR_ORDER;
*/
public MapBuilder setComparator(Comparator comparator) {
this.comparator = comparator;
return this;
}

/*
Sets the Iteration order to be used from [RANDOM, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER]
*/
public MapBuilder setIterationOrder(KeyOrder iterationOrder) {
this.iterationOrder = iterationOrder;
return this;
}

/*
Since most of the maps are not inherently thread safe , this option provides the option if the map has to be synchronised or not
*/
public MapBuilder setSynchronizedMap(boolean synchronizedMap) {
this.synchronizedMap = synchronizedMap;
return this;
}

/*
Option to create a immutable map from the provided data
*/
public MapBuilder setImmutable(boolean immutable) {
this.immutable = immutable;
return this;
}

/*
Populates the Map with some preexisting data. All the selected conditions will be automatically applied to the existing data
*/
public MapBuilder setData(Map data) {
this.data = data;
return this;
}

/*
Builder Method which takes care of all the conditions and returns the required Map.
*/
public Map build() {
Map map;
switch (iterationOrder) {
case NATURAL_ORDER :
case COMPARATOR_ORDER:
map = new TreeMap(comparator);
break;
case INSERTION_ORDER :
map = new LinkedHashMap();
break;
default:
map = new HashedMap();
break;
}

if(MapUtils.isNotEmpty(data)) {
map.putAll(data);
}

if(synchronizedMap) {
map = Collections.synchronizedMap(map);
}

if(immutable) {
map = Collections.unmodifiableMap(map);
}

return map;
}

enum KeyOrder
{
RANDOM, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER;
Amita-Pradhan marked this conversation as resolved.
Show resolved Hide resolved
}
}
122 changes: 122 additions & 0 deletions src/test/java/org/apache/commons/collections4/MapBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.apache.commons.collections4;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

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

/**
* Test Cases for Map Builder
*/
class MapBuilderTest {
Amita-Pradhan marked this conversation as resolved.
Show resolved Hide resolved

@Test
void setComparator() {
// Null Comparator
Map myMap = new HashMap();
Amita-Pradhan marked this conversation as resolved.
Show resolved Hide resolved
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();
Assert.assertEquals(myMap, builderMap);

// Reverse comparator
builderMap = new MapBuilder().setData(myMap).setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).setComparator(Comparator.reverseOrder()).build();
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "Y");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "A");
}

@Test
void setIterationOrder() {
//Key Order = RANDOM
Map 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();
Assert.assertEquals(myMap, builderMap);

//Key Order = INSERTION ORDER
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.INSERTION_ORDER).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "A");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "Y");

//Key Order = NATURAL ORDER
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.NATURAL_ORDER).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "A");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "Y");

//Key Order = COMPARATOR ORDER and null comparator
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "A");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "Y");

//Key Order = COMPARATOR ORDER and valid comparator
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).setComparator(Comparator.reverseOrder()).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "Y");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "A");
}

@Test
void setImmutable() {
Map myMap = new HashMap();
myMap.put("A", 1);
myMap.put("B", 2);
Map builderMap = new MapBuilder().setData(myMap).setImmutable(true).build();
boolean exceptionThrown = false;
try {
builderMap.put("C", 3);
}catch (UnsupportedOperationException e) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
Assert.assertEquals(myMap, builderMap);
}

@Test
void setData() {
Map myMap = new HashMap();
myMap.put("A", 1);
myMap.put("B", 2);
Map builderMap = new MapBuilder().setData(myMap).build();
Assert.assertEquals(myMap, builderMap);
}

@Test
void build() {
Map builderMap = new MapBuilder().build();
Assert.assertTrue(builderMap.size() == 0);
}
}