/** * Constructor. * * @param name the name of this annotation. * @param value the value of this annotation. * @param range the range of this annotation. */ public Annotation(String name, String value, Range range) { this.name = name; this.value = value; this.range = new Range(range.getStart(), range.getEnd()); }
/** * Shifts this annotation by {@code shiftAmount} if it is on a range that * is after or covers the given position. * * @param position the anchor position. * @param shiftAmount the amount to shift the annotation range. */ public void shift(int position, int shiftAmount) { int start = range.getStart(); if (start >= position) { start += shiftAmount; } int end = range.getEnd(); if (end >= position) { end += shiftAmount; } range = new Range(start, end); }
/** * Returns the first hit. * * @return an instance of {@link BlipContent}, that represents the first hit. */ public BlipContent value() { BlipContent result = null; if (iterator.hasNext()) { Range range = iterator.next(); if (range.getEnd() - range.getStart() == 1 && blip.getElements().containsKey(range.getStart())) { result = blip.getElements().get(range.getStart()); } else { result = Plaintext.of(blip.getContent().substring(range.getStart(), range.getEnd())); } } iterator.reset(); return result; }
public void testTextIteratorShiftInsertAfter() { Blip blip = new Blip("blip1", "\nfoofoofoo", null, null, wavelet); TextIterator iterator = new BlipIterator.TextIterator(blip, "foo", -1); for (int i = 0; i < 3; ++i) { assertTrue(iterator.hasNext()); Range range = iterator.next(); assertEquals(i * 6 + 1, range.getStart()); blip.setContent(blip.getContent().substring(0, range.getEnd()) + "foo" + blip.getContent().substring(range.getEnd())); iterator.shift(range.getEnd() - range.getStart() + 2); } assertFalse(iterator.hasNext()); assertEquals("\nfoofoofoofoofoofoo", blip.getContent()); }
public void testTextIteratorShiftInsertAfter() { Blip blip = new Blip("blip1", "\nfoofoofoo", null, null, wavelet); TextIterator iterator = new BlipIterator.TextIterator(blip, "foo", -1); for (int i = 0; i < 3; ++i) { assertTrue(iterator.hasNext()); Range range = iterator.next(); assertEquals(i * 6 + 1, range.getStart()); blip.setContent(blip.getContent().substring(0, range.getEnd()) + "foo" + blip.getContent().substring(range.getEnd())); iterator.shift(range.getEnd() - range.getStart() + 2); } assertFalse(iterator.hasNext()); assertEquals("\nfoofoofoofoofoofoo", blip.getContent()); }
/** * Pretend we're shifting in the manner of INSERT_AFTER */ public void testTextMatcherShiftInsertAfter() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 3 + 1, r.getStart()); apiView.insert(r.getEnd(), "1"); it.shift(r.getEnd(), 1); } assertNull(it.next()); }
/** * Pretend we're shifting in the manner of INSERT_AFTER */ public void testTextMatcherShiftInsertAfter() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 3 + 1, r.getStart()); apiView.insert(r.getEnd(), "1"); it.shift(r.getEnd(), 1); } assertNull(it.next()); }
/** * Copies the content of the source blip into the target blip. * * @param fromBlip the source blip. * @param toBlip the target blip. */ public static void copyBlipContents(Blip fromBlip, Blip toBlip) { for (BlipContent blipContent: fromBlip.all().values()) { toBlip.append(blipContent); } for (Annotation annotation : fromBlip.getAnnotations()) { Range range = annotation.getRange(); toBlip.range(range.getStart() + 1, range.getEnd() + 1).annotate(annotation.getName(), annotation.getValue()); } }
public void testTextMatcherWithoutShift() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 2 + 1, r.getStart()); assertEquals(i * 2 + 2, r.getEnd()); } assertNull(it.next()); }
public void testTextMatcherWithoutShift() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 2 + 1, r.getStart()); assertEquals(i * 2 + 2, r.getEnd()); } assertNull(it.next()); }
public void testShift() throws Exception { Annotation annotation = new Annotation("key", "value", 5 , 10); annotation.shift(4, 2); assertEquals(7, annotation.getRange().getStart()); assertEquals(12, annotation.getRange().getEnd()); annotation = new Annotation("key", "value", 5 , 10); annotation.shift(5, 3); assertEquals(8, annotation.getRange().getStart()); assertEquals(13, annotation.getRange().getEnd()); annotation = new Annotation("key", "value", 5 , 10); annotation.shift(9, 2); assertEquals(5, annotation.getRange().getStart()); assertEquals(12, annotation.getRange().getEnd()); annotation = new Annotation("key", "value", 5 , 10); annotation.shift(10, 2); assertEquals(5, annotation.getRange().getStart()); assertEquals(12, annotation.getRange().getEnd()); } }
public void testShift() throws Exception { Annotation annotation = new Annotation("key", "value", 5 , 10); annotation.shift(4, 2); assertEquals(7, annotation.getRange().getStart()); assertEquals(12, annotation.getRange().getEnd()); annotation = new Annotation("key", "value", 5 , 10); annotation.shift(5, 3); assertEquals(8, annotation.getRange().getStart()); assertEquals(13, annotation.getRange().getEnd()); annotation = new Annotation("key", "value", 5 , 10); annotation.shift(9, 2); assertEquals(5, annotation.getRange().getStart()); assertEquals(12, annotation.getRange().getEnd()); annotation = new Annotation("key", "value", 5 , 10); annotation.shift(10, 2); assertEquals(5, annotation.getRange().getStart()); assertEquals(12, annotation.getRange().getEnd()); } }
public void testTextIteratorDeletingMatches() { Blip blip = new Blip("blip1", "\n1 1 1", null, null, wavelet); TextIterator iterator = new BlipIterator.TextIterator(blip, "1", -1); for (int i = 0; i < 3; ++i) { assertTrue(iterator.hasNext()); Range range = iterator.next(); assertEquals(i + 1, range.getStart()); blip.setContent(blip.getContent().substring(0, range.getStart()) + blip.getContent().substring(range.getEnd())); iterator.shift(-1); } assertFalse(iterator.hasNext()); assertEquals("\n ", blip.getContent()); }
public void testTextIteratorDeletingMatches() { Blip blip = new Blip("blip1", "\n1 1 1", null, null, wavelet); TextIterator iterator = new BlipIterator.TextIterator(blip, "1", -1); for (int i = 0; i < 3; ++i) { assertTrue(iterator.hasNext()); Range range = iterator.next(); assertEquals(i + 1, range.getStart()); blip.setContent(blip.getContent().substring(0, range.getStart()) + blip.getContent().substring(range.getEnd())); iterator.shift(-1); } assertFalse(iterator.hasNext()); assertEquals("\n ", blip.getContent()); }
public void testSingleshotIterator() throws Exception { Blip blip = new Blip("blip1", "\n1 1 1", null, null, wavelet); SingleshotIterator iterator = new BlipIterator.SingleshotIterator(blip, 0, 1); assertTrue(iterator.hasNext()); Range range = iterator.next(); assertEquals(0, range.getStart()); assertEquals(1, range.getEnd()); assertFalse(iterator.hasNext()); }
public void testSingleshotIterator() throws Exception { Blip blip = new Blip("blip1", "\n1 1 1", null, null, wavelet); SingleshotIterator iterator = new BlipIterator.SingleshotIterator(blip, 0, 1); assertTrue(iterator.hasNext()); Range range = iterator.next(); assertEquals(0, range.getStart()); assertEquals(1, range.getEnd()); assertFalse(iterator.hasNext()); }
public void testTextIteratorWithoutShift() { Blip blip = new Blip("blip1", "\n1 1 1", null, null, wavelet); TextIterator iterator = new BlipIterator.TextIterator(blip, "1", -1); for (int i = 0; i < 3; ++i) { assertTrue(iterator.hasNext()); Range range = iterator.next(); assertNotNull(range); assertEquals(i * 2 + 1, range.getStart()); assertEquals(i * 2 + 2, range.getEnd()); } assertFalse(iterator.hasNext()); }
public void testTextIteratorWithoutShift() { Blip blip = new Blip("blip1", "\n1 1 1", null, null, wavelet); TextIterator iterator = new BlipIterator.TextIterator(blip, "1", -1); for (int i = 0; i < 3; ++i) { assertTrue(iterator.hasNext()); Range range = iterator.next(); assertNotNull(range); assertEquals(i * 2 + 1, range.getStart()); assertEquals(i * 2 + 2, range.getEnd()); } assertFalse(iterator.hasNext()); }