From 530aa487847be05d87d9213ef669274289209c05 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 27 Jan 2008 16:53:47 +0000 Subject: Replace ViewCursor with SliceCursor. --- .../com/amazon/carbonado/cursor/SliceCursor.java | 167 +++++++++++++++++++++ .../com/amazon/carbonado/cursor/ViewCursor.java | 110 -------------- 2 files changed, 167 insertions(+), 110 deletions(-) create mode 100644 src/main/java/com/amazon/carbonado/cursor/SliceCursor.java delete mode 100644 src/main/java/com/amazon/carbonado/cursor/ViewCursor.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java b/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java new file mode 100644 index 0000000..882932f --- /dev/null +++ b/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java @@ -0,0 +1,167 @@ +/* + * Copyright 2007 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.amazon.carbonado.cursor; + +import java.util.NoSuchElementException; + +import com.amazon.carbonado.Cursor; +import com.amazon.carbonado.FetchException; + +/** + * Wraps another cursor and only produces a range of elements. The actual range + * might be smaller if the source cursor doesn't have enough elements. + * + * @author Brian S O'Neill + * @since 1.2 + */ +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 + */ + public static Cursor slice(Cursor source, long from, long to) { + return slice(source, (Long) from, (Long) to); + } + + /** + * @param from optional zero-based element to start from, inclusive + * @param to optional 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"); + } + + 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); + } + + final Cursor mSource; + + SliceCursor(Cursor source) { + mSource = source; + } + + public void close() throws FetchException { + mSource.close(); + } + + 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 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; + } + } +} diff --git a/src/main/java/com/amazon/carbonado/cursor/ViewCursor.java b/src/main/java/com/amazon/carbonado/cursor/ViewCursor.java deleted file mode 100644 index 01b02c3..0000000 --- a/src/main/java/com/amazon/carbonado/cursor/ViewCursor.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2007 Amazon Technologies, Inc. or its affiliates. - * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks - * of Amazon Technologies, Inc. or its affiliates. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.amazon.carbonado.cursor; - -import java.util.NoSuchElementException; - -import com.amazon.carbonado.Cursor; -import com.amazon.carbonado.FetchException; - -/** - * Wraps another cursor and only produces a range of elements. The range might - * be smaller if the source cursor doesn't have enough elements. - * - * @author Brian S O'Neill - * @since 1.2 - */ -public class ViewCursor extends AbstractCursor { - private final Cursor mSource; - - private volatile long mFrom; - private volatile long mRemaining; - - /** - * @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 ViewCursor(Cursor source, long from, long to) { - if (source == null) { - throw new IllegalArgumentException("Source is null"); - } - if (from < 0) { - throw new IllegalArgumentException("From is negative: " + from); - } - mSource = source; - mFrom = from; - mRemaining = to - from; - if (mRemaining <= 0) { - if (mRemaining < 0) { - throw new IllegalArgumentException("From is more than to: " + from + " > " + to); - } - // Don't bother skipping. - mFrom = 0; - } - } - - public void close() throws FetchException { - mSource.close(); - } - - public boolean hasNext() throws FetchException { - if (mFrom > 0) { - while (mFrom > Integer.MAX_VALUE) { - mFrom -= mSource.skipNext(Integer.MAX_VALUE); - } - mSource.skipNext((int) mFrom); - mFrom = 0; - } - if (mSource.hasNext()) { - if (mRemaining > 0) { - return true; - } - mSource.close(); - } - return false; - } - - public S next() throws FetchException { - if (!hasNext()) { - throw new NoSuchElementException(); - } - S next = mSource.next(); - if (--mRemaining <= 0) { - mSource.close(); - } - return next; - } - - @Override - public int skipNext(int amount) throws FetchException { - if (!hasNext()) { - return 0; - } - if (amount > mRemaining) { - amount = (int) mRemaining; - } - amount = mSource.skipNext(amount); - if ((mRemaining -= amount) <= 0) { - mSource.close(); - } - return amount; - } -} -- cgit v1.2.3