From 7fb45fe50435142eaf3940e4e2d4ef7df906d640 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 6 Apr 2008 03:03:30 +0000 Subject: Simplified slice API. --- .../com/amazon/carbonado/cursor/SliceCursor.java | 200 ++++++++++----------- 1 file changed, 93 insertions(+), 107 deletions(-) (limited to 'src/main/java/com/amazon/carbonado') diff --git a/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java b/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java index 882932f..bf14339 100644 --- a/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java +++ b/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java @@ -33,52 +33,38 @@ import com.amazon.carbonado.FetchException; public abstract class SliceCursor extends AbstractCursor { /** * @param from zero-based element to start from, inclusive - * @param to zero-based element to end at, exclusive - * @throws IllegalArgumentException if source is null, from is negative or - * if from is more than to + * @throws IllegalArgumentException if source is null or from is negative */ - public static Cursor slice(Cursor source, long from, long to) { - return slice(source, (Long) from, (Long) to); + public static Cursor slice(Cursor source, long from) { + if (source == null) { + throw new IllegalArgumentException("Source is null"); + } + if (from >= 0) { + return from == 0 ? source : new Skip(source, from); + } else { + throw new IllegalArgumentException("Slice from is negative: " + from); + } } /** - * @param from optional zero-based element to start from, inclusive - * @param to optional zero-based element to end at, exclusive + * @param from zero-based element to start from, inclusive + * @param to zero-based element to end at, exclusive * @throws IllegalArgumentException if source is null, from is negative or * if from is more than to */ - public static Cursor slice(Cursor source, Long from, Long to) { - if (source == null) { - throw new IllegalArgumentException("Source is null"); + public static Cursor slice(Cursor source, long from, long to) { + source = slice(source, from); + long remaining = to - from; + if (remaining < 0) { + throw new IllegalArgumentException("Slice from is more than to: " + from + " > " + to); } - - long actualFrom; - - if (from == null) { - actualFrom = 0; - } else if (from >= 0) { - if ((actualFrom = from) > 0) { - source = new Skip(source, actualFrom); - } - } else { - throw new IllegalArgumentException("Slice from is negative: " + from); - } - - long remaining; - - if (to == null) { - return source; - } else if ((remaining = to - actualFrom) < 0) { - throw new IllegalArgumentException("Slice from is more than to: " + from + " > " + to); - } - - return new Limit(source, remaining); + return new Limit(source, remaining); } final Cursor mSource; SliceCursor(Cursor source) { - mSource = source; + mSource = source; } public void close() throws FetchException { @@ -86,82 +72,82 @@ public abstract class SliceCursor extends AbstractCursor { } private static class Skip extends SliceCursor { - private volatile long mSkip; - - Skip(Cursor source, long skip) { - super(source); - mSkip = skip; - } - - public boolean hasNext() throws FetchException { - doSkip(); - return mSource.hasNext(); - } - - public S next() throws FetchException { - doSkip(); - return mSource.next(); - } - - @Override - public int skipNext(int amount) throws FetchException { - doSkip(); - return mSource.skipNext(amount); - } - - private void doSkip() throws FetchException { - if (mSkip > 0) { - while (mSkip > Integer.MAX_VALUE) { - mSkip -= mSource.skipNext(Integer.MAX_VALUE); - } - mSource.skipNext((int) mSkip); - mSkip = 0; - } - } + private volatile long mSkip; + + Skip(Cursor source, long skip) { + super(source); + mSkip = skip; + } + + public boolean hasNext() throws FetchException { + doSkip(); + return mSource.hasNext(); + } + + public S next() throws FetchException { + doSkip(); + return mSource.next(); + } + + @Override + public int skipNext(int amount) throws FetchException { + doSkip(); + return mSource.skipNext(amount); + } + + private void doSkip() throws FetchException { + if (mSkip > 0) { + while (mSkip > Integer.MAX_VALUE) { + mSkip -= mSource.skipNext(Integer.MAX_VALUE); + } + mSource.skipNext((int) mSkip); + mSkip = 0; + } + } } private static class Limit extends SliceCursor { - private volatile long mRemaining; - - Limit(Cursor source, long remaining) { - super(source); - mRemaining = remaining; - } - - public boolean hasNext() throws FetchException { - if (mSource.hasNext()) { - if (mRemaining > 0) { - return true; - } - mSource.close(); - } - return false; - } - - public S next() throws FetchException { - if (mRemaining <= 0) { - throw new NoSuchElementException(); - } - S next = mSource.next(); - if (--mRemaining <= 0) { - mSource.close(); - } - return next; - } - - @Override - public int skipNext(int amount) throws FetchException { - if (mRemaining <= 0) { - return 0; - } - if (amount > mRemaining) { - amount = (int) mRemaining; - } - amount = mSource.skipNext(amount); - if ((mRemaining -= amount) <= 0) { - mSource.close(); - } - return amount; - } + private volatile long mRemaining; + + Limit(Cursor source, long remaining) { + super(source); + mRemaining = remaining; + } + + public boolean hasNext() throws FetchException { + if (mSource.hasNext()) { + if (mRemaining > 0) { + return true; + } + mSource.close(); + } + return false; + } + + public S next() throws FetchException { + if (mRemaining <= 0) { + throw new NoSuchElementException(); + } + S next = mSource.next(); + if (--mRemaining <= 0) { + mSource.close(); + } + return next; + } + + @Override + public int skipNext(int amount) throws FetchException { + if (mRemaining <= 0) { + return 0; + } + if (amount > mRemaining) { + amount = (int) mRemaining; + } + amount = mSource.skipNext(amount); + if ((mRemaining -= amount) <= 0) { + mSource.close(); + } + return amount; + } } } -- cgit v1.2.3