Update java examples

This commit is contained in:
Ilya Gorbunov
2015-09-22 21:31:39 +03:00
parent 60c2f0fe0b
commit ed2b118fc8
3 changed files with 77 additions and 1 deletions
@@ -0,0 +1,46 @@
package test.kotlin.jtests;
import junit.framework.TestCase;
import kotlin.*;
import kotlin.jvm.functions.Function1;
import java.util.Collection;
import java.util.List;
import static kotlin.CollectionsKt.*;
import static kotlin.ArraysKt.*;
/**
* Lets try using the Kotlin standard library from Java code
*/
public class ArraysTest extends TestCase {
public void testArrays() throws Exception {
String[] array = {"foo", "bar", "x"};
String text = joinToString(array, ",", "(", ")", -1, "...", null);
System.out.println("Have text: " + text);
assertEquals("(foo,bar,x)", text);
Collection<String> actual = filter(array, new Function1<String, Boolean>() {
@Override
public Boolean invoke(String text) {
return text.length() > 1;
}
});
int[] actualArray = toIntArray(map(actual, new Function1<String, Integer>() {
@Override
public Integer invoke(String s) {
return s.length();
}
}));
System.out.println("Filtered list from array is " + actual);
assertEquals("(3,3)", joinToString(actualArray, ",", "(", ")", -1, "...", null));
int single = single(distinct(actualArray));
assertEquals(3, single);
}
}
@@ -6,7 +6,7 @@ import kotlin.jvm.functions.Function1;
import java.util.Collection;
import java.util.List;
import static kotlin.KotlinPackage.*;
import static kotlin.CollectionsKt.*;
/**
* Lets try using the Kotlin standard library from Java code
@@ -0,0 +1,30 @@
package test.kotlin.jtests;
import junit.framework.TestCase;
import java.util.*;
import kotlin.*;
import kotlin.jvm.functions.Function0;
public class QualifiedNamesTest extends TestCase {
public void testQualified() throws Exception {
List<Object> items = CollectionsKt.plus(CollectionsKt.plus(Collections.emptyList(), "item"), 1);
Set<Object> set = SetsKt.<Object>setOf("a", "b", "c");
assertTrue(Collections.disjoint(items, set));
}
public void testLazy() throws Exception {
Lazy<Random> randomLazy = LazyKt.lazy(new Function0<Random>() {
@Override
public Random invoke() {
return new Random();
}
});
assertFalse(randomLazy.isInitialized());
Lazy<Random> initializedLazy = LazyKt.lazyOf(randomLazy.getValue());
assertTrue(initializedLazy.isInitialized());
assertTrue(randomLazy.isInitialized());
}
}