/*
* [FontSaver.java]
*
* Summary: Conserves RAM and time by reusing Font and Font peer 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.1 1998-01-15
* 1.2 1998-11-10 new address and phone.
* 1.3 2007-12-19
* 1.4 2008-01-01 bundle with ANT script, pad, icon.
*/
package com.mindprod.fontsaver;
import com.mindprod.common18.FontFactory;
import java.awt.Font;
import java.util.Hashtable;
/**
* Conserves RAM and time by reusing Font and Font peer objects.
*
* Instead of saying:
* Font f = FontFactory.build("Dialog", 12, Font.PLAIN);
* say:
* Font f = FontSaver.create("Dialog", 12, Font.PLAIN);
*
* @author Roedy Green, Canadian Mind Products
* @version 1.4 2008-01-01 bundle with ANT script, pad, icon.
* @since 1998
*/
public class FontSaver
{
private static final int FIRST_COPYRIGHT_YEAR = 1998;
/**
* undisplayed copyright notice
*
* @noinspection UnusedDeclaration
*/
private static final String EMBEDDED_COPYRIGHT =
"Copyright: (c) 1998-2017 Roedy Green, Canadian Mind Products, http://mindprod.com";
/**
* date this version released.
*
* @noinspection UnusedDeclaration
*/
private static final String RELEASE_DATE = "2008-01-01";
/**
* embedded version string.
*
* @noinspection UnusedDeclaration
*/
private static final String VERSION_STRING = "1.4";
/**
* this was JDK 1.2. No HashMap
*/
private static Hashtable h;
/**
* Works just like the Font Constructor: Creates a new font with the specified name, style and point size.
*
* @param name the font name
* @param style the constant style used
* @param size the point size of the font
*
* @return Font from cache or created new if not in cache.
*/
public static Font create( String name, int style, int size )
{
if ( h == null )
{
h = new Hashtable<>( 101, .75f );
}
// see if we have used that font before
FontKey fontKey = new FontKey( name, style, size );
Font prevFont = h.get( fontKey );
if ( prevFont != null )
{
return prevFont;
}
// we have not used that font before. We need to create it.
Font newFont = FontFactory.build( name, style, size );
// save a copy in case caller asks for it again.
// The Hashtable will keep growing to keep all the fonts.
h.put( fontKey, newFont );
return newFont;
} // end create
// used to store previous Fonts, indexed by FontKeys.
} // end class FontSaver
/*
* FontKey is just the name, style and size fields of a font,
* enough to identify it, a key to find it in a Hashtable.
*/
class FontKey
{
/**
* The logical name of this font.
*/
private final String name;
/**
* The point size of this font.
*/
private final int size;
/**
* The style of the font. This is the sum of the constants PLAIN, BOLD, or ITALIC.
*/
private final int style;
/**
* constructor Creates a new FontKey id object with the specified name, style and point size.
*
* @param name the font name
* @param style the constant style used
* @param size the point size of the font
*/
public FontKey( String name, int style, int size )
{
this.name = name;
this.style = style;
this.size = size;
}
public boolean equals( Object obj )
{
if ( obj instanceof FontKey )
{
FontKey fontKey = ( FontKey ) obj;
return ( size == fontKey.size ) && ( style == fontKey.style ) && name.equals( fontKey.name );
}
return false;
}
/*
* Returns a hashcode for this font.
*/
public int hashCode()
{
return name.hashCode() ^ style ^ size;
}
} // end class FontKey