Rewrite progression iterators to Kotlin

This commit is contained in:
Alexander Udalov
2013-12-25 21:38:11 +04:00
parent 1c2e1cb82f
commit 0e7ccec9d4
8 changed files with 122 additions and 337 deletions
+122
View File
@@ -0,0 +1,122 @@
package jet
import jet.runtime.ProgressionUtil
class ByteProgressionIterator(start: Byte, end: Byte, val increment: Int) : ByteIterator() {
private var next = start.toInt()
private val finalElement = ProgressionUtil.getProgressionFinalElement(start.toInt(), end.toInt(), increment).toByte()
private var hasNext = if (increment > 0) start <= end else start >= end
override fun hasNext() = hasNext
override fun nextByte(): Byte {
val value = next
if (value == finalElement.toInt()) {
hasNext = false
}
else {
next += increment
}
return value.toByte()
}
}
class CharProgressionIterator(start: Char, end: Char, val increment: Int) : CharIterator() {
private var next = start.toInt()
private val finalElement = ProgressionUtil.getProgressionFinalElement(start.toInt(), end.toInt(), increment).toChar()
private var hasNext = if (increment > 0) start <= end else start >= end
override fun hasNext() = hasNext
override fun nextChar(): Char {
val value = next
if (value == finalElement.toInt()) {
hasNext = false
}
else {
next += increment
}
return value.toChar()
}
}
class ShortProgressionIterator(start: Short, end: Short, val increment: Int) : ShortIterator() {
private var next = start.toInt()
private val finalElement = ProgressionUtil.getProgressionFinalElement(start.toInt(), end.toInt(), increment).toShort()
private var hasNext = if (increment > 0) start <= end else start >= end
override fun hasNext() = hasNext
override fun nextShort(): Short {
val value = next
if (value == finalElement.toInt()) {
hasNext = false
}
else {
next += increment
}
return value.toShort()
}
}
class IntProgressionIterator(start: Int, end: Int, val increment: Int) : IntIterator() {
private var next = start
private val finalElement = ProgressionUtil.getProgressionFinalElement(start, end, increment)
private var hasNext = if (increment > 0) start <= end else start >= end
override fun hasNext() = hasNext
override fun nextInt(): Int {
val value = next
if (value == finalElement) {
hasNext = false
}
else {
next += increment
}
return value
}
}
class LongProgressionIterator(start: Long, end: Long, val increment: Long) : LongIterator() {
private var next = start
private val finalElement = ProgressionUtil.getProgressionFinalElement(start, end, increment)
private var hasNext = if (increment > 0) start <= end else start >= end
override fun hasNext() = hasNext
override fun nextLong(): Long {
val value = next
if (value == finalElement) {
hasNext = false
}
else {
next += increment
}
return value
}
}
class FloatProgressionIterator(start: Float, val end: Float, val increment: Float) : FloatIterator() {
private var next = start
override fun hasNext() = if (increment > 0) next <= end else next >= end
override fun nextFloat(): Float {
val value = next
next += increment
return value
}
}
class DoubleProgressionIterator(start: Double, val end: Double, val increment: Double) : DoubleIterator() {
private var next = start
override fun hasNext() = if (increment > 0) next <= end else next >= end
override fun nextDouble(): Double {
val value = next
next += increment
return value
}
}
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 jet;
import jet.runtime.ProgressionUtil;
class ByteProgressionIterator extends ByteIterator {
private int next;
private final int increment;
private final byte finalElement;
private boolean hasNext;
public ByteProgressionIterator(byte start, byte end, int increment) {
this.next = start;
this.increment = increment;
this.finalElement = (byte) ProgressionUtil.getProgressionFinalElement(start, end, increment);
this.hasNext = increment > 0 ? start <= end : start >= end;
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public byte nextByte() {
int value = next;
if (value == finalElement) {
hasNext = false;
}
else {
next += increment;
}
return (byte) value;
}
}
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 jet;
import jet.runtime.ProgressionUtil;
class CharProgressionIterator extends CharIterator {
private int next;
private final int increment;
private final char finalElement;
private boolean hasNext;
public CharProgressionIterator(char start, char end, int increment) {
this.next = start;
this.increment = increment;
this.finalElement = (char) ProgressionUtil.getProgressionFinalElement(start, end, increment);
this.hasNext = increment > 0 ? start <= end : start >= end;
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public char nextChar() {
int value = next;
if (value == finalElement) {
hasNext = false;
}
else {
next += increment;
}
return (char) value;
}
}
@@ -1,41 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 jet;
class DoubleProgressionIterator extends DoubleIterator {
private double next;
private final double end;
private final double increment;
public DoubleProgressionIterator(double start, double end, double increment) {
this.next = start;
this.end = end;
this.increment = increment;
}
@Override
public boolean hasNext() {
return increment > 0 ? next <= end : next >= end;
}
@Override
public double nextDouble() {
double value = next;
next += increment;
return value;
}
}
@@ -1,41 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 jet;
class FloatProgressionIterator extends FloatIterator {
private float next;
private final float end;
private final float increment;
public FloatProgressionIterator(float start, float end, float increment) {
this.next = start;
this.end = end;
this.increment = increment;
}
@Override
public boolean hasNext() {
return increment > 0 ? next <= end : next >= end;
}
@Override
public float nextFloat() {
float value = next;
next += increment;
return value;
}
}
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 jet;
import jet.runtime.ProgressionUtil;
class IntProgressionIterator extends IntIterator {
private int next;
private final int increment;
private final int finalElement;
private boolean hasNext;
public IntProgressionIterator(int start, int end, int increment) {
this.next = start;
this.increment = increment;
this.finalElement = ProgressionUtil.getProgressionFinalElement(start, end, increment);
this.hasNext = increment > 0 ? start <= end : start >= end;
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public int nextInt() {
int value = next;
if (value == finalElement) {
hasNext = false;
}
else {
next += increment;
}
return value;
}
}
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 jet;
import jet.runtime.ProgressionUtil;
class LongProgressionIterator extends LongIterator {
private long next;
private final long increment;
private final long finalElement;
private boolean hasNext;
public LongProgressionIterator(long start, long end, long increment) {
this.next = start;
this.increment = increment;
this.finalElement = ProgressionUtil.getProgressionFinalElement(start, end, increment);
this.hasNext = increment > 0 ? start <= end : start >= end;
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public long nextLong() {
long value = next;
if (value == finalElement) {
hasNext = false;
}
else {
next += increment;
}
return value;
}
}
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 jet;
import jet.runtime.ProgressionUtil;
class ShortProgressionIterator extends ShortIterator {
private int next;
private final int increment;
private final short finalElement;
private boolean hasNext;
public ShortProgressionIterator(short start, short end, int increment) {
this.next = start;
this.increment = increment;
this.finalElement = (short) ProgressionUtil.getProgressionFinalElement(start, end, increment);
this.hasNext = increment > 0 ? start <= end : start >= end;
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public short nextShort() {
int value = next;
if (value == finalElement) {
hasNext = false;
}
else {
next += increment;
}
return (short) value;
}
}