Advertisement
Guest User

kinvoke

a guest
Feb 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.25 KB | None | 0 0
  1. package example
  2.  
  3. import com.fasterxml.jackson.databind.JsonNode
  4. import com.fasterxml.jackson.databind.ObjectMapper
  5.  
  6. import java.util.stream.Stream
  7. import java.util.stream.StreamSupport
  8.  
  9. import org.w3c.dom.Document
  10. import org.w3c.dom.Element
  11. import org.w3c.dom.Node
  12. import org.w3c.dom.NodeList
  13. import javax.xml.parsers.DocumentBuilderFactory
  14.  
  15. data class Event(val id: Int)
  16.  
  17. val stringToEvent: (String) -> Event = { s -> Event(s.toInt()) }
  18. val dummyToEvent: (Document) -> Event = { _ -> Event(1) }
  19. val jsonToEvent: (JsonNode) -> Event = { j -> Event(j.get("id").asInt()) }
  20.  
  21. fun elementGen(opt: String): Any {
  22.     return when(opt) {
  23.         "string" -> "1"
  24.         "json" -> {
  25.             val mapper = ObjectMapper()
  26.             mapper.readTree("{\"id\": 1 }")
  27.         }
  28.         "xml" -> {
  29.             DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("<id>1</id>")
  30.         }
  31.         else -> throw RuntimeException("Option not supported")
  32.     }
  33. }
  34.  
  35. fun main(args : Array<String>) {
  36.  
  37.     val parser = when (args[0]) {
  38.         "string" -> stringToEvent
  39.         "json" -> jsonToEvent
  40.         "xml" -> dummyToEvent
  41.         else -> throw RuntimeException("Option not supported")
  42.     }
  43.  
  44.     print(parser(elementGen(args[0])))
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement