From f3c99a7fcf46b93b5c0e874a0e55b8b114491b45 Mon Sep 17 00:00:00 2001 From: cleversonzanon Date: Fri, 7 Jul 2017 03:37:08 -0300 Subject: [PATCH] BAEL-974 - Cleverson Santos | cleverson.ssantos1008@gmail.com (#2213) * Different Types of Bean Injection in Spring code * Dataclasses in Kotlin * Revert "Different Types of Bean Injection in Spring code" This reverts commit 4b747726b93a9f6bf76d6518792fc77e0d5c2fc9. --- .../java/com/baeldung/dataclass/Movie.java | 88 +++++++++++++++++++ .../kotlin/com/baeldung/dataclass/Movie.kt | 3 + .../kotlin/com/baeldung/dataclass/Sandbox.kt | 26 ++++++ 3 files changed, 117 insertions(+) create mode 100644 kotlin/src/main/java/com/baeldung/dataclass/Movie.java create mode 100644 kotlin/src/main/kotlin/com/baeldung/dataclass/Movie.kt create mode 100644 kotlin/src/main/kotlin/com/baeldung/dataclass/Sandbox.kt diff --git a/kotlin/src/main/java/com/baeldung/dataclass/Movie.java b/kotlin/src/main/java/com/baeldung/dataclass/Movie.java new file mode 100644 index 0000000000..7eac98fe2a --- /dev/null +++ b/kotlin/src/main/java/com/baeldung/dataclass/Movie.java @@ -0,0 +1,88 @@ +package com.baeldung.dataclass; + +public class Movie { + + private String name; + private String studio; + private float rating; + + public Movie(String name, String studio, float rating) { + this.name = name; + this.studio = studio; + this.rating = rating; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getStudio() { + return studio; + } + + public void setStudio(String studio) { + this.studio = studio; + } + + public float getRating() { + return rating; + } + + public void setRating(float rating) { + this.rating = rating; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + Float.floatToIntBits(rating); + result = prime * result + ((studio == null) ? 0 : studio.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + + if (obj == null) + return false; + + if (getClass() != obj.getClass()) + return false; + + Movie other = (Movie) obj; + + if (name == null) { + if (other.name != null) + return false; + + } else if (!name.equals(other.name)) + return false; + + if (Float.floatToIntBits(rating) != Float.floatToIntBits(other.rating)) + return false; + + if (studio == null) { + if (other.studio != null) + return false; + + } else if (!studio.equals(other.studio)) + return false; + + return true; + } + + @Override + public String toString() { + return "Movie [name=" + name + ", studio=" + studio + ", rating=" + rating + "]"; + } +} \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/dataclass/Movie.kt b/kotlin/src/main/kotlin/com/baeldung/dataclass/Movie.kt new file mode 100644 index 0000000000..c0c15b2516 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/dataclass/Movie.kt @@ -0,0 +1,3 @@ +package com.baeldung.dataclass + +data class Movie(val name: String, val studio: String, var rating: Float) \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/dataclass/Sandbox.kt b/kotlin/src/main/kotlin/com/baeldung/dataclass/Sandbox.kt new file mode 100644 index 0000000000..d47909bf29 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/dataclass/Sandbox.kt @@ -0,0 +1,26 @@ +package com.baeldung.dataclass + +fun main(args: Array) { + + val movie = Movie("Whiplash", "Sony Pictures", 8.5F) + + println(movie.name) //Whiplash + println(movie.studio) //Sony Pictures + println(movie.rating) //8.5 + + movie.rating = 9F + + println(movie.toString()) //Movie(name=Whiplash, studio=Sony Pictures, rating=9.0) + + val betterRating = movie.copy(rating = 9.5F) + println(betterRating.toString()) //Movie(name=Whiplash, studio=Sony Pictures, rating=9.5) + + movie.component1() //name + movie.component2() //studio + movie.component3() //rating + + val(name, studio, rating) = movie + + fun getMovieInfo() = movie + val(namef, studiof, ratingf) = getMovieInfo() +} \ No newline at end of file