summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2008-01-27 16:53:47 +0000
committerBrian S. O'Neill <bronee@gmail.com>2008-01-27 16:53:47 +0000
commit530aa487847be05d87d9213ef669274289209c05 (patch)
tree089d19b73b2dff14507e062838a2dd5962643317 /src/main
parentf6ea4c0f97bf144ee2501839b6704af661a8e674 (diff)
Replace ViewCursor with SliceCursor.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/amazon/carbonado/cursor/SliceCursor.java167
-rw-r--r--src/main/java/com/amazon/carbonado/cursor/ViewCursor.java110
2 files changed, 167 insertions, 110 deletions
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<S> extends AbstractCursor<S> {
+ /**
+ * @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 <S> Cursor<S> slice(Cursor<S> 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 <S> Cursor<S> slice(Cursor<S> 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<S>(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<S>(source, remaining);
+ }
+
+ final Cursor<S> mSource;
+
+ SliceCursor(Cursor<S> source) {
+ mSource = source;
+ }
+
+ public void close() throws FetchException {
+ mSource.close();
+ }
+
+ private static class Skip<S> extends SliceCursor<S> {
+ private volatile long mSkip;
+
+ Skip(Cursor<S> 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<S> extends SliceCursor<S> {
+ private volatile long mRemaining;
+
+ Limit(Cursor<S> 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<S> extends AbstractCursor<S> {
- private final Cursor<S> 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<S> 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;
- }
-}