Created
May 16, 2018 18:58
-
-
Save javadude/8fbd0ab3bde75673e9990230d59c15c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aaa | |
import kotlin.reflect.KClass | |
import kotlin.reflect.full.createInstance | |
//open class Mammal(override val name: String) : Named | |
//class Cat(name: String) : Mammal(name) | |
// | |
//class List { | |
// fun get(n : Int) : Any? { return null} | |
// fun add(item : Any) {} | |
//} | |
//class CatList { | |
// fun get(n: Int): Cat? { | |
// return null | |
// } | |
// | |
// fun add(item: Cat) {} | |
//} | |
//class MammalList { | |
// fun get(n : Int) : Mammal? { return null} | |
// fun add(item : Mammal) {} | |
//} | |
// | |
//fun listTest() { | |
// val list = List() | |
// list.add(Cat()) | |
// list.add(Mammal()) | |
// val cat = list.get(0) | |
// cat as Cat | |
//} | |
// | |
//class GenericList<ITEM> { | |
// fun get(n : Int) : ITEM? { return null} | |
// fun add(item : ITEM) {} | |
//} | |
//fun listTest2() { | |
// val catList = GenericList<Cat>() | |
// val mammalList = GenericList<Mammal>() | |
// catList.add(Cat()) | |
// mammalList.add(Mammal()) | |
// val cat = catList.get(0) | |
// cat | |
//} | |
//fun vars1() { | |
// var list : GenericList<Mammal>? = null | |
// val catList = GenericList<Cat>() | |
// val mammalList = GenericList<Mammal>() | |
// list = mammalList | |
// list = catList | |
//} | |
// | |
//class NamedGenericList<ITEM : Named> { | |
// fun get(n : Int) : ITEM? { return null} | |
// fun add(item : ITEM) {} | |
// fun getName(n : Int) : String? {return get(n)?.name} | |
//} | |
// | |
// | |
interface Named { | |
var name : String? | |
} | |
// | |
//class List2<ITEM> { | |
// val data = mutableListOf<ITEM>() | |
// fun get(n : Int) : ITEM = data[n] | |
// fun add(item : ITEM) {data.add(item)} | |
//} | |
// | |
//interface Source<out ITEM> { | |
// fun next() : ITEM | |
//} | |
// | |
// | |
// | |
//fun sourceTest1(cats : Source<Cat>) { | |
// val objects : Source<Any> = cats | |
// val mammals : Source<Mammal> = cats | |
// val cats2 : Source<Cat> = cats | |
//} | |
//fun sourceTest2(mammals : Source<Mammal>) { | |
// val objects : Source<Any> = mammals | |
// val mammals2 : Source<Mammal> = mammals | |
// val cat : Source<Cat> = mammals | |
//} | |
//class Transporter<in ITEM> { | |
// fun transport(item : ITEM) {} | |
//// fun get() : ITEM? {return null} | |
//} | |
//fun transportTestUser() { | |
// transportTest(Transporter<Mammal>()) | |
//} | |
//fun transportTest(transporter: Transporter<Cat>) { | |
// transporter.transport(Cat("chewy")) | |
//} | |
//fun transportTest() { | |
// var mammalTransporter : Transporter<Mammal>? = null | |
// var catTransporter : Transporter<Cat>? = null | |
// var anyTransporter : Transporter<Any>? = null | |
// | |
//// { | |
// catTransporter = anyTransporter | |
// mammalTransporter = anyTransporter | |
// catTransporter = mammalTransporter | |
// mammalTransporter = anyTransporter | |
//// } | |
//// { | |
//// anyTransporter = catTransporter | |
//// } | |
//} | |
// | |
//fun <T> copy(from : List2<out T>, to : List2<in T>) { | |
//// from.add("") | |
//// val n = to.get(1) | |
//// n | |
//} | |
// | |
//fun <FROM : TO, TO> copy2(from : List2<FROM>, to : List2<TO>) { | |
//// from.add("") | |
//// val n = to.get(1) | |
//// n | |
//} | |
// | |
//fun copyTest() { | |
// val catList1 = List2<Cat>() | |
// val catList2 = List2<Cat>() | |
// val mammalList = List2<Mammal>() | |
// | |
// copy(catList1, catList2) | |
// copy(catList1, mammalList) | |
// copy2(catList1, mammalList) | |
//} | |
// | |
//interface Source2<out T> { | |
// fun next() : T | |
//} | |
//interface Source3<out T : Mammal> { | |
// fun next() : T | |
//} | |
//interface Transporter2<in T> { | |
// fun transport(item : T) | |
//} | |
//interface Transporter3<in T : Mammal> { | |
// fun transport(item : T) | |
//} | |
// | |
//fun starTest(source : Source2<*>) { | |
// var a = source.next() | |
// a | |
//} | |
//fun starTest2(source : Source3<*>) { | |
// var a = source.next() | |
// a | |
//} | |
//fun starTest3(transporter: Transporter2<*>) { | |
// transporter.transport(null) | |
//} | |
//fun starTest4(transporter: Transporter3<*>) { | |
// transporter.transport(null) | |
//} | |
//fun starTest4(list : List2<*>) { | |
// val n = list.get(0) | |
// n | |
// list.add("") | |
//} | |
open class ItineraryItem(val name : String, vararg val subItems : ItineraryItem) { | |
var parent : ItineraryItem? = null | |
init { | |
for(item in subItems) { | |
item.parent = this | |
} | |
} | |
} | |
class Trip(name : String, vararg subItems: ItineraryItem) : ItineraryItem(name, *subItems) | |
class City(name : String, vararg subItems: ItineraryItem) : ItineraryItem(name, *subItems) | |
class Hotel(name : String, vararg subItems: ItineraryItem) : ItineraryItem(name, *subItems) | |
class Car(name : String, vararg subItems: ItineraryItem) : ItineraryItem(name, *subItems) | |
fun <T:Any> parentOfType(type : KClass<T>, item :ItineraryItem) : T? { | |
var temp : ItineraryItem? = item | |
while (temp != null) { | |
if (type.isInstance(temp)) { | |
return temp as T | |
} | |
temp = temp.parent | |
} | |
return null | |
} | |
inline fun <reified T:Any> parentOfType2(item :ItineraryItem) : T? { | |
var temp : ItineraryItem? = item | |
while (temp != null) { | |
if (temp is T) { | |
return temp | |
} | |
temp = temp.parent | |
} | |
return null | |
} | |
inline fun <reified T : Named> create(name : String) : T { | |
return T::class.createInstance().apply { this.name = name } | |
} | |
class Person : Named { | |
override var name: String? = null | |
} | |
fun main(args : Array<String>) { | |
val hotel = Hotel("Doubletree") | |
val car = Car("National") | |
val city = City("Portland", hotel, car) | |
val trip = Trip("Visit Trevor", city) | |
val city2 = parentOfType(City::class, hotel) | |
println(city2?.name) | |
val city3 = parentOfType2<City>(hotel) | |
println(city3?.name) | |
create<Person>("Scott") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment