@Override protected RawAnnotationSet<Object> getNewSet(AnnotationSetListener<Object> listener) { return new SimpleAnnotationSet(listener); }
@Override public void begin() { reset(); notifications = new ArrayList<Notification>(); }
@Override public void skip(int skipSize) { if (skipSize > size() - currentLocation) { throw new OpCursorException("attempt to skip beyond end of document (cursor at " + currentLocation + ", size is " + size() + ", distance is " + skipSize + ")"); } traverse(skipTraverser, skipSize); currentLocation += skipSize; }
@Override protected RawAnnotationSet<Object> getNewSet() { SimpleAnnotationSet set = new SimpleAnnotationSet(null); // Just to make sure we are in fact testing the generic annotation cursor assertTrue(set.annotationCursor(0, 0, strs()) instanceof GenericAnnotationCursor); return set; }
@Override public int firstAnnotationChange(int start, int end, String key, Object fromValue) { Preconditions.checkPositionIndexes(start, end, size()); checkKeyNotNull(key); start = Math.max(0, start); end = Math.min(end, ranges.size()); OffsetPoint<Values> point = ranges.performActionAt(start, finder); OffsetList.Container<Values> container = point.getContainer(); int offset = point.getOffset(); int location = start; while (location < end) { if (!eq(getValue(container, key), fromValue)) { return location; } if (container == ranges.sentinel()) { break; } location += container.size() - offset; container = container.getNextContainer(); offset = 0; } return -1; }
@Override public Object getAnnotation(int location, String key) { Preconditions.checkElementIndex(location, size()); checkKeyNotNull(key); Values values = ranges.performActionAt(location, finder).getValue(); return values == null ? null : values.get(key); }
@Override public void endAnnotation(String key) { assert currentChanges.containsKey(key); maybeNoteChange(key); currentChanges.remove(key); }
@Override public Iterable<AnnotationInterval<Object>> annotationIntervals(int start, int end, ReadableStringSet keys) { if (keys == null) { if (start > 0 || end < size()) { throw new RuntimeException("not implemented"); } return new Iterable<AnnotationInterval<Object>>() { @Override public Iterator<AnnotationInterval<Object>> iterator() { return new RangesIterator(); } }; } return new GenericAnnotationIntervalIterable<Object>(this, start, end, keys); }
@Override public void insert(int insertSize) { // Semantics: inserts always push an annotation boundary. OffsetPoint<Values> p = ranges.performActionAt(currentLocation, finder); OffsetList.Container<Values> current = p.getContainer(); if (p.getOffset() > 0) { current = current.split(p.getOffset(), current.getValue()); } current.insertBefore(oldValues, insertSize); traverse(paintingTraverser, insertSize); currentLocation += insertSize; }
protected Map<ParticipantId, Set<Range<Integer>>> getAndPrintAnnotations(PluggableMutableDocument document, SimpleAnnotationSet annotationSet) { Map<ParticipantId, Set<Range<Integer>>> rangesPerParticipant = new HashMap<ParticipantId, Set<Range<Integer>>>(); Iterable<AnnotationInterval<Object>> intervals = annotationSet.annotationIntervals(0, document.size(), CollectionUtils.newStringSet(WaveletContributions.ANNOTATION_KEY)); intervals.forEach(interval -> { System.out.println("Interval ("+interval.start()+","+interval.end()+") "); final Range<Integer> r = Range.closed(interval.start(), interval.end()); interval.annotations().each(new ProcV<Object>() { @Override public void apply(String key, Object value) { if (value != null) { ParticipantId p = (ParticipantId) value; if (!rangesPerParticipant.containsKey(p)) { rangesPerParticipant.put(p, new HashSet<Range<Integer>>()); } Set<Range<Integer>> rangeSet = rangesPerParticipant.get(p); rangeSet.add(r); System.out.println("\t "+key+"="+value.toString()); } } }); }); return rangesPerParticipant; }
@Override protected RawAnnotationSet<Object> getNewSet() { SimpleAnnotationSet set = new SimpleAnnotationSet(null); // Just to make sure we are in fact testing the generic annotation cursor assertTrue(set.annotationCursor(0, 0, strs()) instanceof GenericAnnotationCursor); return set; }
@Override public int lastAnnotationChange(int start, int end, String key, Object fromValue) { Preconditions.checkPositionIndexes(start, end, size()); checkKeyNotNull(key); start = Math.max(0, start); end = Math.min(end, ranges.size()); OffsetPoint<Values> point = ranges.performActionAt(end, finder); OffsetList.Container<Values> container = point.getContainer(); int offset = point.getOffset(); if (offset == 0) { container = container.getPreviousContainer(); offset = container == ranges.sentinel() ? 0 : container.size(); } int location = end; while (location > start) { if (!eq(getValue(container, key), fromValue)) { return location; } if (container == null) { break; } location -= offset; container = container.getPreviousContainer(); offset = container == ranges.sentinel() ? 0 : container.size(); } return -1; }
@Override public void startAnnotation(String key, Object value) { maybeNoteChange(key); currentChanges.put(key, new Pair<Integer, Object>(currentLocation, value)); }
@Override protected RawAnnotationSet<Object> getNewSet(AnnotationSetListener<Object> listener) { return new SimpleAnnotationSet(listener); }
@Override public void delete(int deleteSize) { if (deleteSize > size() - currentLocation) { throw new OpCursorException("attempt to delete beyond end of document (cursor at " + currentLocation + ", size is " + size() + ", deleteSize is " + deleteSize + ")"); } traverse(deleteTraverser, deleteSize); }
@Override public void finish() { // For good measure reset(); for (Notification notification : notifications) { notification.exe(); } }
void testPerformance1(Random random) { RawAnnotationSet<Object> a = new SimpleAnnotationSet(null); RawAnnotationSet<Object> b = new AnnotationTree<Object>(new Object(), new Object(), null); String[] keys = new String[NUM_KEYS]; for (int i = 0; i < keys.length; i++) { keys[i] = "k" + i; } String[] values = new String[NUM_KEYS]; for (int i = 0; i < values.length; i++) { values[i] = "v" + i; } int maxLength = 0; RawAnnotationSet<Object> c = b; for (int i = 0; i < NUM_UPDATES; i++) { applyRandomOperation(null, c, keys, values, random); maxLength = Math.max(maxLength, c.size()); for (int j = 0; j < QUERIES_PER_UPDATE; j++) { performRandomQuery(c, keys, random); } } System.err.println("final length=" + c.size() + ", max length=" + maxLength); }
void testPerformance1(Random random) { RawAnnotationSet<Object> a = new SimpleAnnotationSet(null); RawAnnotationSet<Object> b = new AnnotationTree<Object>(new Object(), new Object(), null); String[] keys = new String[NUM_KEYS]; for (int i = 0; i < keys.length; i++) { keys[i] = "k" + i; } String[] values = new String[NUM_KEYS]; for (int i = 0; i < values.length; i++) { values[i] = "v" + i; } int maxLength = 0; RawAnnotationSet<Object> c = b; for (int i = 0; i < NUM_UPDATES; i++) { applyRandomOperation(null, c, keys, values, random); maxLength = Math.max(maxLength, c.size()); for (int j = 0; j < QUERIES_PER_UPDATE; j++) { performRandomQuery(c, keys, random); } } System.err.println("final length=" + c.size() + ", max length=" + maxLength); }