TupleN classes and their usages replaced by Pair and Triple

(KT-2358 Drop tuples)

 #KT-2358 In Progress
This commit is contained in:
Andrey Breslav
2012-09-07 21:23:38 +04:00
parent 8333448f10
commit 89fd0526cf
18 changed files with 58 additions and 59 deletions
+2 -2
View File
@@ -15,10 +15,10 @@ library("jsonGet")
public fun Json.get(paramName : String) : Any? = js.noImpl
library("jsonFromTuples")
public fun json(vararg pairs : Tuple2<String, Any?>) : Json = js.noImpl
public fun json(vararg pairs : Pair<String, Any?>) : Json = js.noImpl
library("jsonFromTuples")
public fun json2(pairs : Array<Tuple2<String, Any?>>) : Json = js.noImpl
public fun json2(pairs : Array<Pair<String, Any?>>) : Json = js.noImpl
library("jsonAddProperties")
public fun Json.add(other : Json) : Json = js.noImpl
+2 -2
View File
@@ -13,14 +13,14 @@ public fun <K, V> MutableMap<K, V>.set(key : K, value : V): Unit {
*
* @includeFunctionBody ../../test/MapTest.kt createUsingTuples
*/
public inline fun <K,V> hashMap(vararg values: #(K,V)): HashMap<K,V> {
public inline fun <K,V> hashMap(vararg values: Pair<K,V>): HashMap<K,V> {
val answer = HashMap<K,V>()
/**
TODO replace by this simpler call when we can pass vararg values into other methods
answer.putAll(values)
*/
for (v in values) {
answer.put(v._1, v._2)
answer.put(v.first, v.second)
}
return answer
}
@@ -30,8 +30,8 @@ var JFrame.title: String
setTitle(t)
}
var JFrame.size: #(Int, Int)
get() = #(getSize().sure().getWidth().toInt(), getSize().sure().getHeight().toInt())
var JFrame.size: Pair<Int, Int>
get() = Pair(getSize().sure().getWidth().toInt(), getSize().sure().getHeight().toInt())
set(dim) {
setSize(Dimension(dim._1, dim._2))
}
@@ -14,7 +14,7 @@ Enter some text here!
val f = frame("Kool Kotlin Swing Demo") {
exitOnClose()
val test = 12
size = #(500, 300)
size = Pair(500, 300)
val textArea = JTextArea(greeting)
@@ -96,11 +96,11 @@ public fun <T> Iterable<T>.contains(item : T) : Boolean {
*
* @includeFunctionBody ../../test/ListTest.kt withIndices
*/
public fun <T> Iterable<T>.withIndices(): List<#(Int, T)> {
val answer = ArrayList<#(Int, T)>()
public fun <T> Iterable<T>.withIndices(): List<Pair<Int, T>> {
val answer = ArrayList<Pair<Int, T>>()
var nextIndex = 0
for (e in this) {
answer.add(#(nextIndex, e))
answer.add(Pair(nextIndex, e))
nextIndex++
}
return answer
+6 -6
View File
@@ -24,14 +24,14 @@ public inline fun sortedSet<T>(comparator: Comparator<T>, vararg values: T) : Tr
*
* @includeFunctionBody ../../test/MapTest.kt createUsingTuples
*/
public inline fun <K,V> hashMap(vararg values: #(K,V)): HashMap<K,V> {
public inline fun <K,V> hashMap(vararg values: Pair<K,V>): HashMap<K,V> {
val answer = HashMap<K,V>(values.size)
/**
TODO replace by this simpler call when we can pass vararg values into other methods
answer.putAll(values)
*/
for (v in values) {
answer.put(v._1, v._2)
answer.put(v.first, v.second)
}
return answer
}
@@ -42,14 +42,14 @@ public inline fun <K,V> hashMap(vararg values: #(K,V)): HashMap<K,V> {
*
* @includeFunctionBody ../../test/MapTest.kt createSortedMap
*/
public inline fun <K,V> sortedMap(vararg values: #(K,V)): SortedMap<K,V> {
public inline fun <K,V> sortedMap(vararg values: Pair<K, V>): SortedMap<K,V> {
val answer = TreeMap<K,V>()
/**
TODO replace by this simpler call when we can pass vararg values into other methods
answer.putAll(values)
*/
for (v in values) {
answer.put(v._1, v._2)
answer.put(v.first, v.second)
}
return answer
}
@@ -61,14 +61,14 @@ public inline fun <K,V> sortedMap(vararg values: #(K,V)): SortedMap<K,V> {
*
* @includeFunctionBody ../../test/MapTest.kt createLinkedMap
*/
public inline fun <K,V> linkedMap(vararg values: #(K,V)): LinkedHashMap<K,V> {
public inline fun <K,V> linkedMap(vararg values: Pair<K, V>): LinkedHashMap<K,V> {
val answer = LinkedHashMap<K,V>(values.size)
/**
TODO replace by this simpler call when we can pass vararg values into other methods
answer.putAll(values)
*/
for (v in values) {
answer.put(v._1, v._2)
answer.put(v.first, v.second)
}
return answer
}
+2 -2
View File
@@ -94,9 +94,9 @@ public inline fun <K,V,R,C: MutableMap<K,R>> MutableMap<K,V>.mapValuesTo(result:
/**
* Puts all the entries into the map with the first value in the tuple being the key and the second the value
*/
public inline fun <K,V> MutableMap<K,V>.putAll(vararg values: #(K,V)): Unit {
public inline fun <K,V> MutableMap<K,V>.putAll(vararg values: Pair<K, V>): Unit {
for (v in values) {
put(v._1, v._2)
put(v.first, v.second)
}
}
+3 -4
View File
@@ -40,12 +40,12 @@ public inline fun <T> Iterator<T>.toHashSet() : HashSet<T> = toCollection(HashSe
/**
* Creates a tuple of type [[#(A,B)]] from this and *that* which can be useful for creating [[Map]] literals
* Creates a tuple of type [[Pair<A,B>]] from this and *that* which can be useful for creating [[Map]] literals
* with less noise, for example
* @includeFunctionBody ../../test/MapTest.kt createUsingTo
*/
public inline fun <A,B> A.to(that: B): #(A, B) = #(this, that)
public inline fun <A,B> A.to(that: B): Pair<A, B> = Pair(this, that)
/**
Run function f
@@ -61,5 +61,4 @@ public inline fun runnable(action: ()-> Unit): Runnable {
action()
}
}
}
}
@@ -16,7 +16,7 @@ class FunctionalQueue<T> (
public fun addFirst(element: T) : FunctionalQueue<T> = FunctionalQueue<T>(input, output add element)
public fun removeFirst() : #(T,FunctionalQueue<T>) =
public fun removeFirst() : Pair<T, FunctionalQueue<T>> =
if(output.empty) {
if(input.empty)
throw java.util.NoSuchElementException()
@@ -24,6 +24,6 @@ class FunctionalQueue<T> (
FunctionalQueue<T>(FunctionalList.emptyList<T>(), input.reversed()).removeFirst()
}
else {
#(output.head, FunctionalQueue<T>(input, output.tail))
Pair(output.head, FunctionalQueue<T>(input, output.tail))
}
}
+3 -3
View File
@@ -93,13 +93,13 @@ class CollectionJVMTest {
test fun sortBy() {
expect(arrayList("two" to 2, "three" to 3)) {
arrayList("three" to 3, "two" to 2).sortBy { it._2 }
arrayList("three" to 3, "two" to 2).sortBy { it.second }
}
expect(arrayList("three" to 3, "two" to 2)) {
arrayList("three" to 3, "two" to 2).sortBy { it._1 }
arrayList("three" to 3, "two" to 2).sortBy { it.first }
}
expect(arrayList("two" to 2, "three" to 3)) {
arrayList("three" to 3, "two" to 2).sortBy { it._1.length }
arrayList("three" to 3, "two" to 2).sortBy { it.first.length }
}
}
+2 -2
View File
@@ -38,8 +38,8 @@ class ListTest {
val wis = data.withIndices()
var index = 0
for (withIndex in wis) {
assertEquals(withIndex._1, index)
assertEquals(withIndex._2, data[index])
assertEquals(withIndex.first, index)
assertEquals(withIndex.second, data[index])
index++
}
assertEquals(data.size(), index)
+5 -5
View File
@@ -103,7 +103,7 @@ class MapTest {
}
test fun createUsingTuples() {
val map = hashMap(#("a", 1), #("b", 2))
val map = hashMap(Pair("a", 1), Pair("b", 2))
assertEquals(2, map.size)
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
@@ -117,7 +117,7 @@ class MapTest {
}
test fun createLinkedMap() {
val map = linkedMap(#("c", 3), #("b", 2), #("a", 1))
val map = linkedMap(Pair("c", 3), Pair("b", 2), Pair("a", 1))
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
assertEquals(3, map.get("c"))
@@ -125,7 +125,7 @@ class MapTest {
}
test fun createSortedMap() {
val map = sortedMap(#("c", 3), #("b", 2), #("a", 1))
val map = sortedMap(Pair("c", 3), Pair("b", 2), Pair("a", 1))
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
assertEquals(3, map.get("c"))
@@ -133,7 +133,7 @@ class MapTest {
}
test fun toSortedMap() {
val map = hashMap<String,Int>(#("c", 3), #("b", 2), #("a", 1))
val map = hashMap<String,Int>(Pair("c", 3), Pair("b", 2), Pair("a", 1))
val sorted = map.toSortedMap<String,Int>()
assertEquals(1, sorted.get("a"))
assertEquals(2, sorted.get("b"))
@@ -142,7 +142,7 @@ class MapTest {
}
test fun toSortedMapWithComparator() {
val map = hashMap(#("c", 3), #("bc", 2), #("bd", 4), #("abc", 1))
val map = hashMap(Pair("c", 3), Pair("bc", 2), Pair("bd", 4), Pair("abc", 1))
val c = comparator<String>{ a, b ->
val answer = a.length() - b.length()
if (answer == 0) a.compareTo(b) else answer
@@ -15,7 +15,7 @@ class Serial(val a : String) : java.lang.Object(), Serializable {
class SerialTest() : TestCase() {
fun testMe() {
val tuple = #("lala", "bbb", Serial("serial"))
val tuple = Triple("lala", "bbb", Serial("serial"))
val op = { -> tuple.toString() }
val baos = ByteArrayOutputStream()
+1 -1
View File
@@ -52,7 +52,7 @@ class MapJsTest {
*/
test fun createUsingTuples() {
val map = hashMap(#("a", 1), #("b", 2))
val map = hashMap(Pair("a", 1), Pair("b", 2))
assertEquals(2, map.size)
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
@@ -4,14 +4,14 @@ import java.util.HashMap
import java.util.List
import java.util.Map
fun <A, K, V> List<A>.toHashMap(f: (A) -> #(K, V)): Map<K, V> {
fun <A, K, V> List<A>.toHashMap(f: (A) -> Pair<K, V>): Map<K, V> {
val r = HashMap<K, V>()
for (item in this) {
val entry = f(item)
r.put(entry._1, entry._2)
r.put(entry.first, entry.second)
}
return r
}
fun <K, V> List<V>.toHashMapMappingToKey(f: (V) -> K): Map<K, V> = toHashMap { v -> #(f(v), v) }
fun <K, V> List<V>.toHashMapMappingToKey(f: (V) -> K): Map<K, V> = toHashMap { v -> Pair(f(v), v) }
@@ -16,24 +16,24 @@ private fun PsiElement.getAllChildren(): List<PsiElement> {
return r.build()
}
private fun splitPsiImpl(psi: PsiElement, listBuilder: ImmutableArrayListBuilder<#(String, PsiElement)>) {
private fun splitPsiImpl(psi: PsiElement, listBuilder: ImmutableArrayListBuilder<Pair<String, PsiElement>>) {
var lastPos = 0
for (child in psi.getAllChildren()) {
if (child.getTextChildRelativeOffset() > lastPos) {
val text = psi.getText()!!.substring(lastPos, child.getTextChildRelativeOffset())
listBuilder.add(#(text, psi))
listBuilder.add(Pair(text, psi))
}
splitPsiImpl(child, listBuilder)
lastPos = child.getTextChildRelativeOffset() + child.getTextRange()!!.getLength()
}
if (lastPos < psi.getTextRange()!!.getLength()) {
val text = psi.getText()!!.substring(lastPos)
listBuilder.add(#(text, psi))
listBuilder.add(Pair(text, psi))
}
}
fun splitPsi(psi: PsiElement): List<#(String, PsiElement)> {
val listBuilder = listBuilder<#(String, PsiElement)>()
fun splitPsi(psi: PsiElement): List<Pair(String, PsiElement)> {
val listBuilder = listBuilder<Pair(String, PsiElement)>()
splitPsiImpl(psi, listBuilder)
return listBuilder.build()
}
@@ -734,13 +734,13 @@ class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate
// TODO dirty hack - remove when this issue is fixed
// http://youtrack.jetbrains.com/issue/KT-1524
val hackedLinks = hashMap(
#("IllegalArgumentException", #("java.lang", "java/lang/IllegalArgumentException.html")),
#("IllegalStateException", #("java.lang", "java/lang/IllegalStateException.html")),
#("Map.Entry", #("java.util", "java/util/Map.Entry.html")),
#("System.in", #("java.lang", "java/lang/System.html#in")),
#("System.out", #("java.lang", "java/lang/System.html#in")),
#("#equals()", #("java.lang", "java/lang/Object.html#equals(java.lang.Object)")),
#("#hashCode()", #("java.lang", "java/lang/Object.html#hashCode()"))
Pair("IllegalArgumentException", Pair("java.lang", "java/lang/IllegalArgumentException.html")),
Pair("IllegalStateException", Pair("java.lang", "java/lang/IllegalStateException.html")),
Pair("Map.Entry", Pair("java.util", "java/util/Map.Entry.html")),
Pair("System.in", Pair("java.lang", "java/lang/System.html#in")),
Pair("System.out", Pair("java.lang", "java/lang/System.html#in")),
Pair("#equals()", Pair("java.lang", "java/lang/Object.html#equals(java.lang.Object)")),
Pair("#hashCode()", Pair("java.lang", "java/lang/Object.html#hashCode()"))
)
@@ -8,13 +8,13 @@ abstract class HtmlTemplate() : TextTemplate() {
tagName: String,
style: String? = null,
className: String? = null,
attributes: List<#(String, String)> = arrayList(),
attributes: List<Pair<String, String>> = arrayList(),
content: () -> Unit) {
val allAttributesBuilder = listBuilder<#(String, String)>()
val allAttributesBuilder = listBuilder<Pair<String, String>>()
if (style != null)
allAttributesBuilder.add(#("style", style))
allAttributesBuilder.add(Pair("style", style))
if (className != null)
allAttributesBuilder.add(#("class", className))
allAttributesBuilder.add(Pair("class", className))
// TODO: add addAll to ListBuilder
for (attribute in attributes)
@@ -55,7 +55,7 @@ abstract class HtmlTemplate() : TextTemplate() {
fun linkCssStylesheet(href: String) =
tag(
tagName = "link",
attributes = arrayList(#("rel", "stylesheet"), #("type", "text/css"), #("href", href))) {}
attributes = arrayList(Pair("rel", "stylesheet"), Pair("type", "text/css"), Pair("href", href))) {}
fun body(style: String? = null, className: String? = null, content: () -> Unit) =
tag(tagName = "body", style = style, className = className, content = content)
@@ -79,11 +79,11 @@ abstract class HtmlTemplate() : TextTemplate() {
tag(tagName = "span", style = style, className = className, content = content)
fun a(href: String? = null, name: String? = null, content: () -> Unit) {
val attributes = listBuilder<#(String, String)>()
val attributes = listBuilder<Pair<String, String>>()
if (href != null)
attributes.add(#("href", href))
attributes.add(Pair("href", href))
if (name != null)
attributes.add(#("name", name))
attributes.add(Pair("name", name))
tag(tagName = "a", attributes = attributes.build(), content = content)
}
}