BioCodec implements SequenceCodec { @Override public List<SequenceItem> encode(List<SequenceItem> aDecoded, int aLength) { List<SequenceItem> encoded = new ArrayList<>(); int idx = offset; Iterator<SequenceItem> i = aDecoded.iterator(); while (i.hasNext()) { SequenceItem current = i.next(); if (idx > current.getBegin()) { throw new IllegalStateException("Illegal sequence item span: " + current); } if (current.getBegin() > current.getEnd()) { throw new IllegalStateException("Illegal sequence item span: " + current); } while (idx < current.getBegin()) { encoded.add(new SequenceItem(idx, idx, markOut)); idx++; } encoded.add(new SequenceItem(idx, idx, markBegin + current.getLabel())); idx++; while (idx <= current.getEnd()) { encoded.add(new SequenceItem(idx, idx, markIn + current.getLabel())); idx++; } } while (idx < aLength + offset) { encoded.add(new SequenceItem(idx, idx, markOut)); idx++; } return encoded; } BioCodec();  BioCodec(int aOffset); @Override List<SequenceItem> decode(List<SequenceItem> aEncoded); @Override List<SequenceItem> encode(List<SequenceItem> aDecoded, int aLength);  }
@Test public void testEncodeSingleUnitSingleItem() { List<SequenceItem> decoded = asList(new SequenceItem(0 + offset, 0 + offset, "PER")); List<SequenceItem> encoded = sut.encode(decoded, 1); assertThat(encoded).containsExactly(new SequenceItem(0 + offset, 0 + offset, "B-PER")); }
@Test public void testEncodeMultipleUnitsSingleItem() { List<SequenceItem> decoded = asList(new SequenceItem(0 + offset, 1 + offset, "PER")); List<SequenceItem> encoded = sut.encode(decoded, 2); assertThat(encoded).containsExactly( new SequenceItem(0 + offset, 0 + offset, "B-PER"), new SequenceItem(1 + offset, 1 + offset, "I-PER")); }
@Test public void testEncodeMultipleItems() { List<SequenceItem> decoded = asList( new SequenceItem(0 + offset, 0 + offset, "PER"), new SequenceItem(1 + offset, 1 + offset, "ORG")); List<SequenceItem> encoded = sut.encode(decoded, 2); assertThat(encoded).containsExactly( new SequenceItem(0 + offset, 0 + offset, "B-PER"), new SequenceItem(1 + offset, 1 + offset, "B-ORG")); }
@Test public void testEncodeMultipleItemsWithGap() { List<SequenceItem> decoded = asList( new SequenceItem(0 + offset, 0 + offset, "PER"), new SequenceItem(2 + offset, 2 + offset, "ORG")); List<SequenceItem> encoded = sut.encode(decoded, 3); assertThat(encoded).containsExactly( new SequenceItem(0 + offset, 0 + offset, "B-PER"), new SequenceItem(1 + offset, 1 + offset, "O"), new SequenceItem(2 + offset, 2 + offset, "B-ORG")); }
@Test public void testEncodeBadItemSpan() { List<SequenceItem> encoded = asList(new SequenceItem(2, 1, "PER")); assertThatExceptionOfType(IllegalStateException.class) .isThrownBy(() -> sut.encode(encoded, 2)) .withMessageContaining("Illegal sequence item span"); }
@Test public void testEncodeBadItemOrder() { List<SequenceItem> encoded = asList( new SequenceItem(1 + offset, 1 + offset, "PER"), new SequenceItem(0 + offset, 0 + offset, "ORG")); assertThatExceptionOfType(IllegalStateException.class) .isThrownBy(() -> sut.encode(encoded, 2)) .withMessageContaining("Illegal sequence item span"); }