Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IResponseCallback.java « api « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 899bec35401d36a7ce0fddc79071e1d3164b76c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package it.niedermann.nextcloud.deck.api;

import java.util.Collection;
import java.util.List;

import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.interfaces.RemoteEntity;


public abstract class IResponseCallback<T> {
    protected Account account;

    public IResponseCallback(Account account) {
        this.account = account;
    }

    public abstract void onResponse(T response);

    public abstract void onError(Throwable throwable);

    public void fillAccountIDs(T response) {
        if (response != null) {
            if (isListOfRemoteEntity(response)) {
                fillAccountIDs((Collection<RemoteEntity>) response);
            } else if (isRemoteEntity(response)) {
                fillAccountIDs((RemoteEntity) response);
            }
        }
    }

    private void fillAccountIDs(RemoteEntity response) {
        response.setAccount(this.account);
    }

    private void fillAccountIDs(Collection<RemoteEntity> response) {
        for (RemoteEntity entity : response) {
            entity.setAccount(this.account);
        }
    }

    private boolean isRemoteEntity(T response) {
        return response instanceof RemoteEntity;
    }

    private boolean isListOfRemoteEntity(T response) {
        if (response instanceof List) {
            List<?> collection = (List) response;
            return collection.size() > 0 && collection.get(0) instanceof RemoteEntity;
        }
        return false;
    }

    public Account getAccount() {
        return account;
    }
}