@Test public void mergeSequences() { List<TimeSeriesCollection> collection1 = Arrays.asList( new EmptyTimeSeriesCollection(new DateTime(0, DateTimeZone.UTC)), new EmptyTimeSeriesCollection(new DateTime(600, DateTimeZone.UTC))); List<TimeSeriesCollection> collection2 = Arrays.asList( new EmptyTimeSeriesCollection(new DateTime(300, DateTimeZone.UTC)), new EmptyTimeSeriesCollection(new DateTime(600, DateTimeZone.UTC))); ObjectSequence<TimeSeriesCollection> inputSeq1 = new ForwardSequence(0, collection1.size()).map(collection1::get, true, true, true); ObjectSequence<TimeSeriesCollection> inputSeq2 = new ForwardSequence(0, collection2.size()).map(collection2::get, true, true, true); assertThat(Util.mergeSequences(), instanceOf(EmptyObjectSequence.class)); assertSame(inputSeq1, Util.mergeSequences(inputSeq1)); ObjectSequence<TimeSeriesCollection> fixed = Util.mergeSequences(inputSeq1, inputSeq2); assertThat(fixed.toArray(new TimeSeriesCollection[0]), arrayContaining( new EmptyTimeSeriesCollection(new DateTime(0, DateTimeZone.UTC)), new EmptyTimeSeriesCollection(new DateTime(300, DateTimeZone.UTC)), new EmptyTimeSeriesCollection(new DateTime(600, DateTimeZone.UTC)))); }
public static ObjectSequence<TimeSeriesCollection> mergeSequences(ObjectSequence<TimeSeriesCollection>... tsSeq) { if (tsSeq.length == 0) return ObjectSequence.empty(); if (tsSeq.length == 1) return tsSeq[0]; final Queue<ObjectSequence<TimeSeriesCollection>> seq = new PriorityQueue<>(Comparator.comparing(ObjectSequence::first)); seq.addAll(Arrays.stream(tsSeq) .filter(s -> !s.isEmpty()) .collect(Collectors.toList())); if (seq.isEmpty()) return ObjectSequence.empty(); if (seq.size() == 1) return seq.element(); final List<ObjectSequence<TimeSeriesCollection>> output = new ArrayList<>(tsSeq.length); while (!seq.isEmpty()) { final ObjectSequence<TimeSeriesCollection> head = seq.remove(); if (seq.isEmpty()) { output.add(head); continue; } if (head.last().compareTo(seq.element().first()) < 0) { output.add(head); continue; } final List<ObjectSequence<TimeSeriesCollection>> toMerge = new ArrayList<>(); final int headProblemStart = head.equalRange(tsc -> tsc.compareTo(seq.element().first())).getBegin(); output.add(head.limit(headProblemStart)); toMerge.add(head.skip(headProblemStart)); final TimeSeriesCollection headLast = head.last(); while (!seq.isEmpty() && seq.element().first().compareTo(headLast) <= 0) { final ObjectSequence<TimeSeriesCollection> succ = seq.remove(); TimeSeriesCollection succFirst = succ.first(); System.err.println("succ.first: " + succ.first() + ", head.last: " + headLast); final int succProblemEnd = succ.equalRange(tsc -> tsc.compareTo(headLast)).getEnd(); assert succProblemEnd > 0; toMerge.add(succ.limit(succProblemEnd)); ObjectSequence<TimeSeriesCollection> succNoProblem = succ.skip(succProblemEnd); if (!succNoProblem.isEmpty()) seq.add(succNoProblem); } assert (toMerge.size() > 1); output.add(fixSequence(ObjectSequence.concat(toMerge, false, false))); } return ObjectSequence.concat(output, true, true); }
Util { public static ObjectSequence<TimeSeriesCollection> mergeSequences(ObjectSequence<TimeSeriesCollection>... tsSeq) { if (tsSeq.length == 0) return ObjectSequence.empty(); if (tsSeq.length == 1) return tsSeq[0]; final Queue<ObjectSequence<TimeSeriesCollection>> seq = new PriorityQueue<>(Comparator.comparing(ObjectSequence::first)); seq.addAll(Arrays.stream(tsSeq) .filter(s -> !s.isEmpty()) .collect(Collectors.toList())); if (seq.isEmpty()) return ObjectSequence.empty(); if (seq.size() == 1) return seq.element(); final List<ObjectSequence<TimeSeriesCollection>> output = new ArrayList<>(tsSeq.length); while (!seq.isEmpty()) { final ObjectSequence<TimeSeriesCollection> head = seq.remove(); if (seq.isEmpty()) { output.add(head); continue; } if (head.last().compareTo(seq.element().first()) < 0) { output.add(head); continue; } final List<ObjectSequence<TimeSeriesCollection>> toMerge = new ArrayList<>(); final int headProblemStart = head.equalRange(tsc -> tsc.compareTo(seq.element().first())).getBegin(); output.add(head.limit(headProblemStart)); toMerge.add(head.skip(headProblemStart)); final TimeSeriesCollection headLast = head.last(); while (!seq.isEmpty() && seq.element().first().compareTo(headLast) <= 0) { final ObjectSequence<TimeSeriesCollection> succ = seq.remove(); TimeSeriesCollection succFirst = succ.first(); System.err.println("succ.first: " + succ.first() + ", head.last: " + headLast); final int succProblemEnd = succ.equalRange(tsc -> tsc.compareTo(headLast)).getEnd(); assert succProblemEnd > 0; toMerge.add(succ.limit(succProblemEnd)); ObjectSequence<TimeSeriesCollection> succNoProblem = succ.skip(succProblemEnd); if (!succNoProblem.isEmpty()) seq.add(succNoProblem); } assert (toMerge.size() > 1); output.add(fixSequence(ObjectSequence.concat(toMerge, false, false))); } return ObjectSequence.concat(output, true, true); } }
Util { public static ObjectSequence<TimeSeriesCollection> mergeSequences(ObjectSequence<TimeSeriesCollection>... tsSeq) { if (tsSeq.length == 0) return ObjectSequence.empty(); if (tsSeq.length == 1) return tsSeq[0]; final Queue<ObjectSequence<TimeSeriesCollection>> seq = new PriorityQueue<>(Comparator.comparing(ObjectSequence::first)); seq.addAll(Arrays.stream(tsSeq) .filter(s -> !s.isEmpty()) .collect(Collectors.toList())); if (seq.isEmpty()) return ObjectSequence.empty(); if (seq.size() == 1) return seq.element(); final List<ObjectSequence<TimeSeriesCollection>> output = new ArrayList<>(tsSeq.length); while (!seq.isEmpty()) { final ObjectSequence<TimeSeriesCollection> head = seq.remove(); if (seq.isEmpty()) { output.add(head); continue; } if (head.last().compareTo(seq.element().first()) < 0) { output.add(head); continue; } final List<ObjectSequence<TimeSeriesCollection>> toMerge = new ArrayList<>(); final int headProblemStart = head.equalRange(tsc -> tsc.compareTo(seq.element().first())).getBegin(); output.add(head.limit(headProblemStart)); toMerge.add(head.skip(headProblemStart)); final TimeSeriesCollection headLast = head.last(); while (!seq.isEmpty() && seq.element().first().compareTo(headLast) <= 0) { final ObjectSequence<TimeSeriesCollection> succ = seq.remove(); TimeSeriesCollection succFirst = succ.first(); System.err.println("succ.first: " + succ.first() + ", head.last: " + headLast); final int succProblemEnd = succ.equalRange(tsc -> tsc.compareTo(headLast)).getEnd(); assert succProblemEnd > 0; toMerge.add(succ.limit(succProblemEnd)); ObjectSequence<TimeSeriesCollection> succNoProblem = succ.skip(succProblemEnd); if (!succNoProblem.isEmpty()) seq.add(succNoProblem); } assert (toMerge.size() > 1); output.add(fixSequence(ObjectSequence.concat(toMerge, false, false))); } return ObjectSequence.concat(output, true, true); }  }
Util { public static ObjectSequence<TimeSeriesCollection> mergeSequences(ObjectSequence<TimeSeriesCollection>... tsSeq) { if (tsSeq.length == 0) return ObjectSequence.empty(); if (tsSeq.length == 1) return tsSeq[0]; final Queue<ObjectSequence<TimeSeriesCollection>> seq = new PriorityQueue<>(Comparator.comparing(ObjectSequence::first)); seq.addAll(Arrays.stream(tsSeq) .filter(s -> !s.isEmpty()) .collect(Collectors.toList())); if (seq.isEmpty()) return ObjectSequence.empty(); if (seq.size() == 1) return seq.element(); final List<ObjectSequence<TimeSeriesCollection>> output = new ArrayList<>(tsSeq.length); while (!seq.isEmpty()) { final ObjectSequence<TimeSeriesCollection> head = seq.remove(); if (seq.isEmpty()) { output.add(head); continue; } if (head.last().compareTo(seq.element().first()) < 0) { output.add(head); continue; } final List<ObjectSequence<TimeSeriesCollection>> toMerge = new ArrayList<>(); final int headProblemStart = head.equalRange(tsc -> tsc.compareTo(seq.element().first())).getBegin(); output.add(head.limit(headProblemStart)); toMerge.add(head.skip(headProblemStart)); final TimeSeriesCollection headLast = head.last(); while (!seq.isEmpty() && seq.element().first().compareTo(headLast) <= 0) { final ObjectSequence<TimeSeriesCollection> succ = seq.remove(); TimeSeriesCollection succFirst = succ.first(); System.err.println("succ.first: " + succ.first() + ", head.last: " + headLast); final int succProblemEnd = succ.equalRange(tsc -> tsc.compareTo(headLast)).getEnd(); assert succProblemEnd > 0; toMerge.add(succ.limit(succProblemEnd)); ObjectSequence<TimeSeriesCollection> succNoProblem = succ.skip(succProblemEnd); if (!succNoProblem.isEmpty()) seq.add(succNoProblem); } assert (toMerge.size() > 1); output.add(fixSequence(ObjectSequence.concat(toMerge, false, false))); } return ObjectSequence.concat(output, true, true); }  static long segmentLength(long dataLength); static ObjectSequence<TimeSeriesCollection> fixSequence(ObjectSequence<TimeSeriesCollection> seq); static ObjectSequence<TimeSeriesCollection> mergeSequences(ObjectSequence<TimeSeriesCollection>... tsSeq); }
Util { public static ObjectSequence<TimeSeriesCollection> mergeSequences(ObjectSequence<TimeSeriesCollection>... tsSeq) { if (tsSeq.length == 0) return ObjectSequence.empty(); if (tsSeq.length == 1) return tsSeq[0]; final Queue<ObjectSequence<TimeSeriesCollection>> seq = new PriorityQueue<>(Comparator.comparing(ObjectSequence::first)); seq.addAll(Arrays.stream(tsSeq) .filter(s -> !s.isEmpty()) .collect(Collectors.toList())); if (seq.isEmpty()) return ObjectSequence.empty(); if (seq.size() == 1) return seq.element(); final List<ObjectSequence<TimeSeriesCollection>> output = new ArrayList<>(tsSeq.length); while (!seq.isEmpty()) { final ObjectSequence<TimeSeriesCollection> head = seq.remove(); if (seq.isEmpty()) { output.add(head); continue; } if (head.last().compareTo(seq.element().first()) < 0) { output.add(head); continue; } final List<ObjectSequence<TimeSeriesCollection>> toMerge = new ArrayList<>(); final int headProblemStart = head.equalRange(tsc -> tsc.compareTo(seq.element().first())).getBegin(); output.add(head.limit(headProblemStart)); toMerge.add(head.skip(headProblemStart)); final TimeSeriesCollection headLast = head.last(); while (!seq.isEmpty() && seq.element().first().compareTo(headLast) <= 0) { final ObjectSequence<TimeSeriesCollection> succ = seq.remove(); TimeSeriesCollection succFirst = succ.first(); System.err.println("succ.first: " + succ.first() + ", head.last: " + headLast); final int succProblemEnd = succ.equalRange(tsc -> tsc.compareTo(headLast)).getEnd(); assert succProblemEnd > 0; toMerge.add(succ.limit(succProblemEnd)); ObjectSequence<TimeSeriesCollection> succNoProblem = succ.skip(succProblemEnd); if (!succNoProblem.isEmpty()) seq.add(succNoProblem); } assert (toMerge.size() > 1); output.add(fixSequence(ObjectSequence.concat(toMerge, false, false))); } return ObjectSequence.concat(output, true, true); }  static long segmentLength(long dataLength); static ObjectSequence<TimeSeriesCollection> fixSequence(ObjectSequence<TimeSeriesCollection> seq); static ObjectSequence<TimeSeriesCollection> mergeSequences(ObjectSequence<TimeSeriesCollection>... tsSeq); static final int HDR_3_LEN; static final int ALL_HDR_CRC_LEN; }