Migration implements Comparable<Migration> { @Override public int compareTo(@NonNull Migration o) { if (o.oldVersion < this.oldVersion) { return 1; } else if (o.oldVersion == this.oldVersion && o.newVersion > this.newVersion) { return 1; } else if (o.oldVersion == this.oldVersion && o.newVersion == this.newVersion) { return 0; } else { return -1; } } Migration(int oldVersion, int newVersion); @Override int compareTo(@NonNull Migration o); @Override boolean equals(Object o); @Override int hashCode(); abstract void migrate(SQLiteDatabase database); final int oldVersion; final int newVersion; }
@Test public void compareTo() throws Exception { List<Migration> migrations = new ArrayList<>(); migrations.add(new MigrationStub(2, 3)); migrations.add(new MigrationStub(1, 2)); migrations.add(new MigrationStub(1, 3)); migrations.add(new MigrationStub(3, 4)); Collections.sort(migrations); assertThat(migrations.get(0), equalTo(new MigrationStub(1, 3))); assertThat(migrations.get(1), equalTo(new MigrationStub(1, 2))); assertThat(migrations.get(2), equalTo(new MigrationStub(2, 3))); assertThat(migrations.get(3), equalTo(new MigrationStub(3, 4))); }