初めてkotlinを触る - Enum Classes

# 概要
* 基礎
* enumValues<T>とenumValueOf<T>()
* 各Enum定数はそれ自身の無名クラスを宣言することもできる
  * javaより簡単、便利
* 拡張関数

# 内容

### 基本定義 - javaと同じ
```kotlin
enum class Weather {
  SUNNY, CLOUDY, RAINY
}
```

### 初期化 - javaと違い
```kotlin
enum class Weather(val weather: String) {
  SUNNY("sunny"),
  CLOUDY("cloudy"),
  RAINY("rainy")
}
```

* https://kotlinlang.org/docs/reference/classes.html


### nameとordinal - javaと同じ

```kotlin
println(Weather.CLOUDY.name) // CLOUNDY
println(Weather.CLOUDY.ordinal) // 1
```

### valueOf()とvalues() - javaと同じ

```kotlin
println(Weather.valueOf("sunny".toUpperCase())) // SUNNY
for (weather: Weather in Weather.values()) {
  println(weather.name) // SUNNY CLOUDY RAINY
}
```



### enumValues<T>とenumValueOf<T>() - javaと違い

```kotlin
println(enumValueOf<Weather>("sunny".toUpperCase())) // SUNNY
print(enumValues<Weather>().joinToString { it.name }) // SUNNY, CLOUDY, RAINY
```



### 各Enum定数はそれ自身の無名クラスを宣言することもできる - javaと違い

##### kotlinで実現
```kotlin
fun main(args: Array<String>) {
    println(Weather.SUNNY.washingIndexName()) // 大変よく乾く
    println(Weather.SUNNY.washingIndex()) // 5
}

enum class Weather(val weather: String) {
  CLOUDY("cloudy"){
      override fun washingIndex() = 3
      override fun washingIndexName() = "乾く"
  },
  RAINY("rainy"){
      override fun washingIndex() = 1
      override fun washingIndexName() = "部屋干し推奨"
  },
  SUNNY("sunny"){
      override fun washingIndex() = 5
      override fun washingIndexName() = "大変よく乾く"
  };
  
  abstract fun washingIndex(): Int
  abstract fun washingIndexName(): String
}
```

コード確認: https://paiza.io/projects/DANyJNSE2ziwn6fqbmLzoQ


* 注意: 最後は;が必要、なければ以下のエラーになってしまう

```
Main.kt:19:4: error: expecting ';' after the last enum entry or '}' to close enum class body
  }
```



##### javaで実現

```kotlin
import java.util.*;

enum Weather {
    SUNNY("sunny", 5, "大変よく乾く"),
    CLOUDY("cloudy", 3, "乾く"),
    RAINY("rainy", 1, "部屋干し推奨");
    
    private String value;
    private int index;
    private String name;
    
    Weather(String value, int index, String name) {
        this.value = value;
        this.index = index;
        this.name = name;
    }
    
    public String getValue() {
        return this.value;
    }
    
    public static int getWashingIndex(Weather weather) {
        for(Weather e : Weather.values()) {
            if (weather == e) {
                return e.index;
            }
        }
        return 0;
    }
    
        public static String getWashingIndexName(Weather weather) {
        for(Weather e : Weather.values()) {
            if (weather == e) {
                return e.name;
            }
        }
        return "なし";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Weather.getWashingIndexName(Weather.SUNNY)); // 大変よく乾く
        System.out.println(Weather.getWashingIndex(Weather.SUNNY)); // 5
    }
}
```

コード確認：https://paiza.io/projects/DANyJNSE2ziwn6fqbmLzoQ




### 拡張関数

```kotlin
fun Weather.umbrellaIndex(){
    val index = when(this){
        Weather.SUNNY -> 0
        Weather.CLOUDY -> 1
        Weather.RAINY -> 2
    }
    println(index)
}

fun main(args: Array<String>) {
    Weather.RAINY.umbrellaIndex(); // 2
}
```

* https://dogwood008.github.io/kotlin-web-site-ja/docs/reference/extensions.html


---

# 参照
* https://tenki.jp/indexes/cloth_dried/3/17/4610/
* https://www.baeldung.com/kotlin-enum