Created
June 2, 2017 14:53
-
-
Save EugeneShapovalov94/07b51e58b9da73ada14eca965bd3dc09 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
import com.codelegs.ao.clean_architecture.presentation.vault.entity.VaultFile; | |
import com.codelegs.ao.utils.mimetype.MimeType; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import java.util.Comparator; | |
import java.util.List; | |
import io.reactivex.Observable; | |
import io.reactivex.ObservableSource; | |
import io.reactivex.annotations.NonNull; | |
import io.reactivex.functions.Consumer; | |
import io.reactivex.functions.Function; | |
import io.reactivex.observables.GroupedObservable; | |
@RunWith(MockitoJUnitRunner.class) | |
public class GroupByTest { | |
@Test | |
public void groupBy() { | |
VaultFile f1 = new VaultFile("1", "F", new MimeType("C", 1, 1)); | |
f1.setName("C"); | |
VaultFile f2 = new VaultFile("1", "D", new MimeType("B", 1, 1)); | |
f2.setName("B"); | |
VaultFile f3 = new VaultFile("1", "F", new MimeType("Z", 1, 1)); | |
f3.setName("Z"); | |
VaultFile f4 = new VaultFile("1", "D", new MimeType("A", 1, 1)); | |
f4.setName("A"); | |
VaultFile f5 = new VaultFile("1", "F", new MimeType("T", 1, 1)); | |
f5.setName("T"); | |
VaultFile f6 = new VaultFile("1", "D", new MimeType("U", 1, 1)); | |
f6.setName("U"); | |
VaultFile f8 = new VaultFile("1", "D", new MimeType("P", 1, 1)); | |
f8.setName("P"); | |
VaultFile f9 = new VaultFile("1", "D", new MimeType("Q", 1, 1)); | |
f9.setName("Q"); | |
VaultFile f10 = new VaultFile("1", "F", new MimeType("F", 1, 1)); | |
f10.setName("F"); | |
VaultFile f11 = new VaultFile("1", "D", new MimeType("M", 1, 1)); | |
f11.setName("M"); | |
//TestObserver<VaultFile> observer = new TestObserver<>(); | |
Observable.fromArray(f1, f2, f3, f4, f5, f6, f8, f9, f10, f11) | |
.groupBy(new Function<VaultFile, Boolean>() { | |
@Override | |
public Boolean apply(@NonNull VaultFile file) throws Exception { | |
return file.isFile(); | |
} | |
}) | |
.flatMap(new Function<GroupedObservable<Boolean, VaultFile>, ObservableSource<List<VaultFile>>>() { | |
@Override | |
public ObservableSource<List<VaultFile>> apply(@NonNull GroupedObservable<Boolean, VaultFile> booleanVaultFileGroupedObservable) throws Exception { | |
return booleanVaultFileGroupedObservable.toSortedList(new FileNameComparator()).toObservable(); | |
} | |
}) | |
.subscribe(new Consumer<List<VaultFile>>() { | |
@Override | |
public void accept(@NonNull List<VaultFile> files) throws Exception { | |
for (VaultFile file : files) { | |
System.out.println(file.getName() + " - " + file.getType()); | |
} | |
} | |
}); | |
} | |
private static class FileNameComparator implements Comparator<VaultFile> { | |
FileNameComparator() { | |
} | |
@Override | |
public int compare(VaultFile o1, VaultFile o2) { | |
return o1.getName().compareTo(o2.getName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment