Skip to content

Instantly share code, notes, and snippets.

@wispborne
Last active August 17, 2020 19:42
Show Gist options
  • Save wispborne/5d7bde79a3967b27d9e25a74bccfdad7 to your computer and use it in GitHub Desktop.
Save wispborne/5d7bde79a3967b27d9e25a74bccfdad7 to your computer and use it in GitHub Desktop.

Revisions

  1. wispborne revised this gist Mar 4, 2019. 2 changed files with 12 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions copy.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    public Person cloneWithNewName(Person original, String newName) {
    return new Person(
    original.id,
    newName,
    original.role,
    original.gender,
    original.seatingPosition,
    original.title
    )
    }
    2 changes: 2 additions & 0 deletions copy.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    fun cloneWithNewName(orignal: Person, newName: String): Person =
    original.copy(name = newName)
  2. wispborne revised this gist Mar 23, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions KotlinComparisonClass.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /** Example of a {@link Class} declaration */
    final class MyClass {
    public final class MyClass {
    private boolean someBool;

    public MyClass() {
    @@ -13,7 +13,7 @@ public boolean getSomeBool() {
    return someBool;
    }

    protected void setSomeBool(@NonNull boolean value) {
    public void setSomeBool(@NonNull boolean value) {
    someBool = value;
    }

  3. wispborne revised this gist Sep 2, 2016. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  4. wispborne revised this gist Jun 1, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions MyClass.java
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    final class MyClass {
    private boolean someBool;

    public MyClass() {
    }

    public MyClass(boolean someBool) {
    this.someBool = someBool;
    }
  5. wispborne revised this gist May 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rxJava.kt
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@ fun printRxJavaStuff() {
    Observable.from(asList("one fish", "two fish", "red fish", "blue fish"))
    .filter { it.contains("one") || it.contains("two") }
    .map { it.replace("fish", "dish") }
    .subscribe( { Timber.v(s) } ) // First prints "one dish", then "two dish"
    .subscribe( { Timber.v(s) } )
    }
  6. wispborne revised this gist May 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _Java_vs_Kotlin.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    Comparison of Java to Kotlin for various simple code snippets.
    Comparison of Java 7 to Kotlin for various simple code snippets.
    Files with the same name do the same thing.
  7. wispborne revised this gist May 20, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions MyClass.java
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ public String toString() {
    return "someBool:" + someBool;
    }

    // .copy override
    // .hashcode override
    // .equals override
    // not shown: .copy override
    // not shown: .hashcode override
    // not shown: .equals override
    }
  8. wispborne revised this gist May 20, 2016. 4 changed files with 31 additions and 25 deletions.
    2 changes: 1 addition & 1 deletion filter.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /** Returns a subset of {@code list} containing only the items where {@code someBool == true} */
    public List<MyClass> filterList(List<MyClass> list) {
    public final List<MyClass> filterList(List<MyClass> list) {
    List<MyClass> ret = new List<>(list.size());

    for(MyClass item : list) {
    2 changes: 1 addition & 1 deletion nullability.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /** Gets the second character in a string or, if not possible, returns some default value (which may be {@code null}) */
    @Nullable
    public String getSecondCharOrDefault(@NonNull String str, @Nullable String defaultVal) {
    public final String getSecondCharOrDefault(@NonNull String str, @Nullable String defaultVal) {
    return str.length() > 2 ? str.get(1) : defaultVal;
    }
    41 changes: 22 additions & 19 deletions rxJava.java
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,22 @@
    Observable.from(Arrays.asList("one fish", "two fish", "red fish", "blue fish"))
    .filter(new Func1<String, Boolean>() {
    @Override
    public Boolean call(String s) {
    return s.contains("one") || s.contains("two");
    }
    })
    .map(new Func1<String, String>() {
    @Override
    public String call(String s) {
    return s.replace("fish", "dish");
    }
    })
    .subscribe(new Action1<String>() {
    @Override
    public void call(String s) {
    Timber.v(s); // First prints "one dish", then "two dish"
    }
    });
    /** First prints "one dish", then "two dish" */
    public final void printRxJavaStuff() {
    Observable.from(Arrays.asList("one fish", "two fish", "red fish", "blue fish"))
    .filter(new Func1<String, Boolean>() {
    @Override
    public Boolean call(String s) {
    return s.contains("one") || s.contains("two");
    }
    })
    .map(new Func1<String, String>() {
    @Override
    public String call(String s) {
    return s.replace("fish", "dish");
    }
    })
    .subscribe(new Action1<String>() {
    @Override
    public void call(String s) {
    Timber.v(s);
    }
    });
    }
    11 changes: 7 additions & 4 deletions rxJava.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    Observable.from(asList("one fish", "two fish", "red fish", "blue fish"))
    .filter { it.contains("one") || it.contains("two") }
    .map { it.replace("fish", "dish") }
    .subscribe( { Timber.v(s) } ) // First prints "one dish", then "two dish"
    /** First prints "one dish", then "two dish" */
    fun printRxJavaStuff() {
    Observable.from(asList("one fish", "two fish", "red fish", "blue fish"))
    .filter { it.contains("one") || it.contains("two") }
    .map { it.replace("fish", "dish") }
    .subscribe( { Timber.v(s) } ) // First prints "one dish", then "two dish"
    }
  9. wispborne revised this gist May 20, 2016. 2 changed files with 23 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions rxJava.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    Observable.from(Arrays.asList("one fish", "two fish", "red fish", "blue fish"))
    .filter(new Func1<String, Boolean>() {
    @Override
    public Boolean call(String s) {
    return s.contains("one") || s.contains("two");
    }
    })
    .map(new Func1<String, String>() {
    @Override
    public String call(String s) {
    return s.replace("fish", "dish");
    }
    })
    .subscribe(new Action1<String>() {
    @Override
    public void call(String s) {
    Timber.v(s); // First prints "one dish", then "two dish"
    }
    });
    4 changes: 4 additions & 0 deletions rxJava.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    Observable.from(asList("one fish", "two fish", "red fish", "blue fish"))
    .filter { it.contains("one") || it.contains("two") }
    .map { it.replace("fish", "dish") }
    .subscribe( { Timber.v(s) } ) // First prints "one dish", then "two dish"
  10. wispborne revised this gist May 17, 2016. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions nullability.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    /** Gets the second character in a string or, if not possible, returns some default value (which may be {@code null}) */
    @Nullable
    public String getSecondCharOrDefault(@NonNull String str, @Nullable String defaultVal) {
    return str.length() > 2 ? str.get(1) : defaultVal;
    1 change: 1 addition & 0 deletions nullability.kt
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    /** Gets the second character in a string or, if not possible, returns some default value (which may be `null`) */
    fun getSecondCharOrDefault(str: String, defaultVal: String?) = if(str.length > 2) str[1] else defaultVal
  11. wispborne revised this gist May 17, 2016. 2 changed files with 5 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions nullability.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    @Nullable
    public String getSecondCharOrDefault(@NonNull String str, @Nullable String defaultVal) {
    return str.length() > 2 ? str.get(1) : defaultVal;
    }
    1 change: 1 addition & 0 deletions nullability.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    fun getSecondCharOrDefault(str: String, defaultVal: String?) = if(str.length > 2) str[1] else defaultVal
  12. wispborne revised this gist May 17, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions _Java_vs_Kotlin.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    Comparison of Java to Kotlin for various simple methods.
    Both files do exactly the same thing.
    Comparison of Java to Kotlin for various simple code snippets.
    Files with the same name do the same thing.
  13. wispborne revised this gist May 17, 2016. 5 changed files with 8 additions and 10 deletions.
    5 changes: 0 additions & 5 deletions java.kt → filter.java
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,4 @@ public List<MyClass> filterList(List<MyClass> list) {
    }

    return ret;
    }

    /** Returns {@code "yes"} if {@code value == true} or {@code "no"} if {@code value == false}. */
    public boolean yesOrNo(boolean value) {
    return value ? "yes" : "no";
    }
    2 changes: 2 additions & 0 deletions filter.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }
    5 changes: 0 additions & 5 deletions kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }

    /** Returns `"yes"` if `value == true` or `"no"` if `value == false`. */
    fun yesOrNo(value: Boolean) = if(value) "yes" else "no"
    4 changes: 4 additions & 0 deletions yesOrNo.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    /** Returns {@code "yes"} if {@code value == true} or {@code "no"} if {@code value == false}. */
    public boolean yesOrNo(boolean value) {
    return value ? "yes" : "no";
    }
    2 changes: 2 additions & 0 deletions yesOrNo.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    /** Returns `"yes"` if `value == true` or `"no"` if `value == false`. */
    fun yesOrNo(value: Boolean) = if(value) "yes" else "no"
  14. wispborne revised this gist May 17, 2016. 4 changed files with 27 additions and 29 deletions.
    25 changes: 25 additions & 0 deletions MyClass.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    /** Example of a {@link Class} declaration */
    final class MyClass {
    private boolean someBool;

    public MyClass(boolean someBool) {
    this.someBool = someBool;
    }

    public boolean getSomeBool() {
    return someBool;
    }

    protected void setSomeBool(@NonNull boolean value) {
    someBool = value;
    }

    @Override
    public String toString() {
    return "someBool:" + someBool;
    }

    // .copy override
    // .hashcode override
    // .equals override
    }
    2 changes: 2 additions & 0 deletions MyClass.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    /** Example of a [Class] declaration. */
    data class MyClass(var someBool: Boolean = false)
    26 changes: 0 additions & 26 deletions java.kt
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,3 @@
    /** Example of a {@link Class} declaration */
    final class MyClass {
    private boolean someBool;

    public MyClass(boolean someBool) {
    this.someBool = someBool;
    }

    public boolean getSomeBool() {
    return someBool;
    }

    protected void setSomeBool(@NonNull boolean value) {
    someBool = value;
    }

    @Override
    public String toString() {
    return "someBool:" + someBool;
    }

    // .copy override
    // .hashcode override
    // .equals override
    }

    /** Returns a subset of {@code list} containing only the items where {@code someBool == true} */
    public List<MyClass> filterList(List<MyClass> list) {
    List<MyClass> ret = new List<>(list.size());
    3 changes: 0 additions & 3 deletions kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    /** Example of a [Class] declaration. */
    data class MyClass(var someBool: Boolean = false)

    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }

  15. wispborne revised this gist May 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion java.kt
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ public List<MyClass> filterList(List<MyClass> list) {
    return ret;
    }

    /** Returns {@code "yes"} if {@code value == true} or {@code "no"} if {@code value == false}.
    /** Returns {@code "yes"} if {@code value == true} or {@code "no"} if {@code value == false}. */
    public boolean yesOrNo(boolean value) {
    return value ? "yes" : "no";
    }
  16. wispborne revised this gist May 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -4,5 +4,5 @@ data class MyClass(var someBool: Boolean = false)
    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }

    /** Returns `"yes"` if `value == true` or `"no"` if `value == false`.
    /** Returns `"yes"` if `value == true` or `"no"` if `value == false`. */
    fun yesOrNo(value: Boolean) = if(value) "yes" else "no"
  17. wispborne revised this gist May 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /** Example of a [Class] declaration.
    /** Example of a [Class] declaration. */
    data class MyClass(var someBool: Boolean = false)

    /** Returns a subset of `list` containing only the items where `someBool == true` */
  18. wispborne revised this gist May 17, 2016. 2 changed files with 11 additions and 1 deletion.
    6 changes: 6 additions & 0 deletions java.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    /** Example of a {@link Class} declaration */
    final class MyClass {
    private boolean someBool;

    @@ -34,4 +35,9 @@ public List<MyClass> filterList(List<MyClass> list) {
    }

    return ret;
    }

    /** Returns {@code "yes"} if {@code value == true} or {@code "no"} if {@code value == false}.
    public boolean yesOrNo(boolean value) {
    return value ? "yes" : "no";
    }
    6 changes: 5 additions & 1 deletion kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    /** Example of a [Class] declaration.
    data class MyClass(var someBool: Boolean = false)
    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }
    /** Returns `"yes"` if `value == true` or `"no"` if `value == false`.
    fun yesOrNo(value: Boolean) = if(value) "yes" else "no"
  19. wispborne revised this gist May 16, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion java.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    class MyClass {
    final class MyClass {
    private boolean someBool;

    public MyClass(boolean someBool) {
    @@ -16,6 +16,11 @@ class MyClass {
    @Override
    public String toString() {
    return "someBool:" + someBool;
    }

    // .copy override
    // .hashcode override
    // .equals override
    }

    /** Returns a subset of {@code list} containing only the items where {@code someBool == true} */
  20. wispborne revised this gist May 16, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion _Java_vs_Kotlin.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    Comparison of Java to Kotlin for various simple methods.
    Comparison of Java to Kotlin for various simple methods.
    Both files do exactly the same thing.
  21. wispborne revised this gist May 16, 2016. 2 changed files with 22 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions java.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,23 @@
    class MyClass {
    private boolean someBool;

    public MyClass(boolean someBool) {
    this.someBool = someBool;
    }

    public boolean getSomeBool() {
    return someBool;
    }

    protected void setSomeBool(@NonNull boolean value) {
    someBool = value;
    }

    @Override
    public String toString() {
    return "someBool:" + someBool;
    }

    /** Returns a subset of {@code list} containing only the items where {@code someBool == true} */
    public List<MyClass> filterList(List<MyClass> list) {
    List<MyClass> ret = new List<>(list.size());
    2 changes: 2 additions & 0 deletions kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,4 @@
    data class MyClass(var someBool: Boolean = false)

    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }
  22. wispborne revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { it.someBool }
    fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool }
  23. wispborne revised this gist May 16, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion java.kt
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,12 @@
    /** Returns a subset of {@code list} containing only the items where {@code someBool == true} */
    public List<MyClass> filterList(List<MyClass> list) {
    List<MyClass> ret = new List<>(list.size());

    for(MyClass item : list) {
    if(item.someBool) {
    ret.add(item);
    ret.add(item);
    }
    }

    return ret;
    }
  24. wispborne renamed this gist May 16, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  25. wispborne revised this gist May 16, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions _ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Comparison of Java to Kotlin for various simple methods.
  26. wispborne revised this gist May 16, 2016. No changes.
  27. wispborne revised this gist May 16, 2016. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions java.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    /** Returns a subset of {@code list} containing only the items where {@code someBool == true} */
    public List<MyClass> filterList(List<MyClass> list) {
    List<MyClass> ret = new List<>(list.size());
    for(MyClass item : list) {
    1 change: 1 addition & 0 deletions kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    /** Returns a subset of `list` containing only the items where `someBool == true` */
    fun filterList(items:List<MyClass>) = items.filter { it.someBool }
  28. wispborne created this gist May 16, 2016.
    9 changes: 9 additions & 0 deletions java.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    public List<MyClass> filterList(List<MyClass> list) {
    List<MyClass> ret = new List<>(list.size());
    for(MyClass item : list) {
    if(item.someBool) {
    ret.add(item);
    }
    }
    return ret;
    }
    1 change: 1 addition & 0 deletions kotlin.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    fun filterList(items:List<MyClass>) = items.filter { it.someBool }