Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin & Modern Libraries

Kotlin & Modern Libraries

Takuji Nishibayashi

February 24, 2016
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. Java @Module
 public class AppModule {
 private final Context context;


    public AppModule(Context context) {
 this.context = context;
 }
 
 @Provides
 public Context context() {
 return context;
 }
 
 @Provides
 public NotificationManager notificationManager(Context context) {
 return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
 }
 }

  2. Kotlin (Provider methods) @Module
 class AppModule(
 val context: Context
 )

    {
 @Provides fun context() : Context = context
 @Provides fun notificationManager(context: Context) : NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
 }
  3. Kotlin (Constructor property) @Module
 class AppModule(
 @get:Provides val context: Context


    ) {
 @Provides fun notificationManager(context: Context) : NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
 }
  4. Kotlin (Constructor property) @Module
 class AppModule(
 @get:Provides val context: Context,


    @get:Provides val notificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
 ) {
 }
  5. Java public class User extends RealmObject {
 @PrimaryKey private String

    uuid;
 private String name;
 
 public User() {
 this(UUID.randomUUID().toString(), null);
 }
 
 public User(String uuid, String name) {
 this.uuid = uuid;
 this.name = name;
 }
 
 public String getUuid() {return uuid;}
 public void setUuid(String uuid) {this.uuid = uuid;}
 public String getName() {return name;}
 public void setName(String name) {this.name = name;}
 }

  6. Kotlin (default constructor) class User constructor() : RealmObject() {
 @PrimaryKey


    var uuid: String? = UUID.randomUUID().toString()
 var name: String? = null
 }

  7. Kotlin (with default value) open class User(
 @PrimaryKey open var

    uuid: String = UUID.randomUUID().toString(),
 open var name: String? = null
 ) : RealmObject() {}
  8. Java public class User extends RealmObject {
 private Date createdDate;


    @Ignore private Instant createdAt;
 
 public Date getCreatedDate() {return createdDate;}
 public void setCreatedDate(Date createdDate) {this.createdDate = createdDate;}
 public Instant getCreatedAt() {
 return DateTimeUtils.toInstant(getCreatedDate());
 }
 public void setCreatedAt(Instant createdAt) {
 setCreatedDate(DateTimeUtils.toDate(createdAt));
 }
 }
  9. Kotlin (extension property) open class User( …
 open var createdDate:

    Date? = null
 ) : RealmObject() {} var User.createdAt : Instant
 get() = DateTimeUtils.toInstant(createdDate)
 set(value) {
 createdDate = DateTimeUtils.toDate(value)
 }