외우지말고 이해하라.

외우는 것 보단 이해해서 내것으로 만들어 활용하기

Another-Develop/kotlin

05 코틀린 - init, mapOf() , toMap()

hyg4196 2021. 4. 27. 11:36
반응형

- 액션Path,파라미터  ...  

 


- init 이란? 일종의 초기화 블록코틀린 문법 -> 객체 생성시 딱 한번 실행된다.

 

class Rq(command:String){

    val actionPath: String
    val paramMap: Map<String, String>

    init {
        val commandBits = command.split("?", limit = 2)
        actionPath = commandBits[0].trim()
        val queryStr = if (commandBits.lastIndex == 1 && commandBits[1].isNotEmpty()) {
            commandBits[1].trim()
        } else {
            ""
        }

        paramMap = if (queryStr.isEmpty()) {
            mapOf()
        } else {
            val paramMapTemp = mutableMapOf<String, String>()

            // queryStr = id=1&body=2&title=3&age
            val queryStrBits = queryStr.split("&")

            for (queryStrBit in queryStrBits) {
                // queryStrBit = id=1
                val queryStrBitBits = queryStrBit.split("=", limit = 2)
                val paramName = queryStrBitBits[0]
                val paramValue = if (queryStrBitBits.lastIndex == 1 && queryStrBitBits[1].isNotEmpty()) {
                    queryStrBitBits[1].trim()
                } else {
                    ""
                }

                if (paramValue.isNotEmpty()) {
                    paramMapTemp[paramName] = paramValue
                }
            }

            paramMapTemp.toMap()
        }
    }


- mapOf() : 읽기 전용으로 만든다.

 

 

- toMap() : 리스트를 Map 으로 바꾼다.

 

paramMap = if (queryStr.isEmpty()) {
            mapOf()
        } else {
            val paramMapTemp = mutableMapOf<String, String>()

            // queryStr = id=1&body=2&title=3&age
            val queryStrBits = queryStr.split("&")

            for (queryStrBit in queryStrBits) {
                // queryStrBit = id=1
                val queryStrBitBits = queryStrBit.split("=", limit = 2)
                val paramName = queryStrBitBits[0]
                val paramValue = if (queryStrBitBits.lastIndex == 1 && queryStrBitBits[1].isNotEmpty()) {
                    queryStrBitBits[1].trim()
                } else {
                    ""
                }

                if (paramValue.isNotEmpty()) {
                    paramMapTemp[paramName] = paramValue
                }
            }

            paramMapTemp.toMap()
        }

 

전체 코드

 

fun Day05_P(){
    println("==== Simple SSG 시작 =====")
    while (true){
        print("명령어) ")
        val command = readLineTrim()

        val rq = Rq(command)
        println("rq.actionPath : ${rq.actionPath}, rq.paramMap : ${rq.paramMap}")
        println(rq.getStringParam("title", "1") == "제목1")
        println(rq.getIntParam("id",-1)==1)
    }
}

class Rq(command:String){

    val actionPath: String
    val paramMap: Map<String, String>

    init {
        val commandBits = command.split("?", limit = 2)
        actionPath = commandBits[0].trim()
        val queryStr = if (commandBits.lastIndex == 1 && commandBits[1].isNotEmpty()) {
            commandBits[1].trim()
        } else {
            ""
        }

        paramMap = if (queryStr.isEmpty()) {
            mapOf()
        } else {
            val paramMapTemp = mutableMapOf<String, String>()

            // queryStr = id=1&body=2&title=3&age
            val queryStrBits = queryStr.split("&")

            for (queryStrBit in queryStrBits) {
                // queryStrBit = id=1
                val queryStrBitBits = queryStrBit.split("=", limit = 2)
                val paramName = queryStrBitBits[0]
                val paramValue = if (queryStrBitBits.lastIndex == 1 && queryStrBitBits[1].isNotEmpty()) {
                    queryStrBitBits[1].trim()
                } else {
                    ""
                }

                if (paramValue.isNotEmpty()) {
                    paramMapTemp[paramName] = paramValue
                }
            }

            paramMapTemp.toMap()
        }
    }

    fun getStringParam(key:String, default:String): String {
        return try {
            paramMap[key]!!
        }catch (e:NullPointerException){
            default
        }
    }

    fun getIntParam(key:String, default: Int): Int {
        return try{
            paramMap[key]!!.trim().toInt()
        }catch (e:NullPointerException){
            default
        }
반응형