package com.octodecillion.learn.rxjava; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import rx.Observable; | |
/** | |
* | |
* @author jbetancourt | |
* | |
*/ | |
public class PruneDeletionTree { | |
/** A struct */ | |
private class State{ | |
public String deletedPath = ""; | |
public boolean isDeleting = false; | |
} | |
/** | |
* | |
* <p> | |
* @param list sorted list of Folders | |
* @return pruned list of those Folders | |
* @since Dec 18, 2015 | |
*/ | |
public List<Folder> pruneDeletedFoldersRx(List<Folder> list){ | |
final State state = new State(); | |
final List<Folder> prunedList = new ArrayList<>(); | |
Observable.from(list).filter(folder -> { | |
String path = folder.getPath(); | |
if (state.isDeleting) { | |
if (path.startsWith(state.deletedPath)) { | |
return false; | |
} else { | |
state.isDeleting = false; | |
state.deletedPath = ""; | |
} | |
} else { | |
if (folder.isDeleted()) { | |
state.isDeleting = true; | |
state.deletedPath = path; | |
} | |
} | |
return true; | |
}).subscribe(prunedList::add); | |
return prunedList; | |
} | |
/** | |
* | |
* <p> | |
* @param list list of sorted Folders | |
* @return pruned list of those Folders | |
* @since Dec 18, 2015 | |
*/ | |
public List<Folder> pruneDeletedFoldersJavaStream(List<Folder> list){ | |
final State state = new State(); | |
List<Folder> prunedList = new ArrayList<>(); | |
prunedList = list.stream().filter(folder -> { | |
String path = folder.getPath(); | |
if (state.isDeleting) { | |
if (path.startsWith(state.deletedPath)) { | |
return false; | |
} else { | |
state.isDeleting = false; | |
state.deletedPath = ""; | |
} | |
} else { | |
if (folder.isDeleted()) { | |
state.isDeleting = true; | |
state.deletedPath = path; | |
} | |
} | |
return true; | |
}).collect(Collectors.toList()); | |
return prunedList; | |
} | |
} |
package com.octodecillion.learn.rxjava; | |
import static org.junit.Assert.fail; | |
import java.util.List; | |
import org.junit.Test; | |
/** | |
* Test {@link PruneDeletionTree}. | |
* @author jbetancourt | |
* | |
*/ | |
public class PruneDeletionTreeTest { | |
private static final String EXPECTED_TREE_JSON = "/expectedTree.json"; | |
private static final String TEST_TREE_JSON = "/tree.json"; | |
/** | |
* Test method for {@link com.octodecillion.learn.rxjava.Iterative#pruneDeletedFolders(java.util.List)}. | |
*/ | |
@Test | |
public void testPruneDeletedFoldersRx() { | |
final List<Folder> expectedList = TestUtils.readData(EXPECTED_TREE_JSON); | |
final List<Folder> prunedList = new PruneDeletionTree().pruneDeletedFoldersRx(TestUtils.readData(TEST_TREE_JSON)); | |
boolean sizesMatch = prunedList.size() == expectedList.size(); | |
if(!sizesMatch && expectedList.stream().noneMatch(f-> prunedList.contains(f))){ | |
fail("Failed pruning list"); | |
} | |
} | |
/** | |
* Test method for {@link com.octodecillion.learn.rxjava.Iterative#pruneDeletedFolders(java.util.List)}. | |
*/ | |
@Test | |
public void testPruneDeletedFoldersJavaStream() { | |
final List<Folder> expectedList = TestUtils.readData(EXPECTED_TREE_JSON); | |
final List<Folder> prunedList = new PruneDeletionTree().pruneDeletedFoldersJavaStream(TestUtils.readData(TEST_TREE_JSON)); | |
boolean sizesMatch = prunedList.size() == expectedList.size(); | |
if(!sizesMatch && expectedList.stream().noneMatch(f-> prunedList.contains(f))){ | |
fail("Failed pruning list"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment