aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillType.java
blob: 33d0387e72772b9bf16c8b078247dbc7e263cd3b (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.bumptech.glide.load.engine.prefill;

import android.graphics.Bitmap;

/**
 * A container for a set of options used to pre-fill a {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}
 * with {@link Bitmap Bitmaps} of a single size and configuration.
 */
public final class PreFillType {
    // Visible for testing.
    static final Bitmap.Config DEFAULT_CONFIG = Bitmap.Config.RGB_565;
    private final int width;
    private final int height;
    private final Bitmap.Config config;
    private final int weight;

    /**
     * Constructor for a single type of {@link android.graphics.Bitmap}.
     *
     * @param width The width in pixels of the {@link android.graphics.Bitmap Bitmaps} to
     *              pre-fill.
     * @param height The height in pixels of the {@link android.graphics.Bitmap Bitmaps} to
     *               pre-fill.
     * @param config The {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps} to
     *               pre-fill.
     * @param weight An integer indicating how to balance pre-filling this size and configuration of
     * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
     */
    PreFillType(int width, int height, Bitmap.Config config, int weight) {
        if (config == null) {
            throw new NullPointerException("Config must not be null");
        }
        if (width <= 0) {
            throw new IllegalArgumentException("Width must be > 0");
        }
        if (height <= 0) {
            throw new IllegalArgumentException("Height must be > 0");
        }
        if (weight <= 0) {
            throw new IllegalArgumentException("Weight must be > 0");
        }

        this.width = width;
        this.height = height;
        this.config = config;
        this.weight = weight;
    }

    /**
     * Returns the width in pixels of the {@link android.graphics.Bitmap Bitmaps}.
     */
    int getWidth() {
        return width;
    }

    /**
     * Returns the height in pixels of the {@link android.graphics.Bitmap Bitmaps}.
     */
    int getHeight() {
        return height;
    }

    /**
     * Returns the {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps}.
     */
    Bitmap.Config getConfig() {
        return config;
    }

    /**
     * Returns the weight of the {@link android.graphics.Bitmap Bitmaps} of this type.
     */
    int getWeight() {
        return weight;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o == null || getClass() != o.getClass()) {
            return false;
        }

        PreFillType size = (PreFillType) o;

        return height == size.height
                && weight == size.weight
                && width == size.width
                && config == size.config;
    }

    @Override
    public int hashCode() {
        int result = width;
        result = 31 * result + height;
        result = 31 * result + config.hashCode();
        result = 31 * result + weight;
        return result;
    }

    @Override
    public String toString() {
        return "PreFillSize{"
                + "width=" + width
                + ", height=" + height
                + ", config=" + config
                + ", weight=" + weight
                + '}';
    }

    /**
     * Builder for {@link PreFillType}.
     */
    public static final class Builder {
        private final int width;
        private final int height;

        private Bitmap.Config config;
        private int weight = 1;

        /**
         * Constructor for a builder that uses the given size as the width and height of the Bitmaps to prefill.
         * @param size The width and height in pixels of the Bitmaps to prefill.
         */
        public Builder(int size) {
            this(size, size);
        }

        /**
         * Constructor for a builder that uses the given dimensions as the dimensions of the Bitmaps to prefill.
         * @param width The width in pixels of the Bitmaps to prefill.
         * @param height The height in pixels of the Bitmaps to prefill.
         */
        public Builder(int width, int height) {
            this.width = width;
            this.height = height;
        }

        /**
         * Sets the {@link android.graphics.Bitmap.Config} for the Bitmaps to pre-fill.
         * @param config The config to use, or null to use Glide's default.
         * @return This builder.
         */
        public Builder setConfig(Bitmap.Config config) {
            this.config = config;
            return this;
        }

        /**
         * Returns the current {@link android.graphics.Bitmap.Config}.
         */
        Bitmap.Config getConfig() {
            return config;
        }

        /**
         * Sets the weight to use to balance how many Bitmaps of this type are prefilled relative to the other requested
         * types.
         * @param weight An integer indicating how to balance pre-filling this size and configuration of
         * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
         * @return This builder.
         */
        public Builder setWeight(int weight) {
            if (weight <= 0) {
                throw new IllegalArgumentException("Weight must be > 0");
            }
            this.weight = weight;
            return this;
        }

        /**
         * Returns a new {@link PreFillType}.
         */
        PreFillType build() {
            return new PreFillType(width, height, config, weight);
        }
    }
}