aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillQueue.java
blob: f88032a8082e9a76222c4b7b63e9bb75b81b0158 (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
package com.bumptech.glide.load.engine.prefill;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

final class PreFillQueue {

    private final Map<PreFillType, Integer> bitmapsPerType;
    private final List<PreFillType> keyList;
    private int bitmapsRemaining;
    private int keyIndex;

    public PreFillQueue(Map<PreFillType, Integer> bitmapsPerType) {
        this.bitmapsPerType = bitmapsPerType;
        // We don't particularly care about the initial order.
        keyList = new ArrayList<PreFillType>(bitmapsPerType.keySet());

        for (Integer count : bitmapsPerType.values()) {
            bitmapsRemaining += count;
        }
    }

    public PreFillType remove() {
        PreFillType result = keyList.get(keyIndex);

        Integer countForResult = bitmapsPerType.get(result);
        if (countForResult == 1) {
            bitmapsPerType.remove(result);
            keyList.remove(keyIndex);
        } else {
            bitmapsPerType.put(result, countForResult - 1);
        }
        bitmapsRemaining--;

        // Avoid divide by 0.
        keyIndex =  keyList.isEmpty() ? 0 : (keyIndex + 1) % keyList.size();

        return result;
    }

    public int getSize() {
        return bitmapsRemaining;
    }

    public boolean isEmpty() {
        return bitmapsRemaining == 0;
    }
}