AdjacentLabelCodec implements SequenceCodec { @Override public List<SequenceItem> decode(List<SequenceItem> aEncoded) { List<SequenceItem> decoded = new ArrayList<>(); Optional<SequenceItem> starter = Optional.empty(); Optional<SequenceItem> previous = Optional.empty(); Iterator<SequenceItem> i = aEncoded.iterator(); while (i.hasNext()) { SequenceItem current = i.next(); if (previous.isPresent()) { SequenceItem prev = previous.get(); if (current.getBegin() < prev.getEnd() || prev.getEnd() > current.getEnd()) { throw new IllegalStateException( "Illegal sequence item span " + current + " following " + prev); } } if (current.getBegin() > current.getEnd()) { throw new IllegalStateException("Illegal sequence item span: " + current); } if (current.getLabel().equals(markOut)) { if (starter.isPresent()) { assert previous.isPresent(); decoded.add(new SequenceItem(starter.get().getBegin(), previous.get().getEnd(), starter.get().getLabel())); } starter = Optional.empty(); } else if (starter.isPresent()) { assert previous.isPresent(); if (starter.get().getLabel().equals(current.getLabel())) { } else { decoded.add(new SequenceItem(starter.get().getBegin(), previous.get().getEnd(), starter.get().getLabel())); starter = Optional.of(current); } } else { starter = Optional.of(current); } previous = Optional.of(current); } if (starter.isPresent()) { decoded.add(new SequenceItem(starter.get().getBegin(), previous.get().getEnd(), starter.get().getLabel())); } return decoded; } AdjacentLabelCodec();  AdjacentLabelCodec(int aOffset); @Override List<SequenceItem> decode(List<SequenceItem> aEncoded); @Override List<SequenceItem> encode(List<SequenceItem> aDecoded, int aLength);  }
@Test public void testDecodeEmpty() { List<SequenceItem> encoded = SequenceItem.of(offset); List<SequenceItem> decoded = sut.decode(encoded); assertThat(decoded).containsExactly(); }
@Test public void testDecodeSingleValidItem() { List<SequenceItem> encoded = SequenceItem.of(offset, "PER"); List<SequenceItem> decoded = sut.decode(encoded); assertThat(decoded).containsExactly(new SequenceItem(0 + offset, 0 + offset, "PER")); }
@Test public void testDecodeMultiUnitSpan() { List<SequenceItem> encoded = SequenceItem.of(offset, "O", "PER", "PER", "O"); List<SequenceItem> decoded = sut.decode(encoded); assertThat(decoded).containsExactly(new SequenceItem(1 + offset, 2 + offset, "PER")); }
@Test public void testDecodeTwoAdjacentUnitsWithDifferentLabels() { List<SequenceItem> encoded = SequenceItem.of(offset, "O", "PER", "ORG", "O"); List<SequenceItem> decoded = sut.decode(encoded); assertThat(decoded).containsExactly( new SequenceItem(1 + offset, 1 + offset, "PER"), new SequenceItem(2 + offset, 2 + offset, "ORG")); }
@Test public void testDecodeEndSmallerThanBegin() { List<SequenceItem> encoded = asList(new SequenceItem(1 + offset, 0 + offset, "O")); assertThatExceptionOfType(IllegalStateException.class) .isThrownBy(() -> sut.decode(encoded)) .withMessageContaining("Illegal sequence item span"); }
@Test public void testDecodeBadItemOrder() { List<SequenceItem> encoded = asList( new SequenceItem(1 + offset, 1 + offset, "O"), new SequenceItem(0 + offset, 0 + offset, "O")); assertThatExceptionOfType(IllegalStateException.class) .isThrownBy(() -> sut.decode(encoded)) .withMessageContaining("Illegal sequence item span"); }