Source for org.jfree.report.util.LazyNameMap

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: LazyNameMap.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.util;
  32: 
  33: import java.util.ArrayList;
  34: import java.util.HashMap;
  35: 
  36: /**
  37:  * Creation-Date: 06.03.2006, 20:20:17
  38:  *
  39:  * @author Thomas Morgner
  40:  */
  41: public class LazyNameMap implements Cloneable
  42: {
  43:   public static class NameCarrier
  44:   {
  45:     private int value;
  46:     private int counter;
  47: 
  48:     public NameCarrier(final int value)
  49:     {
  50:       this.value = value;
  51:       this.counter = 1;
  52:     }
  53: 
  54:     public int getValue()
  55:     {
  56:       return value;
  57:     }
  58: 
  59:     public int getInstanceCount()
  60:     {
  61:       return counter;
  62:     }
  63: 
  64:     public void increase()
  65:     {
  66:       this.counter += 1;
  67:     }
  68: 
  69:     public void decrease()
  70:     {
  71:       this.counter -= 1;
  72:     }
  73:   }
  74: 
  75:   private HashMap map;
  76:   private ArrayList names;
  77:   private boolean clean;
  78: 
  79:   public LazyNameMap()
  80:   {
  81:     map = new HashMap();
  82:     names = new ArrayList();
  83:     clean = false;
  84:   }
  85: 
  86:   public boolean isClean()
  87:   {
  88:     return clean;
  89:   }
  90: 
  91:   public void setValue(final String key, final int value)
  92:   {
  93:     if (clean)
  94:     {
  95:       map = (HashMap) map.clone();
  96:       names = (ArrayList) names.clone();
  97:       clean = false;
  98:     }
  99: 
 100:     map.put(key, new NameCarrier(value));
 101:   }
 102: 
 103:   public NameCarrier get(final String key)
 104:   {
 105:     return (NameCarrier) map.get(key);
 106:   }
 107: 
 108:   public NameCarrier remove(final String key)
 109:   {
 110:     final NameCarrier nc = (NameCarrier) map.get(key);
 111:     if (nc == null)
 112:     {
 113:       return null;
 114:     }
 115: 
 116:     if (clean)
 117:     {
 118:       map = (HashMap) map.clone();
 119:       names = (ArrayList) names.clone();
 120:       clean = false;
 121:     }
 122:     return (NameCarrier) map.remove(key);
 123:   }
 124: 
 125:   public Object clone()
 126:   {
 127:     try
 128:     {
 129:       final LazyNameMap lm = (LazyNameMap) super.clone();
 130:       lm.clean = true;
 131:       clean = true;
 132:       return lm;
 133:     }
 134:     catch (CloneNotSupportedException e)
 135:     {
 136:       throw new IllegalStateException("Clone failed for some reason");
 137:     }
 138:   }
 139: }