Last active
January 4, 2016 04:07
-
-
Save josefbetancourt/8400a7732a6d8a8618db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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