moved the Map<String,String>.toProperties() helper function into the standard library and added a test case
This commit is contained in:
@@ -6,16 +6,6 @@ import java.math.BigDecimal
|
||||
import java.util.Map
|
||||
import java.util.Properties
|
||||
|
||||
private fun Map<String, String>.toProperties() : Properties {
|
||||
val p = Properties()
|
||||
|
||||
this.keySet().forEach {
|
||||
p.put(it, this[it])
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
/**
|
||||
* create connection for the specified jdbc url with no credentials
|
||||
*/
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.Map
|
||||
import java.util.Map.Entry as JEntry
|
||||
import java.util.SortedMap
|
||||
import java.util.TreeMap
|
||||
import java.util.Properties
|
||||
|
||||
// Map APIs
|
||||
|
||||
@@ -31,6 +32,19 @@ public inline fun <K,V> java.util.Map<K,V>.toSortedMap(): SortedMap<K,V> = toMap
|
||||
public inline fun <K,V> java.util.Map<K,V>.toSortedMap(comparator: Comparator<K>): SortedMap<K,V> = toMap<K,V>(TreeMap(comparator)) as SortedMap<K,V>
|
||||
|
||||
|
||||
/**
|
||||
* Converts this [[Map]] to a [[Properties]] object
|
||||
*
|
||||
* @includeFunctionBody ../../test/MapTest.kt toProperties
|
||||
*/
|
||||
public inline fun Map<String, String>.toProperties(): Properties {
|
||||
val answer = Properties()
|
||||
for (e in this) {
|
||||
answer.put(e.key, e.value)
|
||||
}
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
|
||||
*
|
||||
|
||||
@@ -187,4 +187,12 @@ class MapTest {
|
||||
}
|
||||
*/
|
||||
|
||||
test fun toProperties() {
|
||||
val map = hashMap("a" to "A", "b" to "B")
|
||||
val prop = map.toProperties()
|
||||
assertEquals(2, prop.size)
|
||||
assertEquals("A", prop.getProperty("a", "fail"))
|
||||
assertEquals("B", prop.getProperty("b", "fail"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user