Added apache header and fixed checkstyle error
This commit is contained in:
parent
0756ad6c7f
commit
b5cc1de697
|
@ -1,8 +1,28 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.collections4;
|
||||
|
||||
import org.apache.commons.collections4.map.HashedMap;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Defines an Helper Builder that generates a {@code Map}
|
||||
|
@ -18,13 +38,13 @@ import java.util.*;
|
|||
* }</pre>
|
||||
*
|
||||
*/
|
||||
public class MapBuilder<K,V> {
|
||||
public class MapBuilder<K, V> {
|
||||
|
||||
private Comparator<? super K> comparator;
|
||||
private KeyOrder iterationOrder;
|
||||
private boolean synchronizedMap;
|
||||
private boolean immutable;
|
||||
private Map<K,V> data;
|
||||
private Map<K, V> data;
|
||||
|
||||
public MapBuilder() {
|
||||
comparator = null;
|
||||
|
@ -78,37 +98,36 @@ public class MapBuilder<K,V> {
|
|||
Builder Method which takes care of all the conditions and returns the required Map.
|
||||
*/
|
||||
public Map build() {
|
||||
Map<K,V> map;
|
||||
Map<K, V> 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;
|
||||
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)) {
|
||||
if (MapUtils.isNotEmpty(data)) {
|
||||
map.putAll(data);
|
||||
}
|
||||
|
||||
if(synchronizedMap) {
|
||||
if (synchronizedMap) {
|
||||
map = Collections.synchronizedMap(map);
|
||||
}
|
||||
|
||||
if(immutable) {
|
||||
if (immutable) {
|
||||
map = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
enum KeyOrder
|
||||
{
|
||||
enum KeyOrder {
|
||||
UNORDERED, NATURAL_ORDER, INSERTION_ORDER, COMPARATOR_ORDER;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,35 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.collections4;
|
||||
|
||||
import org.apache.commons.collections4.map.HashedMap;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* Test Cases for Map Builder
|
||||
*/
|
||||
class MapBuilderTest {
|
||||
public class MapBuilderTest {
|
||||
|
||||
@Test
|
||||
void setComparator() {
|
||||
|
@ -20,11 +39,9 @@ class MapBuilderTest {
|
|||
myMap.put("X", 24);
|
||||
myMap.put("B", 2);
|
||||
myMap.put("Y", 26);
|
||||
Map<String, Integer> 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();
|
||||
Map<String, Integer> 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");
|
||||
|
@ -40,7 +57,7 @@ class MapBuilderTest {
|
|||
myMap.put("B", 2);
|
||||
myMap.put("Y", 26);
|
||||
Map<String, Integer> builderMap = new MapBuilder().setData(myMap).setIterationOrder(MapBuilder.KeyOrder.UNORDERED).build();
|
||||
Assert.assertEquals(myMap, builderMap);
|
||||
Assert.assertTrue(builderMap instanceof HashedMap);
|
||||
|
||||
//Key Order = INSERTION ORDER
|
||||
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.INSERTION_ORDER).build();
|
||||
|
@ -48,10 +65,7 @@ class MapBuilderTest {
|
|||
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");
|
||||
Assert.assertTrue(builderMap instanceof LinkedHashMap);
|
||||
|
||||
//Key Order = NATURAL ORDER
|
||||
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.NATURAL_ORDER).build();
|
||||
|
@ -59,10 +73,7 @@ class MapBuilderTest {
|
|||
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");
|
||||
Assert.assertTrue(builderMap instanceof TreeMap);
|
||||
|
||||
//Key Order = COMPARATOR ORDER and null comparator
|
||||
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).build();
|
||||
|
@ -70,10 +81,7 @@ class MapBuilderTest {
|
|||
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");
|
||||
Assert.assertTrue(builderMap instanceof TreeMap);
|
||||
|
||||
//Key Order = COMPARATOR ORDER and valid comparator
|
||||
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).setComparator(Comparator.reverseOrder()).build();
|
||||
|
@ -81,10 +89,7 @@ class MapBuilderTest {
|
|||
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");
|
||||
Assert.assertTrue(builderMap instanceof TreeMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -100,7 +105,6 @@ class MapBuilderTest {
|
|||
exceptionThrown = true;
|
||||
}
|
||||
Assert.assertTrue(exceptionThrown);
|
||||
Assert.assertEquals(myMap, builderMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -117,4 +121,4 @@ class MapBuilderTest {
|
|||
Map<String, Integer> builderMap = new MapBuilder().build();
|
||||
Assert.assertTrue(builderMap.size() == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue