|
| 1 | +package com.baeldung.kotlin |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import kotlin.test.assertEquals |
| 5 | +import kotlin.test.assertTrue |
| 6 | + |
| 7 | +class GenericsTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + fun givenParametrizeClass_whenInitializeItWithSpecificType_thenShouldBeParameterized() { |
| 11 | + //given |
| 12 | + val parameterizedClass = ParameterizedClass<String>("string-value") |
| 13 | + |
| 14 | + //when |
| 15 | + val res = parameterizedClass.getValue() |
| 16 | + |
| 17 | + //then |
| 18 | + assertTrue(res is String) |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun givenParametrizeClass_whenInitializeIt_thenShouldBeParameterizedByInferredType() { |
| 23 | + //given |
| 24 | + val parameterizedClass = ParameterizedClass("string-value") |
| 25 | + |
| 26 | + //when |
| 27 | + val res = parameterizedClass.getValue() |
| 28 | + |
| 29 | + //then |
| 30 | + assertTrue(res is String) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun givenParameterizedProducerByOutKeyword_whenGetValue_thenCanAssignItToSuperType() { |
| 35 | + //given |
| 36 | + val parameterizedProducer = ParameterizedProducer("string") |
| 37 | + |
| 38 | + //when |
| 39 | + val ref: ParameterizedProducer<Any> = parameterizedProducer |
| 40 | + |
| 41 | + //then |
| 42 | + assertTrue(ref is ParameterizedProducer<Any>) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun givenParameterizedConsumerByInKeyword_whenGetValue_thenCanAssignItToSubType() { |
| 47 | + //given |
| 48 | + val parameterizedConsumer = ParameterizedConsumer<Number>() |
| 49 | + |
| 50 | + //when |
| 51 | + val ref: ParameterizedConsumer<Double> = parameterizedConsumer |
| 52 | + |
| 53 | + //then |
| 54 | + assertTrue(ref is ParameterizedConsumer<Double>) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun givenTypeProjections_whenOperateOnTwoList_thenCanAcceptListOfSubtypes() { |
| 59 | + //given |
| 60 | + val ints: Array<Int> = arrayOf(1, 2, 3) |
| 61 | + val any: Array<Any?> = arrayOfNulls(3) |
| 62 | + |
| 63 | + //when |
| 64 | + copy(ints, any) |
| 65 | + |
| 66 | + //then |
| 67 | + assertEquals(any[0], 1) |
| 68 | + assertEquals(any[1], 2) |
| 69 | + assertEquals(any[2], 3) |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + fun copy(from: Array<out Any>, to: Array<Any?>) { |
| 74 | + assert(from.size == to.size) |
| 75 | + for (i in from.indices) |
| 76 | + to[i] = from[i] |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun givenTypeProjection_whenHaveArrayOfIn_thenShouldAddElementsOfSubtypesToIt() { |
| 81 | + //given |
| 82 | + val objects: Array<Any?> = arrayOfNulls(1) |
| 83 | + |
| 84 | + //when |
| 85 | + fill(objects, 1) |
| 86 | + |
| 87 | + //then |
| 88 | + assertEquals(objects[0], 1) |
| 89 | + } |
| 90 | + |
| 91 | + fun fill(dest: Array<in Int>, value: Int) { |
| 92 | + dest[0] = value |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun givenStartProjection_whenPassAnyType_thenCompile() { |
| 97 | + //given |
| 98 | + val array = arrayOf(1,2,3) |
| 99 | + |
| 100 | + //then |
| 101 | + printArray(array) |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + fun printArray(array: Array<*>) { |
| 106 | + array.forEach { println(it) } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun givenFunctionWithDefinedGenericConstraints_whenCallWithProperType_thenCompile(){ |
| 111 | + //given |
| 112 | + val listOfInts = listOf(5,2,3,4,1) |
| 113 | + |
| 114 | + //when |
| 115 | + val sorted = sort(listOfInts) |
| 116 | + |
| 117 | + //then |
| 118 | + assertEquals(sorted[0], 1) |
| 119 | + assertEquals(sorted[1], 2) |
| 120 | + assertEquals(sorted[2], 3) |
| 121 | + assertEquals(sorted[3], 4) |
| 122 | + assertEquals(sorted[4], 5) |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + fun <T: Comparable<T>> sort(list: List<T>): List<T>{ |
| 127 | + return list.sorted() |
| 128 | + } |
| 129 | + |
| 130 | + class ParameterizedClass<A>(private val value: A) { |
| 131 | + |
| 132 | + fun getValue(): A { |
| 133 | + return value |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + class ParameterizedProducer<out T>(private val value: T) { |
| 138 | + fun get(): T { |
| 139 | + return value |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + class ParameterizedConsumer<in T> { |
| 144 | + fun toString(value: T): String { |
| 145 | + return value.toString() |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments