001 /**
002 * ========================================
003 * JFreeReport : a free Java report library
004 * ========================================
005 *
006 * Project Info: http://reporting.pentaho.org/
007 *
008 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009 *
010 * This library is free software; you can redistribute it and/or modify it under the terms
011 * of the GNU Lesser General Public License as published by the Free Software Foundation;
012 * either version 2.1 of the License, or (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public License along with this
019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020 * Boston, MA 02111-1307, USA.
021 *
022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023 * in the United States and other countries.]
024 *
025 * ------------
026 * $Id: LazyNameMap.java,v 1.3 2007/04/01 18:49:33 taqua Exp $
027 * ------------
028 * (C) Copyright 2000-2005, by Object Refinery Limited.
029 * (C) Copyright 2005-2007, by Pentaho Corporation.
030 */
031 package org.jfree.report.util;
032
033 import java.util.ArrayList;
034 import java.util.HashMap;
035
036 /**
037 * Creation-Date: 06.03.2006, 20:20:17
038 *
039 * @author Thomas Morgner
040 */
041 public class LazyNameMap implements Cloneable
042 {
043 public static class NameCarrier
044 {
045 private int value;
046 private int counter;
047
048 public NameCarrier(final int value)
049 {
050 this.value = value;
051 this.counter = 1;
052 }
053
054 public int getValue()
055 {
056 return value;
057 }
058
059 public int getInstanceCount()
060 {
061 return counter;
062 }
063
064 public void increase()
065 {
066 this.counter += 1;
067 }
068
069 public void decrease()
070 {
071 this.counter -= 1;
072 }
073 }
074
075 private HashMap map;
076 private ArrayList names;
077 private boolean clean;
078
079 public LazyNameMap()
080 {
081 map = new HashMap();
082 names = new ArrayList();
083 clean = false;
084 }
085
086 public boolean isClean()
087 {
088 return clean;
089 }
090
091 public void setValue(final String key, final int value)
092 {
093 if (clean)
094 {
095 map = (HashMap) map.clone();
096 names = (ArrayList) names.clone();
097 clean = false;
098 }
099
100 map.put(key, new NameCarrier(value));
101 }
102
103 public NameCarrier get(String key)
104 {
105 return (NameCarrier) map.get(key);
106 }
107
108 public NameCarrier remove(final String key)
109 {
110 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 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 }