1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.resources.manager;
18 
19 import com.android.resources.ResourceType;
20 import com.android.util.Pair;
21 import com.android.utils.SparseArray;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 
26 public class DynamicIdMap {
27 
28     private final Map<Pair<ResourceType, String>, Integer> mDynamicIds = new HashMap<Pair<ResourceType, String>, Integer>();
29     private final SparseArray<Pair<ResourceType, String>> mRevDynamicIds = new SparseArray<Pair<ResourceType, String>>();
30     private int mDynamicSeed;
31 
DynamicIdMap(int seed)32     public DynamicIdMap(int seed) {
33         mDynamicSeed = seed;
34     }
35 
reset(int seed)36     public void reset(int seed) {
37         mDynamicIds.clear();
38         mRevDynamicIds.clear();
39         mDynamicSeed = seed;
40     }
41 
42     /**
43      * Returns a dynamic integer for the given resource type/name, creating it if it doesn't
44      * already exist.
45      *
46      * @param type the type of the resource
47      * @param name the name of the resource
48      * @return an integer.
49      */
getId(ResourceType type, String name)50     public Integer getId(ResourceType type, String name) {
51         return getId(Pair.of(type, name));
52     }
53 
54     /**
55      * Returns a dynamic integer for the given resource type/name, creating it if it doesn't
56      * already exist.
57      *
58      * @param resource the type/name of the resource
59      * @return an integer.
60      */
getId(Pair<ResourceType, String> resource)61     public Integer getId(Pair<ResourceType, String> resource) {
62         Integer value = mDynamicIds.get(resource);
63         if (value == null) {
64             value = Integer.valueOf(++mDynamicSeed);
65             mDynamicIds.put(resource, value);
66             mRevDynamicIds.put(value, resource);
67         }
68 
69         return value;
70     }
71 
resolveId(int id)72     public Pair<ResourceType, String> resolveId(int id) {
73         return mRevDynamicIds.get(id);
74     }
75 }
76