// Path: app/src/main/java/io/github/droidkaigi/confsched2017/model/MySession.java
// @Table
// public class MySession {
// 
//     @PrimaryKey
//     @Column(indexed = true)
//     @SerializedName("id")
//     public int id;
// 
//     @Column(indexed = true, unique = true)
//     @SerializedName("session")
//     public Session session;
// 
//     public MySession() {
//     }
// 
//     public MySession(@NonNull Session session) {
//         this.session = session;
//     }
// 
//     @Override
//     public boolean equals(Object o) {
//         return o instanceof MySession && ((MySession) o).id == id || super.equals(o);
//     }
// 
//     @Override
//     public int hashCode() {
//         return id;
//     }
// }


import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.droidkaigi.confsched2017.model.MySession;
import io.github.droidkaigi.confsched2017.model.Session;
import io.reactivex.Completable;
import io.reactivex.Single;
    public MySessionsRepository(MySessionsLocalDataSource localDataSource) {
        this.localDataSource = localDataSource;
        this.cachedMySessions = new LinkedHashMap<>();
    }

    @Override
    public Single<List<MySession>> findAll() {
        if (cachedMySessions != null && !cachedMySessions.isEmpty()) {
            return Single.create(emitter -> {
                emitter.onSuccess(new ArrayList<>(cachedMySessions.values()));
            });
        }

        return localDataSource.findAll().doOnSuccess(this::refreshCache);
    }

    @Override
    public Completable save(@NonNull Session session) {
        cachedMySessions.put(session.id, new MySession(session));
        return localDataSource.save(session);
    }

    @Override
    public Single<Integer> delete(@NonNull Session session) {
        cachedMySessions.remove(session.id);
        return localDataSource.delete(session);
    }

    @Override
    public boolean isExist(int sessionId) {
// Path: app/src/main/java/io/github/droidkaigi/confsched2017/model/MySession.java
// @Table
// public class MySession {
// 
//     @PrimaryKey
//     @Column(indexed = true)
//     @SerializedName("id")
//     public int id;
// 
//     @Column(indexed = true, unique = true)
//     @SerializedName("session")
//     public Session session;
// 
//     public MySession() {
//     }
// 
//     public MySession(@NonNull Session session) {
//         this.session = session;
//     }
// 
//     @Override
//     public boolean equals(Object o) {
//         return o instanceof MySession && ((MySession) o).id == id || super.equals(o);
//     }
// 
//     @Override
//     public int hashCode() {
//         return id;
//     }
// }


// Path: app/src/main/java/io/github/droidkaigi/confsched2017/repository/sessions/MySessionsRepository.java
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.droidkaigi.confsched2017.model.MySession;
import io.github.droidkaigi.confsched2017.model.Session;
import io.reactivex.Completable;
import io.reactivex.Single;

    public MySessionsRepository(MySessionsLocalDataSource localDataSource) {
        this.localDataSource = localDataSource;
        this.cachedMySessions = new LinkedHashMap<>();
    }

    @Override
    public Single<List<MySession>> findAll() {
        if (cachedMySessions != null && !cachedMySessions.isEmpty()) {
            return Single.create(emitter -> {
                emitter.onSuccess(new ArrayList<>(cachedMySessions.values()));
            });
        }

        return localDataSource.findAll().doOnSuccess(this::refreshCache);
    }

    @Override
    public Completable save(@NonNull Session session) {
        cachedMySessions.put(session.id, new MySession(session));
        return localDataSource.save(session);
    }

    @Override
    public Single<Integer> delete(@NonNull Session session) {
        cachedMySessions.remove(session.id);
        return localDataSource.delete(session);
    }

    @Override
    public boolean isExist(int sessionId) {
        return localDataSource.isExist(sessionId);