Skip to content

Commit

Permalink
Support mutating CConfiguration via Map
Browse files Browse the repository at this point in the history
  • Loading branch information
chtyim committed Sep 6, 2019
1 parent 9a61ff2 commit 120ab1e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import java.io.File;
import java.net.URI;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Utilities for {@link CConfiguration}.
Expand All @@ -38,14 +38,42 @@ public class CConfigurationUtil extends Configuration {
private CConfigurationUtil() { }

/**
* Returns an immutable {@link Map} that is backed by the given {@link CConfiguration}.
* Returns a {@link Map} that is backed by the given {@link CConfiguration}. Updates to the returned Map
* will be reflected in the {@link CConfiguration}.
*/
public static Map<String, String> asMap(CConfiguration cConf) {
return new AbstractMap<String, String>() {
@Override
public Set<Entry<String, String>> entrySet() {
return Collections.unmodifiableSet(
StreamSupport.stream(cConf.spliterator(), false).collect(Collectors.toSet()));
return new AbstractSet<Entry<String, String>>() {

@Override
public boolean add(Entry<String, String> entry) {
String oldValue = cConf.get(entry.getKey());
if (Objects.equals(oldValue, entry.getValue())) {
return false;
}
cConf.set(entry.getKey(), entry.getValue());
return true;
}

@Override
public Iterator<Entry<String, String>> iterator() {
return cConf.iterator();
}

@Override
public int size() {
return cConf.size();
}
};
}

@Override
public String put(String key, String value) {
String oldValue = cConf.get(key);
cConf.set(key, value);
return oldValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,35 @@ public void testIsEmpty() {
Assert.assertTrue(CConfigurationUtil.asMap(cConf).isEmpty());
}

@Test (expected = UnsupportedOperationException.class)
public void testDisallowPut() {
CConfigurationUtil.asMap(CConfiguration.create()).put("test.key", "test.value");
@Test
public void testPut() {
Map<String, String> map = CConfigurationUtil.asMap(CConfiguration.create());
Assert.assertNull(map.put("test.key", "test.value"));
Assert.assertEquals("test.value", map.get("test.key"));
}

@Test (expected = UnsupportedOperationException.class)
public void testDisallowPutAll() {
CConfigurationUtil.asMap(CConfiguration.create()).putAll(Collections.singletonMap("test.key", "test.value"));
@Test
public void testPutAll() {
Map<String, String> map = CConfigurationUtil.asMap(CConfiguration.create());
map.putAll(Collections.singletonMap("test.key", "test.value"));
Assert.assertEquals("test.value", map.get("test.key"));
}

@Test (expected = UnsupportedOperationException.class)
public void testDisallowRemove() {
CConfigurationUtil.asMap(CConfiguration.create()).remove(Constants.CFG_LOCAL_DATA_DIR);
@Test
public void testRemove() {
Map<String, String> map = CConfigurationUtil.asMap(CConfiguration.create());
String oldValue = map.get(Constants.CFG_LOCAL_DATA_DIR);
Assert.assertNotNull(oldValue);
String removed = map.remove(Constants.CFG_LOCAL_DATA_DIR);
Assert.assertEquals(oldValue, removed);
Assert.assertNull(map.get(Constants.CFG_LOCAL_DATA_DIR));
}

@Test (expected = UnsupportedOperationException.class)
public void testDisallowRemoveSet() {
CConfigurationUtil.asMap(CConfiguration.create()).entrySet().removeIf(e -> true);
@Test
public void testIteratorRemove() {
Map<String, String> map = CConfigurationUtil.asMap(CConfiguration.create());
Assert.assertNotNull(map.get(Constants.CFG_LOCAL_DATA_DIR));
map.entrySet().removeIf(e -> e.getKey().equals(Constants.CFG_LOCAL_DATA_DIR));
Assert.assertNull(map.get(Constants.CFG_LOCAL_DATA_DIR));
}
}

0 comments on commit 120ab1e

Please sign in to comment.