Source for gnu.java.awt.java2d.TextCacheKey

   1: /* TextCacheKey.java -- Key to use for caching texts with their rendered layout
   2:    Copyright (C) 2007 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.awt.java2d;
  39: 
  40: import java.awt.Font;
  41: import java.awt.font.FontRenderContext;
  42: 
  43: /**
  44:  * A key object to be used when caching pre-rendered text.
  45:  */
  46: public class TextCacheKey
  47: {
  48: 
  49:   /**
  50:    * The actual string.
  51:    */
  52:   private String string;
  53: 
  54:   /**
  55:    * The font render context.
  56:    */
  57:   private FontRenderContext fontRenderContext;
  58: 
  59:   /**
  60:    * The font.
  61:    */
  62:   private Font font;
  63: 
  64:   /**
  65:    * Creates a new TextCacheKey.
  66:    *
  67:    * This is intended to be used as search key. It is important to initialize
  68:    * the values using the setter methods before using this key, otherwise
  69:    * it will throw NPEs.
  70:    */
  71:   public TextCacheKey()
  72:   {
  73:     // No-arg constructor.
  74:   }
  75: 
  76:   /**
  77:    * Creates a new TextCacheKey with initial values.
  78:    *
  79:    * @param s the string
  80:    * @param f the font
  81:    * @param frc the font render context
  82:    */
  83:   public TextCacheKey(String s, Font f, FontRenderContext frc)
  84:   {
  85:     string = s;
  86:     font = f;
  87:     fontRenderContext = frc;
  88:   }
  89: 
  90:   /**
  91:    * Re-sets the string. This is intented to be used in search keys only.
  92:    *
  93:    * @param s the string to set
  94:    */
  95:   public void setString(String s)
  96:   {
  97:     string = s;
  98:   }
  99: 
 100:   /**
 101:    * Sets the font render context.
 102:    * This is intented to be used in search keys only.
 103:    *
 104:    * @param frc the new font render context
 105:    */
 106:   public void setFontRenderContext(FontRenderContext frc)
 107:   {
 108:     fontRenderContext = frc;
 109:   }
 110: 
 111:   /**
 112:    * Sets the font.
 113:    * This is intented to be used in search keys only.
 114:    *
 115:    * @param f the font to set
 116:    */
 117:   public void setFont(Font f)
 118:   {
 119:     font = f;
 120:   }
 121: 
 122:   /**
 123:    * Determines if two objects are equal.
 124:    *
 125:    * @see Object#equals(Object)
 126:    */
 127:   public boolean equals(Object o)
 128:   {
 129:     boolean eq;
 130:     if (o instanceof TextCacheKey)
 131:       {
 132:         TextCacheKey other = (TextCacheKey) o;
 133:         eq = other.string.equals(string)
 134:              && other.font.equals(font)
 135:              && other.fontRenderContext.equals(fontRenderContext);
 136:       }
 137:     else
 138:       {
 139:         eq = false;
 140:       }
 141:     return eq;
 142:   }
 143: 
 144:   /**
 145:    * Computes a hashcode for this key.
 146:    *
 147:    * @see Object#hashCode()
 148:    */
 149:   public int hashCode()
 150:   {
 151:     return string.hashCode() ^ font.hashCode() ^ fontRenderContext.hashCode();
 152:   }
 153: }