// Path: 2016-07-12_rxjava_in_action/src/main/java/com/androidhuman/example/rxjavainaction/error/InsufficientBalanceError.java
// public final class InsufficientBalanceError extends Error {
// 
//     private int amount;
// 
//     public InsufficientBalanceError(int amount) {
//         this.amount = amount;
//     }
// 
//     public int getAmount() {
//         return amount;
//     }
// }


import com.androidhuman.example.rxjavainaction.error.InsufficientBalanceError;
import com.androidhuman.example.rxjavainaction.model.Content;
import com.androidhuman.example.rxjavainaction.model.User;
import rx.Observable;
import rx.Subscriber;
package com.androidhuman.example.rxjavainaction.rx;



final class ChecksBalanceOnSubscribe implements Observable.OnSubscribe<Void> {

    private final Content content;

    private final User user;

    ChecksBalanceOnSubscribe(Content content, User user) {
        this.content = content;
        this.user = user;
    }

    @Override
    public void call(Subscriber<? super Void> subscriber) {
        if (!subscriber.isUnsubscribed()) {
            if (content.getPrice() <= user.getBalance()) {
                // Can purchase a content
                subscriber.onNext(null);
                subscriber.onCompleted();
            } else {
                // Cannot purchase a content; balance is low
                subscriber.onError(
// Path: 2016-07-12_rxjava_in_action/src/main/java/com/androidhuman/example/rxjavainaction/error/InsufficientBalanceError.java
// public final class InsufficientBalanceError extends Error {
// 
//     private int amount;
// 
//     public InsufficientBalanceError(int amount) {
//         this.amount = amount;
//     }
// 
//     public int getAmount() {
//         return amount;
//     }
// }


// Path: 2016-07-12_rxjava_in_action/src/main/java/com/androidhuman/example/rxjavainaction/rx/ChecksBalanceOnSubscribe.java
import com.androidhuman.example.rxjavainaction.error.InsufficientBalanceError;
import com.androidhuman.example.rxjavainaction.model.Content;
import com.androidhuman.example.rxjavainaction.model.User;
import rx.Observable;
import rx.Subscriber;

package com.androidhuman.example.rxjavainaction.rx;



final class ChecksBalanceOnSubscribe implements Observable.OnSubscribe<Void> {

    private final Content content;

    private final User user;

    ChecksBalanceOnSubscribe(Content content, User user) {
        this.content = content;
        this.user = user;
    }

    @Override
    public void call(Subscriber<? super Void> subscriber) {
        if (!subscriber.isUnsubscribed()) {
            if (content.getPrice() <= user.getBalance()) {
                // Can purchase a content
                subscriber.onNext(null);
                subscriber.onCompleted();
            } else {
                // Cannot purchase a content; balance is low
                subscriber.onError(
                        new InsufficientBalanceError(content.getPrice() - user.getBalance()));