Last active
December 12, 2016 16:47
-
-
Save tmtrademarked/d4dc376f1e9397445d0b2aaa211971f8 to your computer and use it in GitHub Desktop.
Portal example
This file contains hidden or 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
package com.blueapron.service.cache; | |
import com.blueapron.service.models.NetworkModel; | |
import com.blueapron.service.models.network.JsonModelConverter; | |
import java.io.Closeable; | |
import java.util.Collection; | |
import java.util.List; | |
import io.realm.Realm; | |
import io.realm.RealmModel; | |
import io.realm.RealmQuery; | |
/** | |
* Helper class to wrap a Realm object. This allows us to mock methods of the Realm to stub out | |
* functionality as necessary. | |
*/ | |
public class Portal implements Closeable { | |
private final Realm mRealm; | |
public Portal(Realm realm) { | |
mRealm = realm; | |
} | |
public void beginTransaction() { | |
mRealm.beginTransaction(); | |
} | |
public void commitTransaction() { | |
mRealm.commitTransaction(); | |
} | |
public <T extends RealmModel> PortalQuery<T> where(Class<T> clazz) { | |
return new PortalQuery<T>(mRealm.where(clazz)); | |
} | |
public void insertOrUpdate(RealmModel object) { | |
mRealm.insertOrUpdate(object); | |
} | |
public void insertOrUpdate(Collection<? extends RealmModel> objects) { | |
mRealm.insertOrUpdate(objects); | |
} | |
public <T extends RealmModel> T copyOrUpdate(T object) { | |
return mRealm.copyToRealmOrUpdate(object); | |
} | |
@Override | |
public void close() { | |
mRealm.close(); | |
} | |
public static class PortalQuery<T extends RealmModel> { | |
private final RealmQuery<T> mQuery; | |
PortalQuery(RealmQuery<T> query) { | |
mQuery = query; | |
} | |
public PortalQuery<T> equalTo(String key, String value) { | |
mQuery.equalTo(key, value); | |
return this; | |
} | |
public PortalQuery<T> equalTo(String key, boolean value) { | |
mQuery.equalTo(key, value); | |
return this; | |
} | |
public PortalQuery<T> equalTo(String key, int value) { | |
mQuery.equalTo(key, value); | |
return this; | |
} | |
public PortalQuery<T> in(String key, String[] values) { | |
mQuery.in(key, values); | |
return this; | |
} | |
public T findFirst() { | |
return mQuery.findFirst(); | |
} | |
public void findAndDeleteAll() { | |
mQuery.findAll().deleteAllFromRealm(); | |
} | |
// TODO - these should really return RealmResults, but using List makes it mockable. | |
public List<T> findAll() { | |
return mQuery.findAll(); | |
} | |
public List<T> findAllSorted(String fieldName) { | |
return mQuery.findAllSorted(fieldName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment