-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slowly killing UncheckedScalar as there is no checked exceptions in K…
…otlin
- Loading branch information
Showing
8 changed files
with
80 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 18 additions & 36 deletions
54
kactoos-jvm/src/main/kotlin/nnl/rocks/kactoos/iterator/Sorted.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,39 @@ | ||
package nnl.rocks.kactoos.iterator | ||
|
||
import nnl.rocks.kactoos.Scalar | ||
import nnl.rocks.kactoos.scalar.Constant | ||
import nnl.rocks.kactoos.scalar.StickyScalar | ||
import nnl.rocks.kactoos.scalar.UncheckedScalar | ||
import java.util.Comparator | ||
import java.util.LinkedList | ||
|
||
/** | ||
* Sorted iterator. | ||
* | ||
* | ||
* There is no thread-safety guarantee. | ||
* | ||
* | ||
* | ||
* @param T Element type | ||
* @since 0.7 | ||
* @since 0.3 | ||
*/ | ||
class Sorted<T : Comparable<T>>( | ||
comparator: Comparator<T>, | ||
iterator: Iterator<T> | ||
) : Iterator<T> { | ||
|
||
/** | ||
* Sorted one. | ||
*/ | ||
private val scalar: UncheckedScalar<Iterator<T>> | ||
|
||
/** | ||
* If you're using this ctor you must be sure that type `T` | ||
* implements [Comparable] interface. Otherwise, there will be | ||
* a type casting exception in runtime. | ||
* | ||
* @param items The underlying iterator | ||
*/ | ||
constructor(items: Iterator<T>) : this(Comparator.naturalOrder<T>() as Comparator<T>, items) | ||
private val scalar: Scalar<Iterator<T>> | ||
) : Iterator<T> by scalar() { | ||
|
||
init { | ||
this.scalar = UncheckedScalar( | ||
StickyScalar<Iterator<T>>( | ||
Constant { | ||
val items = LinkedList<T>() | ||
while (iterator.hasNext()) { | ||
items.add(iterator.next()) | ||
} | ||
items.sortWith(comparator) | ||
items.iterator() | ||
constructor( | ||
comparator: Comparator<T>, | ||
iterator: Iterator<T> | ||
) : this( | ||
StickyScalar<Iterator<T>>( | ||
Constant { | ||
val items = LinkedList<T>() | ||
while (iterator.hasNext()) { | ||
items.add(iterator.next()) | ||
} | ||
) | ||
items.sortWith(comparator) | ||
items.iterator() | ||
} | ||
) | ||
} | ||
) | ||
|
||
override fun hasNext(): Boolean = this.scalar().hasNext() | ||
constructor(items: Iterator<T>) : this(Comparator.naturalOrder<T>(), items) | ||
|
||
override fun next(): T = this.scalar().next() | ||
} |
43 changes: 15 additions & 28 deletions
43
kactoos-jvm/src/main/kotlin/nnl/rocks/kactoos/iterator/StickyIterator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,37 @@ | ||
package nnl.rocks.kactoos.iterator | ||
|
||
import nnl.rocks.kactoos.Scalar | ||
import nnl.rocks.kactoos.iterable.IterableOf | ||
import nnl.rocks.kactoos.scalar.Constant | ||
import nnl.rocks.kactoos.scalar.StickyScalar | ||
import nnl.rocks.kactoos.scalar.UncheckedScalar | ||
import java.util.LinkedList | ||
|
||
/** | ||
* Iterator that returns the same set of elements always. | ||
* | ||
* | ||
* There is no thread-safety guarantee. | ||
* | ||
* | ||
* | ||
* @param X Type of item | ||
* @since 0.8 | ||
* @since 0.3 | ||
*/ | ||
class StickyIterator<X : Any>(iterator: Iterator<X>) : Iterator<X> { | ||
|
||
/** | ||
* The gate. | ||
*/ | ||
private val gate: UncheckedScalar<Iterator<X>> | ||
class StickyIterator<X : Any>( | ||
private val gate: Scalar<Iterator<X>> | ||
) : Iterator<X> by gate() { | ||
|
||
init { | ||
this.gate = UncheckedScalar( | ||
StickyScalar<Iterator<X>>( | ||
Constant { | ||
val temp = LinkedList<X>() | ||
while (iterator.hasNext()) { | ||
temp.add(iterator.next()) | ||
} | ||
temp.iterator() | ||
constructor(iterator: Iterator<X>) : this( | ||
StickyScalar<Iterator<X>>( | ||
Constant { | ||
val temp = LinkedList<X>() | ||
while (iterator.hasNext()) { | ||
temp.add(iterator.next()) | ||
} | ||
) | ||
temp.iterator() | ||
} | ||
) | ||
} | ||
) | ||
|
||
/** | ||
* @param items Items to iterate | ||
* @param items Items to iterate over | ||
*/ | ||
@SafeVarargs | ||
constructor(vararg items: X) : this(IterableOf<X>(items.iterator()).iterator()) | ||
|
||
override fun hasNext(): Boolean = this.gate().hasNext() | ||
|
||
override fun next(): X = this.gate().next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 8 additions & 12 deletions
20
kactoos-jvm/src/main/kotlin/nnl/rocks/kactoos/time/OffsetDateTimeAsText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters