2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.layoutlib.bridge.util;
19 import com.android.resources.ResourceType;
20 import com.android.util.Pair;
22 import android.annotation.NonNull;
23 import android.util.SparseArray;
25 import java.util.HashMap;
28 public class DynamicIdMap {
30 private final Map<Pair<ResourceType, String>, Integer> mDynamicIds = new HashMap<Pair<ResourceType, String>, Integer>();
31 private final SparseArray<Pair<ResourceType, String>> mRevDynamicIds = new SparseArray<Pair<ResourceType, String>>();
32 private int mDynamicSeed;
34 public DynamicIdMap(int seed) {
38 public void reset(int seed) {
40 mRevDynamicIds.clear();
45 * Returns a dynamic integer for the given resource type/name, creating it if it doesn't
48 * @param type the type of the resource
49 * @param name the name of the resource
53 public Integer getId(ResourceType type, String name) {
54 return getId(Pair.of(type, name));
58 * Returns a dynamic integer for the given resource type/name, creating it if it doesn't
61 * @param resource the type/name of the resource
65 public Integer getId(Pair<ResourceType, String> resource) {
66 Integer value = mDynamicIds.get(resource);
68 value = ++mDynamicSeed;
69 mDynamicIds.put(resource, value);
70 mRevDynamicIds.put(value, resource);
76 public Pair<ResourceType, String> resolveId(int id) {
77 return mRevDynamicIds.get(id);