|
| 1 | +package org.codeoverflow.chatoverflow.requirement.parameter |
| 2 | + |
| 3 | +import java.util.{Collections, List} |
| 4 | + |
| 5 | +import org.codeoverflow.chatoverflow.registry.Impl |
| 6 | +import org.codeoverflow.chatoverflow.api.io.parameter.MapParameter |
| 7 | + |
| 8 | +import collection.JavaConverters._ |
| 9 | +import java.util.Map |
| 10 | +/** |
| 11 | + * A parameter holding a Map<String, String> value. |
| 12 | + */ |
| 13 | +@Impl(impl = classOf[MapParameter]) |
| 14 | +class MapParameterImpl extends MapParameter{ |
| 15 | + private var value: Map[String, String] = null |
| 16 | + |
| 17 | + override def getType: Class[Map[String, String]] = classOf[Map[String, String]] |
| 18 | + |
| 19 | + override def serialize(): String = { |
| 20 | + var out = "" |
| 21 | + value.keySet().forEach(key => { |
| 22 | + var valueSet = value.get(key) |
| 23 | + if(out == ""){ |
| 24 | + out = out + "("+key+";"+valueSet+")" |
| 25 | + }else{ |
| 26 | + out = out + ",("+key+";"+valueSet+")" |
| 27 | + } |
| 28 | + }) |
| 29 | + out |
| 30 | + } |
| 31 | + |
| 32 | + override def get(): Map[String, String] = value |
| 33 | + |
| 34 | + override def deserialize(value: String): Unit = { |
| 35 | + var splits = value.split(","); |
| 36 | + if (splits.length == 1) { |
| 37 | + var obj = splits(0) |
| 38 | + obj = obj.replaceAll("\\(" ,"").replaceAll("\\)","").trim |
| 39 | + splits = obj.split(";") |
| 40 | + if(splits.length == 2){ |
| 41 | + set(Collections.singletonMap[String, String](splits(0), splits(1))) |
| 42 | + } |
| 43 | + } else { |
| 44 | + var map = scala.collection.mutable.Map[String, String]() |
| 45 | + for (part <- splits) { |
| 46 | + var obj: String = part.replaceAll("\\(" ,"").replaceAll("\\)","").trim |
| 47 | + var parts = obj.split(";") |
| 48 | + if(parts.length == 2){ |
| 49 | + map += (parts(0) -> parts(1)) |
| 50 | + } |
| 51 | + } |
| 52 | + if(map.nonEmpty) set(mutableMapAsJavaMap[String, String](map)) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + override def set(value: Map[String, String]): Unit = this.value = value |
| 57 | + |
| 58 | +} |
0 commit comments