package org.robolectric.res;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.robolectric.res.android.ConfigDescription;
import org.robolectric.res.android.ResTable_config;

public class ResBundle {
  private final ResMap valuesMap = new ResMap();

  public void put(ResName resName, TypedResource value) {
    valuesMap.put(resName, value);
  }

  public TypedResource get(ResName resName, String qualifiers) {
    return valuesMap.pick(resName, qualifiers);
  }

  public void receive(ResourceTable.Visitor visitor) {
    for (final Map.Entry<ResName, Map<String, TypedResource>> entry : valuesMap.map.entrySet()) {
      visitor.visit(entry.getKey(), entry.getValue().values());
    }
  }

  static class ResMap {
    private final Map<ResName, Map<String, TypedResource>> map = new HashMap<>();

    public TypedResource pick(ResName resName, String qualifiersStr) {
      Map<String, TypedResource> values = map.get(resName);
      if (values == null || values.size() == 0) return null;

      Collection<TypedResource> typedResources = values.values();

      ResTable_config toMatch = new ResTable_config();
      new ConfigDescription().parse(qualifiersStr == null ? "" : qualifiersStr, toMatch);

      TypedResource bestMatchSoFar = null;
      for (TypedResource candidate : typedResources) {
        ResTable_config candidateConfig = candidate.getConfig();
        if (candidateConfig.match(toMatch)) {
          if (bestMatchSoFar == null || candidateConfig.isBetterThan(bestMatchSoFar.getConfig(), toMatch)) {
            bestMatchSoFar = candidate;
          }
        }
      }

      return bestMatchSoFar;
    }

    public void put(ResName resName, TypedResource value) {
      Map<String, TypedResource> values = map.get(resName);
      if (values == null) map.put(resName, values = new HashMap<>());
      if (!values.containsKey(value.getXmlContext().getQualifiers())) {
        values.put(value.getXmlContext().getQualifiers(), value);
      }
    }

    public int size() {
      return map.size();
    }
  }
}

@@ -40,26 +40,25 @@
 va.util.
-Collection
+ArrayList
 ;%0Aimport
@@ -73,24 +73,47 @@
 il.HashMap;%0A
+import java.util.List;%0A
 import java.
@@ -605,36 +605,29 @@
 ry%3CResName, 
-Map%3CString, 
+List%3C
 TypedResourc
@@ -718,25 +718,16 @@
 tValue()
-.values()
 );%0A    %7D
@@ -787,28 +787,21 @@
 esName, 
-Map%3CString, 
+List%3C
 TypedRes
@@ -905,36 +905,29 @@
 tr) %7B%0A      
-Map%3CString, 
+List%3C
 TypedResourc
@@ -1022,75 +1022,8 @@
 l;%0A%0A
-      Collection%3CTypedResource%3E typedResources = values.values();%0A%0A
     
@@ -1248,20 +1248,12 @@
 e : 
-typedResourc
+valu
 es) 
@@ -1651,20 +1651,13 @@
     
-Map%3CString, 
+List%3C
 Type
@@ -1746,31 +1746,33 @@
 alues = new 
-HashMap
+ArrayList
 %3C%3E());%0A     
@@ -1776,147 +1776,26 @@
     
-if (!
 values.
-containsKey(value.getXmlContext().getQualifiers())) %7B%0A        values.put(value.getXmlContext().getQualifiers(), value);%0A      %7D
+add(value);
 %0A   

Simplify ResBundle.