Skip to content

Commit

Permalink
Change implementation of AtomicCyclicIterator so that it works with J…
Browse files Browse the repository at this point in the history
…S/Native
  • Loading branch information
adamw committed Jun 1, 2021
1 parent 11968c9 commit 3579395
Showing 1 changed file with 3 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package sttp.client3.testing

import java.util.concurrent.atomic.AtomicInteger
import java.util.function.IntUnaryOperator
import scala.util.{Failure, Success, Try}

final class AtomicCyclicIterator[+T] private(val elements: Seq[T]) {
final class AtomicCyclicIterator[+T] private (val elements: Seq[T]) {
private val vector = elements.toVector
private val lastIndex = elements.length - 1
private val length = elements.length
private val currentIndex = new AtomicInteger(0)

private val toNextIndex = new IntUnaryOperator {
final override def applyAsInt(i: Int): Int =
if (i == lastIndex) 0 else i + 1
}

def next(): T = {
val index = currentIndex.getAndUpdate(toNextIndex)
val index = currentIndex.getAndIncrement % length
vector(index)
}
}
Expand Down

0 comments on commit 3579395

Please sign in to comment.