1package com.bumptech.glide.load;
2
3import com.bumptech.glide.load.engine.Resource;
4
5import java.util.Arrays;
6import java.util.Collection;
7
8/**
9 * A transformation that applies one or more transformations in iteration order to a resource.
10 *
11 * @param <T> The type of {@link com.bumptech.glide.load.engine.Resource} that will be transformed.
12 */
13public class MultiTransformation<T> implements Transformation<T> {
14    private final Collection<? extends Transformation<T>> transformations;
15    private String id;
16
17    @SafeVarargs
18    public MultiTransformation(Transformation<T>... transformations) {
19        if (transformations.length < 1) {
20            throw new IllegalArgumentException("MultiTransformation must contain at least one Transformation");
21        }
22        this.transformations = Arrays.asList(transformations);
23    }
24
25    public MultiTransformation(Collection<? extends Transformation<T>> transformationList) {
26        if (transformationList.size() < 1) {
27            throw new IllegalArgumentException("MultiTransformation must contain at least one Transformation");
28        }
29        this.transformations = transformationList;
30    }
31
32    @Override
33    public Resource<T> transform(Resource<T> resource, int outWidth, int outHeight) {
34        Resource<T> previous = resource;
35
36        for (Transformation<T> transformation : transformations) {
37            Resource<T> transformed = transformation.transform(previous, outWidth, outHeight);
38            if (previous != null && !previous.equals(resource) && !previous.equals(transformed)) {
39                previous.recycle();
40            }
41            previous = transformed;
42        }
43        return previous;
44    }
45
46    @Override
47    public String getId() {
48        if (id == null) {
49            StringBuilder sb = new StringBuilder();
50            for (Transformation<T> transformation : transformations) {
51                sb.append(transformation.getId());
52            }
53            id = sb.toString();
54        }
55        return id;
56    }
57}
58