All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.greenrobot.daocompat.rx.RxDao Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2011-2017 Markus Junginger, greenrobot (http://greenrobot.org)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.greenrobot.daocompat.rx;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;

import org.greenrobot.daocompat.AbstractDao;
import io.objectbox.annotation.apihint.Experimental;
import rx.Observable;
import rx.Scheduler;

/**
 * Like {@link AbstractDao} but with Rx support. Most methods from AbstractDao are present here, but will return an
 * {@link Observable}. Modifying operations return the given entities, so they can be further processed in Rx.
 * 

* Instances of RxDao may have an default {@link rx.Scheduler}, which is used to configure returned observables with * {@link Observable#subscribeOn(Scheduler)} (see {@link AbstractDao#rxPlain()}, which uses the IO scheduler). * * Note: DO NOT call more than one data modification operation when you can use a transaction instead (see * {@link RxTransaction}. Individual calls use a transaction each and are much slower. * * @param Entity type * @param Primary key (PK) type; use Void if entity does not have exactly one PK * @see AbstractDao#rxPlain() */ @Experimental public class RxDao extends RxBase { private final AbstractDao dao; /** * Creates a new RxDao without a default scheduler. */ @Experimental public RxDao(AbstractDao dao) { this(dao, null); } /** * Creates a new RxDao with a default scheduler, which is used to configure returned observables with * {@link Observable#subscribeOn(Scheduler)}. */ @Experimental public RxDao(AbstractDao dao, Scheduler scheduler) { super(scheduler); this.dao = dao; } /** * Rx version of {@link AbstractDao#loadAll()} returning an Observable. */ @Experimental public Observable> loadAll() { return wrap(new Callable>() { @Override public List call() throws Exception { return dao.loadAll(); } }); } /** * Rx version of {@link AbstractDao#loadAll()} returning an Observable. */ @Experimental public Observable load(final Long key) { return wrap(new Callable() { @Override public T call() throws Exception { return dao.load(key); } }); } /** * Rx version of {@link AbstractDao#refresh(Object)} returning an Observable. * Note that the Observable will emit the given entity back to its subscribers. */ @Experimental public Observable refresh(final T entity) { return wrap(new Callable() { @Override public T call() throws Exception { dao.refresh(entity); return entity; } }); } /** * Rx version of {@link AbstractDao#insert(Object)} returning an Observable. * Note that the Observable will emit the given entity back to its subscribers. */ @Experimental public Observable insert(final T entity) { return wrap(new Callable() { @Override public T call() throws Exception { dao.insert(entity); return entity; } }); } /** * Rx version of {@link AbstractDao#insertInTx(Collection)} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable> insertInTx(final Collection entities) { return wrap(new Callable>() { @Override public Iterable call() throws Exception { dao.insertInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#insertInTx(Object[])} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable insertInTx(final T... entities) { return wrap(new Callable() { @Override public Object[] call() throws Exception { dao.insertInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#insertOrReplace(Object)} returning an Observable. * Note that the Observable will emit the given entity back to its subscribers. */ @Experimental public Observable insertOrReplace(final T entity) { return wrap(new Callable() { @Override public T call() throws Exception { dao.insertOrReplace(entity); return entity; } }); } /** * Rx version of {@link AbstractDao#insertOrReplaceInTx(Collection)} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable> insertOrReplaceInTx(final Collection entities) { return wrap(new Callable>() { @Override public Iterable call() throws Exception { dao.insertOrReplaceInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#insertOrReplaceInTx(Object[])} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable insertOrReplaceInTx(final T... entities) { return wrap(new Callable() { @Override public Object[] call() throws Exception { dao.insertOrReplaceInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#save(Object)} returning an Observable. * Note that the Observable will emit the given entity back to its subscribers. */ @Experimental public Observable save(final T entity) { return wrap(new Callable() { @Override public T call() throws Exception { dao.save(entity); return entity; } }); } /** * Rx version of {@link AbstractDao#saveInTx(Collection)} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable> saveInTx(final Collection entities) { return wrap(new Callable>() { @Override public Iterable call() throws Exception { dao.saveInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#saveInTx(Object[])} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable saveInTx(final T... entities) { return wrap(new Callable() { @Override public Object[] call() throws Exception { dao.saveInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#update(Object)} returning an Observable. * Note that the Observable will emit the given entity back to its subscribers. */ @Experimental public Observable update(final T entity) { return wrap(new Callable() { @Override public T call() throws Exception { dao.update(entity); return entity; } }); } /** * Rx version of {@link AbstractDao#updateInTx(Collection)} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable> updateInTx(final Collection entities) { return wrap(new Callable>() { @Override public Iterable call() throws Exception { dao.updateInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#updateInTx(Object[])} returning an Observable. * Note that the Observable will emit the given entities back to its subscribers. */ @Experimental public Observable updateInTx(final T... entities) { return wrap(new Callable() { @Override public Object[] call() throws Exception { dao.updateInTx(entities); return entities; } }); } /** * Rx version of {@link AbstractDao#delete(Object)} returning an Observable. */ @Experimental public Observable delete(final T entity) { return wrap(new Callable() { @Override public Void call() throws Exception { dao.delete(entity); return null; } }); } /** * Rx version of {@link AbstractDao#deleteByKey(Long)} returning an Observable. */ @Experimental public Observable deleteByKey(final Long key) { return wrap(new Callable() { @Override public Void call() throws Exception { dao.deleteByKey(key); return null; } }); } /** * Rx version of {@link AbstractDao#deleteAll()} returning an Observable. */ @Experimental public Observable deleteAll() { return wrap(new Callable() { @Override public Void call() throws Exception { dao.deleteAll(); return null; } }); } /** * Rx version of {@link AbstractDao#deleteInTx(Collection)} returning an Observable. */ @Experimental public Observable deleteInTx(final Collection entities) { return wrap(new Callable() { @Override public Void call() throws Exception { dao.deleteInTx(entities); return null; } }); } /** * Rx version of {@link AbstractDao#deleteInTx(Object[])} returning an Observable. */ @Experimental public Observable deleteInTx(final T... entities) { return wrap(new Callable() { @Override public Void call() throws Exception { dao.deleteInTx(entities); return null; } }); } /** * Rx version of {@link AbstractDao#deleteByKeyInTx(Collection)} returning an Observable. */ @Experimental public Observable deleteByKeyInTx(final Collection keys) { return wrap(new Callable() { @Override public Void call() throws Exception { dao.deleteByKeyInTx(keys); return null; } }); } /** * Rx version of {@link AbstractDao#deleteByKeyInTx(Long[])} returning an Observable. */ @Experimental public Observable deleteByKeyInTx(final Long... keys) { return wrap(new Callable() { @Override public Void call() throws Exception { dao.deleteByKeyInTx(keys); return null; } }); } /** * Rx version of {@link AbstractDao#count()} returning an Observable. */ @Experimental public Observable count() { return wrap(new Callable() { @Override public Long call() throws Exception { return dao.count(); } }); } /** * The plain DAO. */ @Experimental public AbstractDao getDao() { return dao; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy