Created
July 13, 2016 19:27
-
-
Save fixxer/66a4a483146953c7d94bade587b0f6b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 rx.Observable; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import static java.util.Arrays.asList; | |
public class ParseWithRx { | |
public static void main(String[] args) { | |
String[][] sourceData = { | |
{"Master1", "Master2", "Master3"}, | |
{"A1", "B1", "C1", "D1"}, | |
{"A2", "B2", "C2", "D2"}, | |
}; | |
Observable.from(asList(sourceData)).publish(rows -> { | |
Observable<String[]> master = rows.take(1); | |
Observable<String[]> detail = rows.skip(1); | |
return detail | |
.map(row -> new Detail(row[0], row[1], row[2], row[3])) | |
.collect(() -> new ArrayList<Detail>(), (acc, row) -> { acc.add(row); }) | |
.zipWith(master, (details, row) -> new MasterDetail(row[0], row[1], row[2], details)); | |
}).subscribe(System.out::println); | |
/* Output: | |
MasterDetail{ | |
master1='Master1', | |
master2='Master2', | |
master3='Master3', | |
details=[ | |
Detail{a='A1', b='B1', c='C1', d='D1'}, | |
Detail{a='A2', b='B2', c='C2', d='D2'} | |
]} | |
*/ | |
} | |
private static class MasterDetail { | |
private String master1; | |
private String master2; | |
private String master3; | |
private Collection<Detail> details; | |
public MasterDetail(String master1, String master2, String master3, Collection<Detail> details) { | |
this.master1 = master1; | |
this.master2 = master2; | |
this.master3 = master3; | |
this.details = details; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
MasterDetail that = (MasterDetail) o; | |
if (master1 != null ? !master1.equals(that.master1) : that.master1 != null) return false; | |
if (master2 != null ? !master2.equals(that.master2) : that.master2 != null) return false; | |
if (master3 != null ? !master3.equals(that.master3) : that.master3 != null) return false; | |
return details != null ? details.equals(that.details) : that.details == null; | |
} | |
@Override | |
public int hashCode() { | |
int result = master1 != null ? master1.hashCode() : 0; | |
result = 31 * result + (master2 != null ? master2.hashCode() : 0); | |
result = 31 * result + (master3 != null ? master3.hashCode() : 0); | |
result = 31 * result + (details != null ? details.hashCode() : 0); | |
return result; | |
} | |
@Override | |
public String toString() { | |
return "MasterDetail{" + | |
"master1='" + master1 + '\'' + | |
", master2='" + master2 + '\'' + | |
", master3='" + master3 + '\'' + | |
", details=" + details + | |
'}'; | |
} | |
} | |
private static class Detail { | |
private String a; | |
private String b; | |
private String c; | |
private String d; | |
public Detail(String a, String b, String c, String d) { | |
this.a = a; | |
this.b = b; | |
this.c = c; | |
this.d = d; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Detail detail = (Detail) o; | |
if (a != null ? !a.equals(detail.a) : detail.a != null) return false; | |
if (b != null ? !b.equals(detail.b) : detail.b != null) return false; | |
if (c != null ? !c.equals(detail.c) : detail.c != null) return false; | |
return d != null ? d.equals(detail.d) : detail.d == null; | |
} | |
@Override | |
public int hashCode() { | |
int result = a != null ? a.hashCode() : 0; | |
result = 31 * result + (b != null ? b.hashCode() : 0); | |
result = 31 * result + (c != null ? c.hashCode() : 0); | |
result = 31 * result + (d != null ? d.hashCode() : 0); | |
return result; | |
} | |
@Override | |
public String toString() { | |
return "Detail{" + | |
"a='" + a + '\'' + | |
", b='" + b + '\'' + | |
", c='" + c + '\'' + | |
", d='" + d + '\'' + | |
'}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment