From 83c9091b5b1ae0fca552e03eb55e85cfb316ccd2 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 6 Apr 2008 04:17:35 +0000 Subject: Replace SliceCursor with simpler SkipCursor and LimitCursor classes. --- .../com/amazon/carbonado/cursor/LimitCursor.java | 89 ++++++++++++ .../com/amazon/carbonado/cursor/SkipCursor.java | 77 +++++++++++ .../com/amazon/carbonado/cursor/SliceCursor.java | 153 --------------------- 3 files changed, 166 insertions(+), 153 deletions(-) create mode 100644 src/main/java/com/amazon/carbonado/cursor/LimitCursor.java create mode 100644 src/main/java/com/amazon/carbonado/cursor/SkipCursor.java delete mode 100644 src/main/java/com/amazon/carbonado/cursor/SliceCursor.java (limited to 'src/main/java/com/amazon/carbonado/cursor') diff --git a/src/main/java/com/amazon/carbonado/cursor/LimitCursor.java b/src/main/java/com/amazon/carbonado/cursor/LimitCursor.java new file mode 100644 index 0000000..ce96f6a --- /dev/null +++ b/src/main/java/com/amazon/carbonado/cursor/LimitCursor.java @@ -0,0 +1,89 @@ +/* + * Copyright 2008 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 to limit the amount of elements. + * + * @author Brian S O'Neill + */ +public class LimitCursor extends AbstractCursor { + private final Cursor mSource; + private volatile long mRemaining; + + /** + * @param limit maximum amount of elements + * @throws IllegalArgumentException if source is null or limit is negative + */ + public LimitCursor(Cursor source, long limit) { + if (source == null) { + throw new IllegalArgumentException("Source is null"); + } + if (limit < 0) { + throw new IllegalArgumentException("Limit is negative: " + limit); + } + mSource = source; + mRemaining = limit; + } + + 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; + } + + public void close() throws FetchException { + mSource.close(); + } +} diff --git a/src/main/java/com/amazon/carbonado/cursor/SkipCursor.java b/src/main/java/com/amazon/carbonado/cursor/SkipCursor.java new file mode 100644 index 0000000..7379373 --- /dev/null +++ b/src/main/java/com/amazon/carbonado/cursor/SkipCursor.java @@ -0,0 +1,77 @@ +/* + * Copyright 2008 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 com.amazon.carbonado.Cursor; +import com.amazon.carbonado.FetchException; + +/** + * Wraps another cursor to skip an initial amount of elements. + * + * @author Brian S O'Neill + */ +public class SkipCursor extends AbstractCursor { + private final Cursor mSource; + private volatile long mSkip; + + /** + * @param skip initial amount of elements to skip + * @throws IllegalArgumentException if source is null or skip is negative + */ + public SkipCursor(Cursor source, long skip) { + if (source == null) { + throw new IllegalArgumentException("Source is null"); + } + if (skip < 0) { + throw new IllegalArgumentException("Skip is negative: " + skip); + } + mSource = 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); + } + + public void close() throws FetchException { + mSource.close(); + } + + 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; + } + } +} diff --git a/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java b/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java deleted file mode 100644 index bf14339..0000000 --- a/src/main/java/com/amazon/carbonado/cursor/SliceCursor.java +++ /dev/null @@ -1,153 +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 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 - * @throws IllegalArgumentException if source is null or from is negative - */ - 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 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) { - source = slice(source, from); - long remaining = to - from; - if (remaining < 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; - } - } -} -- cgit v1.2.3