Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<groupId>io.appform.databuilderframework</groupId>
<version>1.1.1</version>
<version>1.1.2</version>
<modelVersion>4.0.0</modelVersion>

<artifactId>databuilderframework</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/appform/databuilderframework/model/DataSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import io.appform.databuilderframework.engine.DataSetAccessor;
import io.appform.databuilderframework.engine.Utils;
Expand Down Expand Up @@ -58,6 +59,14 @@ public <T extends Data> DataSet add(T data) {
return add(Utils.name(data.getClass()), data);
}

public Data remove(final String dataName) {
return safeWriteOp(() -> availableData.remove(dataName));
}

public Data remove(final Class<?> dataClass) {
return safeWriteOp(() -> availableData.remove(Utils.name(dataClass)));
}

public Map<String, Data> filter(final Collection<String> requiredKeys) {
if (null == requiredKeys || requiredKeys.isEmpty()) {
return Collections.emptyMap();
Expand Down Expand Up @@ -103,6 +112,10 @@ public DataSetAccessor accessor() {
return accessor(this);
}

public Set<String> keySet() {
return safeOp(() -> ImmutableSet.copyOf(availableData.keySet()));
}

private <T> T safeOp(Supplier<T> operation) {
val stamp = lock.readLock();
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.appform.databuilderframework.model;

import io.appform.databuilderframework.engine.Utils;
import junit.framework.TestCase;
import org.junit.Test;

import java.util.Set;

public class DataSetTest extends TestCase {

public class TestData extends Data {
protected TestData() {
super(Utils.name(TestData.class));
}
}

@Test
public void testKeyRemovalFromDataSetKeyExists() {
DataSet ds = new DataSet();
ds.add(new TestData());
assertNotNull(ds.get(Utils.name(TestData.class)));
Data removedData = ds.remove(Utils.name(TestData.class));
assertNotNull(removedData);
assertNull(ds.get(Utils.name(TestData.class)));
}

@Test
public void testKeyRemovalFromDataSetKeyDoesNotExist() {
DataSet ds = new DataSet();
assertNull(ds.get(Utils.name(TestData.class)));
assertNull(ds.remove(Utils.name(TestData.class)));
assertNull(ds.get(Utils.name(TestData.class)));
}

@Test
public void testKeyRemovalFromDataSetWithClassAsParameter() {
DataSet ds = new DataSet();
ds.add(new TestData());
assertNotNull(ds.get(Utils.name(TestData.class)));
Data removedData = ds.remove(TestData.class);
assertNotNull(removedData);
assertNull(ds.get(Utils.name(TestData.class)));
}

@Test
public void testKeySetFetch() {
DataSet ds = new DataSet();
ds.add(new TestData());
Set<String> dataKeys = ds.keySet();
assertNotNull(dataKeys);
assertEquals(1, dataKeys.size());
}

}