Skip to content

Instantly share code, notes, and snippets.

@FrantisekGazo
Created March 16, 2016 15:15
Show Gist options
  • Save FrantisekGazo/2cd407f639438e3f52f2 to your computer and use it in GitHub Desktop.
Save FrantisekGazo/2cd407f639438e3f52f2 to your computer and use it in GitHub Desktop.
package eu.f3rog.db;
import android.support.annotation.Nullable;
import com.j256.ormlite.field.DatabaseField;
/**
* Class {@link CompositeIdObject}.
*
* @author Frantisek Gazo
* @version 2015-09-09
*/
public abstract class CompositeIdObject {
public static final class Comparator {
public static boolean equals(CompositeIdObject cio1, CompositeIdObject cio2) {
if (cio1 == null || cio2 == null || cio1.getDatabaseId() == null || cio2.getDatabaseId() == null)
return false;
return cio1.getDatabaseId().equals(cio2.getDatabaseId());
}
}
public static final String COLUMN_DATABASE_ID = "database_id";
@DatabaseField(id = true, useGetSet = true, persisterClass = CompositeIdFieldPersister.class, columnName = COLUMN_DATABASE_ID)
private transient CompositeId databaseId;
@Nullable
public CompositeId getDatabaseId() {
if (databaseId == null) {
databaseId = composeDatabaseId();
}
return databaseId;
}
public void refreshDatabaseId() {
databaseId = composeDatabaseId();
}
@Nullable
public String getDatabaseIdPart(@CompositeId.Tag String idTag) {
return (getDatabaseId() != null) ? getDatabaseId().getId(idTag) : null;
}
@Nullable
public Long getDatabaseIdPartLong(@CompositeId.Tag String idTag) {
return (getDatabaseId() != null) ? getDatabaseId().getLongId(idTag) : null;
}
@Nullable
public String getDatabaseIdString() {
return (getDatabaseId() != null) ? getDatabaseId().toString() : null;
}
public boolean hasDatabaseIdPart(@CompositeId.Tag String idTag) {
return (getDatabaseId() != null) && getDatabaseId().containsIdFor(idTag);
}
public void setDatabaseId(CompositeId databaseId) {
this.databaseId = databaseId;
}
public void setDatabaseIdString(String databaseIdString) {
this.databaseId = CompositeId.Builder.buildFrom(databaseIdString);
}
protected abstract CompositeId composeDatabaseId();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment