Extracting out CacheKey to a separate class

Change-Id: Ifdd7cc79668b34298e3f788ee684080cecb86c3e
diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java
index 91d4aaf..43f838e 100644
--- a/src/com/android/launcher3/IconCache.java
+++ b/src/com/android/launcher3/IconCache.java
@@ -40,6 +40,7 @@
 import com.android.launcher3.compat.LauncherAppsCompat;
 import com.android.launcher3.compat.UserHandleCompat;
 import com.android.launcher3.compat.UserManagerCompat;
+import com.android.launcher3.util.ComponentKey;
 
 import java.util.HashMap;
 import java.util.HashSet;
@@ -67,35 +68,14 @@
         public CharSequence contentDescription;
     }
 
-    private static class CacheKey {
-        public ComponentName componentName;
-        public UserHandleCompat user;
-
-        CacheKey(ComponentName componentName, UserHandleCompat user) {
-            this.componentName = componentName;
-            this.user = user;
-        }
-
-        @Override
-        public int hashCode() {
-            return componentName.hashCode() + user.hashCode();
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            CacheKey other = (CacheKey) o;
-            return other.componentName.equals(componentName) && other.user.equals(user);
-        }
-    }
-
     private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons =
             new HashMap<UserHandleCompat, Bitmap>();
     private final Context mContext;
     private final PackageManager mPackageManager;
     private final UserManagerCompat mUserManager;
     private final LauncherAppsCompat mLauncherApps;
-    private final HashMap<CacheKey, CacheEntry> mCache =
-            new HashMap<CacheKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
+    private final HashMap<ComponentKey, CacheEntry> mCache =
+            new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
     private final int mIconDpi;
     private final IconDB mIconDb;
 
@@ -180,21 +160,21 @@
      * Remove any records for the supplied ComponentName.
      */
     public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
-        mCache.remove(new CacheKey(componentName, user));
+        mCache.remove(new ComponentKey(componentName, user));
     }
 
     /**
      * Remove any records for the supplied package name from memory.
      */
     private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
-        HashSet<CacheKey> forDeletion = new HashSet<CacheKey>();
-        for (CacheKey key: mCache.keySet()) {
+        HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
+        for (ComponentKey key: mCache.keySet()) {
             if (key.componentName.getPackageName().equals(packageName)
                     && key.user.equals(user)) {
                 forDeletion.add(key);
             }
         }
-        for (CacheKey condemned: forDeletion) {
+        for (ComponentKey condemned: forDeletion) {
             mCache.remove(condemned);
         }
     }
@@ -324,7 +304,7 @@
         entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
         entry.title = app.getLabel();
         entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
-        mCache.put(new CacheKey(app.getComponentName(), app.getUser()), entry);
+        mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
 
         ContentValues values = new ContentValues();
         values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(entry.icon));
@@ -344,7 +324,7 @@
      * Empty out the cache that aren't of the correct grid size
      */
     public synchronized void flushInvalidIcons(DeviceProfile grid) {
-        Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator();
+        Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
         while (it.hasNext()) {
             final CacheEntry e = it.next().getValue();
             if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
@@ -426,7 +406,7 @@
      */
     private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
             UserHandleCompat user, boolean usePackageIcon) {
-        CacheKey cacheKey = new CacheKey(componentName, user);
+        ComponentKey cacheKey = new ComponentKey(componentName, user);
         CacheEntry entry = mCache.get(cacheKey);
         if (entry == null) {
             entry = new CacheEntry();
@@ -487,7 +467,7 @@
      */
     private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
         ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
-        CacheKey cacheKey = new CacheKey(cn, user);
+        ComponentKey cacheKey = new ComponentKey(cn, user);
         CacheEntry entry = mCache.get(cacheKey);
         if (entry == null) {
             entry = new CacheEntry();
diff --git a/src/com/android/launcher3/util/ComponentKey.java b/src/com/android/launcher3/util/ComponentKey.java
new file mode 100644
index 0000000..0f17f00
--- /dev/null
+++ b/src/com/android/launcher3/util/ComponentKey.java
@@ -0,0 +1,51 @@
+package com.android.launcher3.util;
+
+/**
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import android.content.ComponentName;
+
+import com.android.launcher3.compat.UserHandleCompat;
+
+import java.util.Arrays;
+
+public class ComponentKey {
+
+    public final ComponentName componentName;
+    public final UserHandleCompat user;
+
+    private final int mHashCode;
+
+    public ComponentKey(ComponentName componentName, UserHandleCompat user) {
+        assert (componentName != null);
+        assert (user != null);
+        this.componentName = componentName;
+        this.user = user;
+        mHashCode = Arrays.hashCode(new Object[] {componentName, user});
+
+    }
+
+    @Override
+    public int hashCode() {
+        return mHashCode;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        ComponentKey other = (ComponentKey) o;
+        return other.componentName.equals(componentName) && other.user.equals(user);
+    }
+}
\ No newline at end of file