Home | History | Annotate | Download | only in engine
      1 package com.bumptech.glide.load.engine;
      2 
      3 import com.bumptech.glide.load.Encoder;
      4 import com.bumptech.glide.load.Key;
      5 import com.bumptech.glide.load.ResourceDecoder;
      6 import com.bumptech.glide.load.ResourceEncoder;
      7 import com.bumptech.glide.load.Transformation;
      8 import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
      9 
     10 import java.io.UnsupportedEncodingException;
     11 import java.nio.ByteBuffer;
     12 import java.security.MessageDigest;
     13 
     14 @SuppressWarnings("rawtypes")
     15 class EngineKey implements Key {
     16     private final String id;
     17     private final int width;
     18     private final int height;
     19     private final ResourceDecoder cacheDecoder;
     20     private final ResourceDecoder decoder;
     21     private final Transformation transformation;
     22     private final ResourceEncoder encoder;
     23     private final ResourceTranscoder transcoder;
     24     private final Encoder sourceEncoder;
     25     private final Key signature;
     26     private String stringKey;
     27     private int hashCode;
     28     private Key originalKey;
     29 
     30     public EngineKey(String id, Key signature, int width, int height, ResourceDecoder cacheDecoder,
     31             ResourceDecoder decoder, Transformation transformation, ResourceEncoder encoder,
     32             ResourceTranscoder transcoder, Encoder sourceEncoder) {
     33         this.id = id;
     34         this.signature = signature;
     35         this.width = width;
     36         this.height = height;
     37         this.cacheDecoder = cacheDecoder;
     38         this.decoder = decoder;
     39         this.transformation = transformation;
     40         this.encoder = encoder;
     41         this.transcoder = transcoder;
     42         this.sourceEncoder = sourceEncoder;
     43     }
     44 
     45     public Key getOriginalKey() {
     46         if (originalKey == null) {
     47             originalKey = new OriginalKey(id, signature);
     48         }
     49         return originalKey;
     50     }
     51 
     52     @Override
     53     public boolean equals(Object o) {
     54         if (this == o) {
     55             return true;
     56         }
     57         if (o == null || getClass() != o.getClass()) {
     58             return false;
     59         }
     60 
     61         EngineKey engineKey = (EngineKey) o;
     62 
     63         if (!id.equals(engineKey.id)) {
     64             return false;
     65         } else if (!signature.equals(engineKey.signature)) {
     66             return false;
     67         } else if (height != engineKey.height) {
     68             return false;
     69         } else if (width != engineKey.width) {
     70             return false;
     71         } else if (transformation == null ^ engineKey.transformation == null) {
     72             return false;
     73         } else if (transformation != null && !transformation.getId().equals(engineKey.transformation.getId())) {
     74             return false;
     75         } else if (decoder == null ^ engineKey.decoder == null) {
     76             return false;
     77         } else if (decoder != null && !decoder.getId().equals(engineKey.decoder.getId())) {
     78             return false;
     79         } else if (cacheDecoder == null ^ engineKey.cacheDecoder == null) {
     80             return false;
     81         } else if (cacheDecoder != null && !cacheDecoder.getId().equals(engineKey.cacheDecoder.getId())) {
     82             return false;
     83         } else if (encoder == null ^ engineKey.encoder == null) {
     84             return false;
     85         } else if (encoder != null && !encoder.getId().equals(engineKey.encoder.getId())) {
     86             return false;
     87         } else if (transcoder == null ^ engineKey.transcoder == null) {
     88             return false;
     89         } else if (transcoder != null && !transcoder.getId().equals(engineKey.transcoder.getId())) {
     90             return false;
     91         } else if (sourceEncoder == null ^ engineKey.sourceEncoder == null) {
     92             return false;
     93         } else if (sourceEncoder != null && !sourceEncoder.getId().equals(engineKey.sourceEncoder.getId())) {
     94             return false;
     95         }
     96         return true;
     97     }
     98 
     99     @Override
    100     public int hashCode() {
    101         if (hashCode == 0) {
    102             hashCode = id.hashCode();
    103             hashCode = 31 * hashCode + signature.hashCode();
    104             hashCode = 31 * hashCode + width;
    105             hashCode = 31 * hashCode + height;
    106             hashCode = 31 * hashCode + (cacheDecoder   != null ? cacheDecoder  .getId().hashCode() : 0);
    107             hashCode = 31 * hashCode + (decoder        != null ? decoder       .getId().hashCode() : 0);
    108             hashCode = 31 * hashCode + (transformation != null ? transformation.getId().hashCode() : 0);
    109             hashCode = 31 * hashCode + (encoder        != null ? encoder       .getId().hashCode() : 0);
    110             hashCode = 31 * hashCode + (transcoder     != null ? transcoder    .getId().hashCode() : 0);
    111             hashCode = 31 * hashCode + (sourceEncoder  != null ? sourceEncoder .getId().hashCode() : 0);
    112         }
    113         return hashCode;
    114     }
    115 
    116     @Override
    117     public String toString() {
    118         if (stringKey == null) {
    119             stringKey = new StringBuilder()
    120                 .append(id)
    121                 .append(signature)
    122                 .append(width)
    123                 .append(height)
    124                 .append(cacheDecoder   != null ? cacheDecoder  .getId() : "")
    125                 .append(decoder        != null ? decoder       .getId() : "")
    126                 .append(transformation != null ? transformation.getId() : "")
    127                 .append(encoder        != null ? encoder       .getId() : "")
    128                 .append(transcoder     != null ? transcoder    .getId() : "")
    129                 .append(sourceEncoder  != null ? sourceEncoder .getId() : "")
    130                 .toString();
    131         }
    132         return stringKey;
    133     }
    134 
    135     @Override
    136     public void updateDiskCacheKey(MessageDigest messageDigest) throws UnsupportedEncodingException {
    137         byte[] dimensions = ByteBuffer.allocate(8)
    138                 .putInt(width)
    139                 .putInt(height)
    140                 .array();
    141         signature.updateDiskCacheKey(messageDigest);
    142         messageDigest.update(id.getBytes(STRING_CHARSET_NAME));
    143         messageDigest.update(dimensions);
    144         messageDigest.update((cacheDecoder   != null ? cacheDecoder  .getId() : "").getBytes(STRING_CHARSET_NAME));
    145         messageDigest.update((decoder        != null ? decoder       .getId() : "").getBytes(STRING_CHARSET_NAME));
    146         messageDigest.update((transformation != null ? transformation.getId() : "").getBytes(STRING_CHARSET_NAME));
    147         messageDigest.update((encoder        != null ? encoder       .getId() : "").getBytes(STRING_CHARSET_NAME));
    148         // The Transcoder is not included in the disk cache key because its result is not cached.
    149         messageDigest.update((sourceEncoder  != null ? sourceEncoder .getId() : "").getBytes(STRING_CHARSET_NAME));
    150     }
    151 }
    152