/*
* [ColorSaver.java]
*
* Summary: conserves RAM and time by reusing Color objects.
*
* Copyright: (c) 1998-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
*
* Licence: This software may be copied and used freely for any purpose but military.
* http://mindprod.com/contact/nonmil.html
*
* Requires: JDK 1.8+
*
* Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
*
* Version History:
* 1.0 1998-01-16
* 1.1 1998-11-10 new address and phone.
* 1.2 2000-01-01
* 1.3 2009-01-01 convert to JDK 1.5, split off from business package.
*/
package com.mindprod.colorsaver;
import java.awt.Color;
import java.util.HashMap;
@SuppressWarnings( { "ALL" } )
/**
* conserves RAM and time by reusing Color objects.
*
* Instead of saying:
* Color c = new Color(150,0,250);
* say:
* Color c = ColorSaver.create(150,0,250);
*
* @author Roedy Green, Canadian Mind Products
* @version 1.3 2009-01-01 convert to JDK 1.5, split off from business package.
* @since 1998-01-16
*/
public class ColorSaver
{
private static final int FIRST_COPYRIGHT_YEAR = 1998;
/**
* undisplayed copyright notice
*/
@SuppressWarnings( { "UnusedDeclaration" } )
private static final String EMBEDDED_COPYRIGHT =
"Copyright: (c) 1998-2017 Roedy Green, Canadian Mind Products, http://mindprod.com";
private static final String RELEASE_DATE = "2009-01-01";
private static final String VERSION_STRING = "1.3";
/**
* used to store previous Colors, indexed by Color
*/
private static final HashMap h = new HashMap<>( 51 /* init size */, .75f );
static
{
// Load up the Hashtable with the color constant objects.
// There is no need to create new copies of them.
add( Color.BLACK );
add( Color.WHITE );
add( Color.LIGHT_GRAY );
add( Color.GRAY );
add( Color.DARK_GRAY );
add( Color.BLACK );
add( Color.RED );
add( Color.PINK );
add( Color.ORANGE );
add( Color.YELLOW );
add( Color.GREEN );
add( Color.MAGENTA );
add( Color.CYAN );
add( Color.BLUE );
}
/**
* add a Color to the cache, does not check if it is added already.
*
* @param color reference to a Color object.
*/
private static void add( Color color )
{
h.put( color, color );
}
/**
* returns Color object you give it if that colour has
* not been used before, but gives you an old Color object
* if it has. The one you passed will be garbage collected.
*
* @param color new Color
*
* @return Color object, hopefully one created previously
*/
public static Color create( Color color )
{
// see if we have used that Color before
/* equals will find Color object with matching RGB. Need note be exact same Color object */
Color prevColor = h.get( color );
if ( prevColor != null )
{
return prevColor;
}
// we have not used that Color before.
// Save a copy in case caller asks for it again.
// The Hashtable will keep growing to keep all the colours used.
add( color );
return color;
} // end create
/**
* create a new color
*
* @param r red 0..255
* @param g green 0..255
* @param b blue 0.255
*
* @return Color object, hopefully one created previously
*/
public static Color create( int r, int g, int b )
{
return create( new Color( r, g, b ) );
}
/**
* create a new color
*
* @param r red 0..1
* @param g green 0..1
* @param b blue 0.1
*
* @return Color object, hopefully one created previously
*/
public static Color create( float r, float g, float b )
{
return create( new Color( r, g, b ) );
}
/**
* create works just like the Color constructors:
* create a new color
*
* @param rgb red 0..255, green 0..255 blue 0.255, high bits 0.
*
* @return Color object, hopefully one created previously
*/
public static Color create( int rgb )
{
return create( new Color( rgb ) );
}
} // end class ColorSaver