This file contains 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
/** | |
* A generic class that describes data with a status | |
*/ | |
class Resource<T> constructor(val status: Status, var data: T? = null, val message: String? = null, val date: Date? = null) { | |
companion object { | |
fun <T> success(data: T?, date: Date?): Resource<T> { | |
return Resource(status = Status.SUCCESS, data = data, date = date) | |
} |
This file contains 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
class UserRepository (private val webservice: UserApi, private val userDao: UserDao, private val appExecutors: AppExecutors){ | |
fun loadUser(userId: Int) : LiveData<Resource<User>>{ | |
return object : NetworkBoundResource<User, User> (appExecutors) { | |
override fun loadFromDb(): User? { | |
return userDao.load(userId) | |
} | |
override fun getDataFetchDate(data: User?): Date? { | |
return data?.fetchDate |
This file contains 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
class MoveAction { | |
operator fun invoke(units: Int, direction: String) {...} | |
operator fun invoke(targetPoint: Point) {...} | |
} | |
val move = MoveAction() | |
val targetPoint = Point(90, 90) | |
move(5, "left") | |
move(targetPoint) |
This file contains 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
class SpaceShuttle { | |
operator fun invoke(units: Int, direction: String) {...} | |
operator fun invoke(targetPoint: Point) {...} | |
} | |
val spaceShuttle = SpaceShuttle() | |
val targetPoint = Point(90, 90) | |
spaceShuttle(5, "left") | |
spaceShuttle(targetPoint) |
This file contains 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
fun return1And2(): Pair<String, String> { | |
return "returnValue1" to "returnValue2" | |
} | |
val (r1, r2) = return1And2() |
This file contains 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
data class FlightPath(private val point1: Point, private val point2: Point){ | |
operator fun contains(point: Point): Boolean { | |
//Do some maths to determine if the point is on the flightpath | |
} | |
operator fun iterator(): Iterator<Point>{ | |
//Iterate over certain checkpoints in the flightpath | |
} | |
} | |
val spaceShuttlePosition = Point(10.0, 10.0) |
This file contains 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
class Point(private var x: Double, | |
private var y: Double) { | |
operator fun get(index: Int): Double { return if(index == 0) x else y } | |
operator fun set(index: Int, value: Double) { if(index == 0) x = value else y = value} | |
} | |
val spaceShuttlePosition = Point(10.0, 10.0) | |
//Determine the spaceshuttle's x coordinate using the index operator | |
spaceShuttlePosition[0] |
This file contains 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
data class Point(private val x: Double, | |
private val y: Double) { | |
operator fun minus(other: Point): Double {...} | |
operator fun compareTo(other: Point): Int { | |
val distanceToOrigin = this - Point(0.0, 0.0) | |
val otherDistanceToOrigin = other - Point(0.0, 0.0) | |
return when { | |
distanceToOrigin > otherDistanceToOrigin -> 1 | |
distanceToOrigin == otherDistanceToOrigin -> 0 | |
else -> -1 |
This file contains 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
data class Point(private val x: Double, | |
private val y: Double) { | |
operator fun minus(other: Point): Double { | |
//Pythagorian algorithm - not important here! | |
return sqrt(pow(x - other.x, 2.0) + pow(y - other.y, 2.0)) | |
} | |
} | |
val spaceStationPosition = Point(100.0, 100.0) | |
val spaceShuttlePosition = Point(10.0, 10.0) |
This file contains 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
class Memento(private val originalText: String) { | |
} | |
interface UndoCommand { | |
operator fun invoke() | |
} | |
class UndoTypingCommand(private val memento: Memento) : UndoCommand { | |
override operator fun invoke() { | |
//restore the state using the memento object |
OlderNewer