summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/util/QueueAccess.java
blob: 4c70d74e27d6f704b0f33535c85ad293b8866173 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package de.danoeh.antennapod.util;

import de.danoeh.antennapod.feed.FeedItem;

import java.util.Iterator;
import java.util.List;

/**
 * Provides methods for accessing the queue. It is possible to load only a part of the information about the queue that
 * is stored in the database (e.g. sometimes the user just has to test if a specific item is contained in the List.
 * QueueAccess provides an interface for accessing the queue without having to care about the type of the queue
 * representation.
 */
public abstract class QueueAccess {
    /**
     * Returns true if the item is in the queue, false otherwise.
     */
    public abstract boolean contains(long id);

    /**
     * Removes the item from the queue.
     *
     * @return true if the queue was modified by this operation.
     */
    public abstract boolean remove(long id);

    private QueueAccess() {

    }

    public static QueueAccess IDListAccess(final List<Long> ids) {
        return new QueueAccess() {
            @Override
            public boolean contains(long id) {
                return (ids != null) && ids.contains(id);
            }

            @Override
            public boolean remove(long id) {
                return ids.remove(id);
            }


        };
    }

    public static QueueAccess ItemListAccess(final List<FeedItem> items) {
        return new QueueAccess() {
            @Override
            public boolean contains(long id) {
                Iterator<FeedItem> it = items.iterator();
                for (FeedItem i = it.next(); it.hasNext(); i = it.next()) {
                    if (i.getId() == id) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public boolean remove(long id) {
                Iterator<FeedItem> it = items.iterator();
                for (FeedItem i = it.next(); it.hasNext(); i = it.next()) {
                    if (i.getId() == id) {
                        it.remove();
                        return true;
                    }
                }
                return false;
            }
        };
    }

    public static QueueAccess NotInQueueAccess() {
        return new QueueAccess() {
            @Override
            public boolean contains(long id) {
                return false;
            }

            @Override
            public boolean remove(long id) {
                return false;
            }
        };

    }

}