Skip to content

Commit

Permalink
remove deprecated use of Enum.values()
Browse files Browse the repository at this point in the history
  • Loading branch information
Baret committed Mar 10, 2024
1 parent 0e01497 commit f27a745
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ object DemoMap {
for (x in 0 until TILE_COUNT) {
for (y in 0 until TILE_COUNT) {
val coordinate = Coordinate(origin.eastingFromLeft + x, origin.northingFromBottom + y)
val height = TerrainHeight.values()[x % TerrainHeight.values().size]
var type = TerrainType.values()[(x / (TILE_COUNT / 2) + y / (TILE_COUNT / 2))]
val height = TerrainHeight.entries[x % TerrainHeight.entries.size]
var type = TerrainType.entries[(x / (TILE_COUNT / 2) + y / (TILE_COUNT / 2))]
if (x > TILE_COUNT / 2 && y > TILE_COUNT / 2) {
type = TerrainType.values()[3]
type = TerrainType.entries.toTypedArray()[3]
}
if (coordinate.eastingFromLeft % 5 == 0 && coordinate.northingFromBottom % 5 == 0) {
sectorTiles.add(WorldTile(coordinate, Terrain.of(type, TerrainHeight.TEN)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import de.gleex.pltcmd.util.debug.DebugFeature

@DebugFeature("Just to see all FrontendStrings of the units")
fun main() {
val formatsFormat = Format.values()
val formatsFormat = Format.entries
.joinToString("", "", "|") { it.asFormatString() }
val headerFormat = "| %3s $formatsFormat"
val lineFormat = "| %3d $formatsFormat"

val headerValues: MutableList<String> = Format.values()
val headerValues: MutableList<String> = Format.entries
.map {
it.toFrontendString(it).value
}
Expand All @@ -26,12 +26,12 @@ fun main() {

println()
var i = 1
Units.values()
Units.entries
.groupBy { it.kind }
.forEach { (kind, units) ->
println(kind)
units.forEach { unit ->
val formatValues: MutableList<Any> = Format.values()
val formatValues: MutableList<Any> = Format.entries
.map {
unit.toFrontendString(it).value
}
Expand All @@ -41,7 +41,7 @@ fun main() {
}
}
println()
println("There you have all ${Units.values().size} units.")
println("There you have all ${Units.entries.size} units.")
}

private fun Format.asFormatString() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum class Format(val length: Int) {
* Returns the largest [Format] that fits into the given width.
*/
fun forWidth(width: Int): Format {
return Format.values()
return entries
.filter { it.length <= width }
.maxByOrNull { it.length }
?: Format.ICON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.kotest.matchers.shouldBe
class CoordinateFrontendStringTest : WordSpec({
val normalCoordinate = Coordinate(123, 456)
"A typical coordinate like $normalCoordinate" should {
Format.values()
Format.entries
.forEach { format ->
"be formatted correctly for format $format" {
val actual = normalCoordinate.toFrontendString(format).value
Expand All @@ -26,7 +26,7 @@ class CoordinateFrontendStringTest : WordSpec({

"A coordinate with more than 3 digits" should {
val longEasting = Coordinate(1234, 567)
Format.values()
Format.entries
.forEach { format ->
"use a star for easting when necessary for format $format, testing with coordinate $longEasting" {
val actual = longEasting.toFrontendString(format).value
Expand All @@ -41,7 +41,7 @@ class CoordinateFrontendStringTest : WordSpec({
}

val longNorthing = Coordinate(123, 4567)
Format.values()
Format.entries
.forEach { format ->
"use a star for northing when necessary for format $format, testing with coordinate $longNorthing" {
val actual = longNorthing.toFrontendString(format).value
Expand All @@ -56,7 +56,7 @@ class CoordinateFrontendStringTest : WordSpec({
}

val longCoordinate = Coordinate(12345, 67890)
Format.values()
Format.entries
.forEach { format ->
"use two stars when necessary for format $format, testing with coordinate $longCoordinate" {
val actual = longCoordinate.toFrontendString(format).value
Expand All @@ -73,7 +73,7 @@ class CoordinateFrontendStringTest : WordSpec({

"Negative coordinates" should {
val negativeEasting = Coordinate(-1234, 567)
Format.values()
Format.entries
.forEach { format ->
"should use a minus for easting when necessary for format $format, testing with coordinate $negativeEasting" {
val actual = negativeEasting.toFrontendString(format).value
Expand All @@ -88,7 +88,7 @@ class CoordinateFrontendStringTest : WordSpec({
}

val negativeNorthing = Coordinate(123, -456)
Format.values()
Format.entries
.forEach { format ->
"should use a minus for northing when necessary for format $format, testing with coordinate $negativeNorthing" {
val actual = negativeNorthing.toFrontendString(format).value
Expand All @@ -103,7 +103,7 @@ class CoordinateFrontendStringTest : WordSpec({
}

val negative = Coordinate(-123, -345)
Format.values()
Format.entries
.forEach { format ->
"should use two minus when necessary for format $format, testing with coordinate $negative" {
val actual = negative.toFrontendString(format).value
Expand All @@ -118,7 +118,7 @@ class CoordinateFrontendStringTest : WordSpec({
}

val negativeAndLong = Coordinate(-123, 45678)
Format.values()
Format.entries
.forEach { format ->
"should use a minus AND a star when necessary for format $format, testing with coordinate $negativeAndLong" {
val actual = negativeAndLong.toFrontendString(format).value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class DefaultTransformationTest : WordSpec({

"The default transformation for frontend strings" should {
"truncate correctly" {
Format.values()
Format.entries
.toTypedArray()
.forAll { format ->
val actualValue = testString.defaultTransformation(format)
when (format) {
Expand All @@ -25,7 +26,8 @@ class DefaultTransformationTest : WordSpec({

"call toString()" {
val a = A(15)
Format.values()
Format.entries
.toTypedArray()
.forAll { format ->
val actualValue = a.defaultTransformation(format)
when (format) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import io.kotest.matchers.string.shouldHaveMaxLength
class TerrainTransformationsTest : WordSpec({
"All transformations for terrain height" should {
"be valid" {
Format.values()
Format.entries.toTypedArray()
.forAll { format ->
TerrainHeight.values()
TerrainHeight.entries.toTypedArray()
.forAll { terrainHeight ->
assertSoftly {
val directlyTransformed = terrainHeight.terrainHeightTransformation(format)
Expand All @@ -30,9 +30,9 @@ class TerrainTransformationsTest : WordSpec({

"All transformations for terrain type" should {
"be valid" {
Format.values()
Format.entries.toTypedArray()
.forAll { format ->
TerrainType.values()
TerrainType.entries.toTypedArray()
.forAll { terrainType ->
assertSoftly {
val directlyTransformed = terrainType.terrainTypeTransformation(format)
Expand All @@ -46,11 +46,11 @@ class TerrainTransformationsTest : WordSpec({

"All transformations for every terrain combination" should {
"be valid" {
Format.values()
Format.entries.toTypedArray()
.forAll { format ->
TerrainType.values()
TerrainType.entries.toTypedArray()
.forAll { type ->
TerrainHeight.values().forAll { height ->
TerrainHeight.entries.toTypedArray().forAll { height ->
assertSoftly {
val terrain = Terrain.of(type, height)
val directlyTransformed = terrain.terrainTransformation(format)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class UnitTransformationTest : WordSpec() {
init {
"The unit transformation" should {
"work for units and unit blueprints" {
Format.values()
Format.entries.toTypedArray()
.forAll { format ->
Units.values()
Units.entries.toTypedArray()
.forAll { blueprint ->
val blueprintValue: String = blueprint.toFrontendString(format).value
val unitValue: String = blueprint.new()
Expand All @@ -32,7 +32,7 @@ class UnitTransformationTest : WordSpec() {
}

"The icon for a unit" should {
Units.values()
Units.entries
.groupBy { it.kind }
.forEach { (unitKind, blueprints) ->
"be unique for unit kind $unitKind" {
Expand All @@ -44,7 +44,7 @@ class UnitTransformationTest : WordSpec() {
}

"The 3 digit frontend string of all units" should {
val short3Strings = Units.values()
val short3Strings = Units.entries
.map { it.toFrontendString(Format.SHORT3).value }
"be unique" {
short3Strings.logDuplicates(Format.SHORT3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object ColorRepository {
* Creates a [TileColor] for the given [Rung] to highlight and differentiate it in the UI.
*/
fun forRung(rung: Rung): TileColor {
val ratio = rung.ordinal.toDouble() / (Rung.values().size - 1).toDouble()
val ratio = rung.ordinal.toDouble() / (Rung.entries.size - 1).toDouble()
return TileColor
.create(77, 77, 77)
.interpolateTo(TileColor.create(191, 191, 191))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IncompleteMapGameArea(size: Size, worldSizeInTiles: Size) :
initialFilters = emptyList()) {

companion object {
private val BLOCKS_HEIGHT = TerrainHeight.values().size
private val BLOCKS_HEIGHT = TerrainHeight.entries.size
private const val Z_LEVEL_DEFAULT: Int = 0

val Position.verticalPositions: Sequence<Position3D>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CommandingElementTest : WordSpec() {
"The corps of a commanding element" should {
val commandingElement = buildCommandingElement()

val invalidCorps = Corps.values()
val invalidCorps = Corps.entries
.filter { it != commandingElement.corps }
"NOT allow adding elements of corps $invalidCorps when it is of corps ${commandingElement.corps} itself" {
invalidCorps.forNone {
Expand All @@ -46,7 +46,7 @@ class CommandingElementTest : WordSpec() {
"The element kind of a commanding element" should {
val commandingElement = buildCommandingElement()

val invalidKinds = ElementKind.values()
val invalidKinds = ElementKind.entries
.filter { it != commandingElement.kind }
"NOT allow adding elements of kind $invalidKinds when it is of kind ${commandingElement.kind} itself" {
invalidKinds.forNone {
Expand All @@ -64,7 +64,7 @@ class CommandingElementTest : WordSpec() {
"The rung of a subordinate" should {
val commandingElement = buildCommandingElement()

val invalidRungs = Rung.values()
val invalidRungs = Rung.entries
.filter { it >= commandingElement.rung }
"NOT allow adding elements of rung $invalidRungs when it is of rung ${commandingElement.rung} itself (only smaller rungs allowed)" {
invalidRungs.forNone {
Expand All @@ -73,7 +73,7 @@ class CommandingElementTest : WordSpec() {
}
}

val validRungs = Rung.values()
val validRungs = Rung.entries
.filter { it < commandingElement.rung }
"allow adding elements of rung $validRungs when it is of rung ${commandingElement.rung} itself" {
validRungs.forAll {
Expand All @@ -82,8 +82,8 @@ class CommandingElementTest : WordSpec() {
}
}

"have been checked against all ${Rung.values().size} rungs" {
invalidRungs + validRungs shouldHaveSize Rung.values().size
"have been checked against all ${Rung.entries.size} rungs" {
invalidRungs + validRungs shouldHaveSize Rung.entries.size
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class ElementTest : WordSpec({
}

// Test element creation with invalid unit kinds
ElementKind.values()
ElementKind.entries
.forEach { elementKind ->
val invalidUnitKinds = UnitKind
.values()
.filter {
elementKind.allowedUnitKinds.contains(it).not()
}
.entries
.filter {
elementKind.allowedUnitKinds.contains(it).not()
}
invalidUnitKinds.forEach { invalidUnitKind ->
val invalidUnitBlueprint = Units.values()
val invalidUnitBlueprint = Units.entries
.first { it.kind == invalidUnitKind }
"An element of kind $elementKind created with a $invalidUnitBlueprint" should {
"not be valid" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ data class Terrain private constructor(val type: TerrainType, val height: Terrai
/**
* Return a randomly created terrain.
*/
fun random(r: Random) = of(TerrainType.values()
fun random(r: Random) = of(
TerrainType.entries.toTypedArray()
.random(r), TerrainHeight.random(r))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ enum class TerrainType {
WATER_SHALLOW;

companion object {
fun random(r: Random) = TerrainType.values().random(r)
fun random(r: Random) = entries.toTypedArray().random(r)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import io.kotest.matchers.shouldBe
class TerrainHeightTest: StringSpec({
"Max terrain height should be ${TerrainHeight.TEN}" {
TerrainHeight.MAX shouldBe TerrainHeight.TEN
TerrainHeight.values().forAll {
TerrainHeight.entries.toTypedArray().forAll {
it.value shouldBeLessThanOrEqual TerrainHeight.MAX.value
}
}

"Min terrain height should be ${TerrainHeight.ONE}" {
TerrainHeight.MIN shouldBe TerrainHeight.ONE
TerrainHeight.values().forAll {
TerrainHeight.entries.toTypedArray().forAll {
it.value shouldBeGreaterThanOrEqual TerrainHeight.MIN.value
}
}
Expand Down

0 comments on commit f27a745

Please sign in to comment.