• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.res;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import org.robolectric.res.android.ResTable_config;
8 import org.robolectric.util.Logger;
9 
10 public class ResBundle {
11   private final ResMap valuesMap = new ResMap();
12 
put(ResName resName, TypedResource value)13   public void put(ResName resName, TypedResource value) {
14     valuesMap.put(resName, value);
15   }
16 
get(ResName resName, ResTable_config config)17   public TypedResource get(ResName resName, ResTable_config config) {
18     return valuesMap.pick(resName, config);
19   }
20 
receive(ResourceTable.Visitor visitor)21   public void receive(ResourceTable.Visitor visitor) {
22     for (final Map.Entry<ResName, List<TypedResource>> entry : valuesMap.map.entrySet()) {
23       visitor.visit(entry.getKey(), entry.getValue());
24     }
25   }
26 
27   static class ResMap {
28     private final Map<ResName, List<TypedResource>> map = new HashMap<>();
29 
pick(ResName resName, ResTable_config toMatch)30     public TypedResource pick(ResName resName, ResTable_config toMatch) {
31       List<TypedResource> values = map.get(resName);
32       if (values == null || values.size() == 0) return null;
33 
34       TypedResource bestMatchSoFar = null;
35       for (TypedResource candidate : values) {
36         ResTable_config candidateConfig = candidate.getConfig();
37         if (candidateConfig.match(toMatch)) {
38           if (bestMatchSoFar == null || candidateConfig.isBetterThan(bestMatchSoFar.getConfig(), toMatch)) {
39             bestMatchSoFar = candidate;
40           }
41         }
42       }
43 
44       if (Logger.loggingEnabled()) {
45         Logger.debug("Picked '%s' for %s for qualifiers '%s' (%d candidates)",
46             bestMatchSoFar == null ? "<none>" : bestMatchSoFar.getXmlContext().getQualifiers().toString(),
47             resName.getFullyQualifiedName(),
48             toMatch,
49             values.size());
50       }
51       return bestMatchSoFar;
52     }
53 
put(ResName resName, TypedResource value)54     public void put(ResName resName, TypedResource value) {
55       if (!map.containsKey(resName)) {
56         map.put(resName, new ArrayList<>());
57       }
58 
59       map.get(resName).add(value);
60     }
61 
size()62     public int size() {
63       return map.size();
64     }
65   }
66 }
67