1

I have a list of CardViews that contain Bitmaps that return from the Network or from a LruCache.

I also run a Palette.from(Bitmap).generate() operation on these bitmaps. This is expensive. I would like to save these palette colors in a HashMap<...> of some sort.

This is the idea that I have, use HashMap<String, Integer> for each item name, and its corresponding color value.

If the name changes then change the color value. The items in the list do not change that often so that is why I am considering using a HashMap<String, Integer>.

Secondly save this HashMap somewhere, maybe on the disk so when the user starts the app again there is no need to generate the Palette swatch if it already exists (and matches the name).

I am thinking to implement it this way:

public class PaletteCache {

    private static PaletteCache sPaletteCache;
    private static HashMap<String, Integer> mCache;

    private PaletteCache() { 
        loadCache();
    }

    public static PaletteCache getInstance() {
        if (sPaletteCache == null)
            sPaletteCache = new PaletteCache();
        return sPaletteCache;
    }

    private static void loadCache() {
        // Load the HashMap from disk if present
    }

    public static int getVibrantColor(String itemName) {
        return mCache.get(itemName);
    }

    public static void setColor(String itemName, int color) {
        mCache.put(itemName, color);
    }

}

Any criticisms of this approach? If so what are alternatives?

AndyRoid
  • 5,062
  • 8
  • 38
  • 73

1 Answers1

0

That approach to lazy initialization of a singleton will fail in multithread applications.

An alternative which does not have that problem and does not carry a performance hit is using an enum to implement the singleton.

public enum PaletteCache
{
    INSTANCE;

    private static HashMap<String, Integer> mCache;

    private PaletteCache() {
        loadCache();
    }

    public static PaletteCache getInstance() {
        return INSTANCE;
    }

    private static void loadCache() {
        // Load the HashMap from disk if present
    }

    public static int getVibrantColor(String itemName) {
        return mCache.get(itemName);
    }

    public static void setColor(String itemName, int color) {
        mCache.put(itemName, color);
    }

}
Anonymous Coward
  • 3,140
  • 22
  • 39
  • Can you give me an example of how an `enum` would work in say a `RecyclerView.Adapter<>`? – AndyRoid Aug 23 '15 at 06:33
  • Also aren't `enum`s generally bad practice on Android? – AndyRoid Aug 23 '15 at 06:41
  • Look at the section titled "Avoid Enums", this is from someone working on the Android UI Team. https://medium.com/google-developers/developing-for-android-ii-bb9a51f8c8b9 – AndyRoid Aug 23 '15 at 06:43
  • That document recomends to avoid using enums to replace integer constants for performance reasons. I agree with that. The reason being that an enum object is a full fledged object and thus more expensive than a simple integer. But here you are not replacing an integer constant by an enum. You are replacing a singleton object by an enum singleton object. Both objects, both with the same cost. But enum is more robust and also has better performance, you don't need a comparision against null each time you access your singleton. – Anonymous Coward Aug 23 '15 at 06:49
  • As for ReciclerViewAdapter<> I am not sure what you are asking. Do you mean that your singleton needs to extend it? – Anonymous Coward Aug 23 '15 at 06:50
  • Using it inside one, that would be the entire purpose for using a PaletteCache, please look at my original question "I have a list of `CardView`s. I am not sure how to use what you are suggesting. – AndyRoid Aug 23 '15 at 06:52
  • You use it exactly the same way you would have used your original PaletteCache. Take notice that the public interface of my PaletteCache is equal to the public interface of yours. You can your implementation by mine without need of change to any other code. – Anonymous Coward Aug 23 '15 at 06:55
  • Looks like there's a lot of people against using enums, check here: http://stackoverflow.com/questions/9246934/working-with-enums-in-android – AndyRoid Aug 23 '15 at 06:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87678/discussion-between-jose-antonio-dura-olmos-and-andyroid). – Anonymous Coward Aug 23 '15 at 06:56