@Test public void slice() throws Exception { BufferedLogger log = new BufferedLogger(100); for (int i = 0; i < 10; i++) { log.log("line " + i); } log.flush(); List<BufferedLogger.Entry> all = log.getCopy(); List<BufferedLogger.Entry> entries = log.slice(all.get(5).id, 3); String result = str(entries); assertEquals("line 6,line 7,line 8", result); result = str(log.slice(all.get(5).id, -3)); assertEquals("line 2,line 3,line 4", result); result = str(log.slice(all.get(2).id, -3)); assertEquals("line 0,line 1", result); result = str(log.slice(all.get(8).id, +3)); assertEquals("line 9", result); result = str(log.slice(all.get(0).id, 20)); assertEquals("line 1,line 2,line 3,line 4,line 5,line 6,line 7,line 8,line 9", result); result = str(log.slice(all.get(9).id, -20)); assertEquals("line 0,line 1,line 2,line 3,line 4,line 5,line 6,line 7,line 8", result); result = str(log.slice(all.get(0).id-1, 20)); assertEquals("line 0,line 1,line 2,line 3,line 4,line 5,line 6,line 7,line 8,line 9", result); }
public List<Entry> slice(long id, int maxEntries) { List<Entry> copy = getCopy(); Entry start = new Entry(id); int fromIndex = Collections.binarySearch(copy, start); if (maxEntries > 0) fromIndex++; if (fromIndex < 0) fromIndex = 0; int toIndex = fromIndex + maxEntries; if (toIndex < fromIndex) { int t = fromIndex; fromIndex = toIndex; toIndex = t; } int length = copy.size(); if (fromIndex < 0) fromIndex = 0; if (toIndex >= length) toIndex = length; if (fromIndex >= length || toIndex < 1 || fromIndex == toIndex) return Collections.emptyList(); return copy.subList(fromIndex, toIndex); }
BufferedLogger implements AutoCloseable { public List<Entry> slice(long id, int maxEntries) { List<Entry> copy = getCopy(); Entry start = new Entry(id); int fromIndex = Collections.binarySearch(copy, start); if (maxEntries > 0) fromIndex++; if (fromIndex < 0) fromIndex = 0; int toIndex = fromIndex + maxEntries; if (toIndex < fromIndex) { int t = fromIndex; fromIndex = toIndex; toIndex = t; } int length = copy.size(); if (fromIndex < 0) fromIndex = 0; if (toIndex >= length) toIndex = length; if (fromIndex >= length || toIndex < 1 || fromIndex == toIndex) return Collections.emptyList(); return copy.subList(fromIndex, toIndex); } }
BufferedLogger implements AutoCloseable { public List<Entry> slice(long id, int maxEntries) { List<Entry> copy = getCopy(); Entry start = new Entry(id); int fromIndex = Collections.binarySearch(copy, start); if (maxEntries > 0) fromIndex++; if (fromIndex < 0) fromIndex = 0; int toIndex = fromIndex + maxEntries; if (toIndex < fromIndex) { int t = fromIndex; fromIndex = toIndex; toIndex = t; } int length = copy.size(); if (fromIndex < 0) fromIndex = 0; if (toIndex >= length) toIndex = length; if (fromIndex >= length || toIndex < 1 || fromIndex == toIndex) return Collections.emptyList(); return copy.subList(fromIndex, toIndex); } BufferedLogger(int maxEntries); }
BufferedLogger implements AutoCloseable { public List<Entry> slice(long id, int maxEntries) { List<Entry> copy = getCopy(); Entry start = new Entry(id); int fromIndex = Collections.binarySearch(copy, start); if (maxEntries > 0) fromIndex++; if (fromIndex < 0) fromIndex = 0; int toIndex = fromIndex + maxEntries; if (toIndex < fromIndex) { int t = fromIndex; fromIndex = toIndex; toIndex = t; } int length = copy.size(); if (fromIndex < 0) fromIndex = 0; if (toIndex >= length) toIndex = length; if (fromIndex >= length || toIndex < 1 || fromIndex == toIndex) return Collections.emptyList(); return copy.subList(fromIndex, toIndex); } BufferedLogger(int maxEntries); void e(String s); void flush(); boolean flush(long millis); PrintStream printTo(PrintStream ps, boolean printTimestamp); @NonNull Entry log(String message); @NonNull Entry d(String message); @NonNull List<Entry> getLast(int maxEntries); List<Entry> slice(long id, int maxEntries); void clear(); List<Entry> getCopy(); void interceptStdOut(); @Override void close(); void stopInterceptingStdOut(); }
BufferedLogger implements AutoCloseable { public List<Entry> slice(long id, int maxEntries) { List<Entry> copy = getCopy(); Entry start = new Entry(id); int fromIndex = Collections.binarySearch(copy, start); if (maxEntries > 0) fromIndex++; if (fromIndex < 0) fromIndex = 0; int toIndex = fromIndex + maxEntries; if (toIndex < fromIndex) { int t = fromIndex; fromIndex = toIndex; toIndex = t; } int length = copy.size(); if (fromIndex < 0) fromIndex = 0; if (toIndex >= length) toIndex = length; if (fromIndex >= length || toIndex < 1 || fromIndex == toIndex) return Collections.emptyList(); return copy.subList(fromIndex, toIndex); } BufferedLogger(int maxEntries); void e(String s); void flush(); boolean flush(long millis); PrintStream printTo(PrintStream ps, boolean printTimestamp); @NonNull Entry log(String message); @NonNull Entry d(String message); @NonNull List<Entry> getLast(int maxEntries); List<Entry> slice(long id, int maxEntries); void clear(); List<Entry> getCopy(); void interceptStdOut(); @Override void close(); void stopInterceptingStdOut();  }