Updating the Trip Sample

Back in the chapter on Room basics, we saw some rudimentary code to track upcoming travel. The Trips/RoomConverters sample project extends that code with four new fields on Trip:

Those latter two are largely ignored for the moment, though they will become more important later in the book.

The latter three are all Date fields, and so we need to have some code to support getting them into and out of our table. So, this project has a TypeTransmogrifier class, akin to the ones seen above, but right now only with the Date converters:

package com.commonsware.android.room;

import android.arch.persistence.room.TypeConverter;
import java.util.Date;

public class TypeTransmogrifier {
  @TypeConverter
  public static Long fromDate(Date date) {
    if (date==null) {
      return(null);
    }

    return(date.getTime());
  }

  @TypeConverter
  public static Date toDate(Long millisSinceEpoch) {
    if (millisSinceEpoch==null) {
      return(null);
    }

    return(new Date(millisSinceEpoch));
  }
}

priority, though, is an enum, as there is a list of valid values:

package com.commonsware.android.room;

import android.arch.persistence.room.TypeConverter;

enum Priority {
  LOW(0), MEDIUM(1), HIGH(2), OMG(3);

  private final int level;

  @TypeConverter
  public static Priority fromLevel(Integer level) {
    for (Priority p : values()) {
      if (p.level==level) {
        return(p);
      }
    }

    return(null);
  }

  @TypeConverter
  public static Integer fromPriority(Priority p) {
    return(p.level);
  }

  Priority(int level) {
    this.level=level;
  }
}

Here, we implement the @TypeConverter methods right on Priority, as there is little value in having them elsewhere. Note that the enum assigns explicit numeric values to the priorities (level). That way, we are in control over the mapping between Priority values and their representation in the database.

Rather than apply these type converters on the TripDatabase (though we could), we instead apply them on the Trip model:

package com.commonsware.android.room;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.arch.persistence.room.TypeConverters;
import android.support.annotation.NonNull;
import java.util.Date;
import java.util.UUID;

@Entity(tableName = "trips")
@TypeConverters({TypeTransmogrifier.class})
class Trip {
  @PrimaryKey
  @NonNull
  public final String id;

  public final String title;
  public final int duration;

  @TypeConverters({Priority.class})
  public final Priority priority;

  public final Date startTime;
  public final Date creationTime;
  public final Date updateTime;

  @Ignore
  Trip(String title, int duration, Priority priority, Date startTime) {
    this(UUID.randomUUID().toString(), title, duration, priority, startTime,
      null, null);
  }

  Trip(String id, String title, int duration, Priority priority,
       Date startTime, Date creationTime, Date updateTime) {
    this.id=id;
    this.title=title;
    this.duration=duration;
    this.priority=priority;
    this.startTime=startTime;
    this.creationTime=creationTime;
    this.updateTime=updateTime;
  }

  @Override
  public String toString() {
    return(title);
  }
}

The Priority type converters are applied specific to the priority field, as this specific conversion is only needed here. The TypeTransmogrifier is registered on the Trip class, as there are multiple Date fields.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.