1package com.bumptech.glide.load;
2
3import com.bumptech.glide.load.engine.Resource;
4
5import java.util.List;
6
7/**
8 * A transformation that applies an ordered array of one or more transformations to an image.
9 */
10public class MultiTransformation<T> implements Transformation<T> {
11    private Transformation<T>[] transformations;
12    private List<Transformation<T>> transformationList;
13    private String id;
14
15    public MultiTransformation(Transformation<T>... transformations) {
16        if (transformations.length < 1) {
17            throw new IllegalArgumentException("MultiTransformation must contain at least one Transformation");
18        }
19        this.transformations = transformations;
20    }
21
22    public MultiTransformation(List<Transformation<T>> transformationList) {
23        if (transformationList.size() < 1) {
24            throw new IllegalArgumentException("MultiTransformation must contain at least one Transformation");
25        }
26        this.transformationList = transformationList;
27    }
28
29    @Override
30    public Resource<T> transform(Resource<T> resource, int outWidth, int outHeight) {
31        Resource<T> previous = resource;
32
33        if (transformations != null) {
34            for (Transformation<T> transformation : transformations) {
35                Resource<T> transformed = transformation.transform(previous, outWidth, outHeight);
36                if (transformed != previous && previous != resource && previous != null) {
37                    previous.recycle();
38                }
39                previous = transformed;
40            }
41        } else {
42            for (Transformation<T> transformation : transformationList) {
43                 Resource<T> transformed = transformation.transform(previous, outWidth, outHeight);
44                if (transformed != previous && previous != resource && previous != null) {
45                    previous.recycle();
46                }
47                previous = transformed;
48            }
49
50        }
51        return previous;
52    }
53
54    @Override
55    public String getId() {
56        if (id == null) {
57            StringBuilder sb = new StringBuilder();
58            if (transformations != null) {
59                for (Transformation transformation : transformations) {
60                    sb.append(transformation.getId());
61                }
62            } else {
63                for (Transformation transformation : transformationList) {
64                    sb.append(transformation.getId());
65                }
66            }
67            id = sb.toString();
68        }
69        return id;
70    }
71}
72