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 3525 2007-10-16 11:43:48Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.util;
031:
032: import java.util.ArrayList;
033: import java.util.HashMap;
034:
035: /**
036: * Creation-Date: 06.03.2006, 20:20:17
037: *
038: * @author Thomas Morgner
039: */
040: public class LazyNameMap implements Cloneable {
041: public static class NameCarrier {
042: private int value;
043: private int counter;
044:
045: public NameCarrier(final int value) {
046: this .value = value;
047: this .counter = 1;
048: }
049:
050: public int getValue() {
051: return value;
052: }
053:
054: public int getInstanceCount() {
055: return counter;
056: }
057:
058: public void increase() {
059: this .counter += 1;
060: }
061:
062: public void decrease() {
063: this .counter -= 1;
064: }
065: }
066:
067: private HashMap map;
068: private ArrayList names;
069: private boolean clean;
070:
071: public LazyNameMap() {
072: map = new HashMap();
073: names = new ArrayList();
074: clean = false;
075: }
076:
077: public boolean isClean() {
078: return clean;
079: }
080:
081: public void setValue(final String key, final int value) {
082: if (clean) {
083: map = (HashMap) map.clone();
084: names = (ArrayList) names.clone();
085: clean = false;
086: }
087:
088: map.put(key, new NameCarrier(value));
089: }
090:
091: public NameCarrier get(final String key) {
092: return (NameCarrier) map.get(key);
093: }
094:
095: public NameCarrier remove(final String key) {
096: final NameCarrier nc = (NameCarrier) map.get(key);
097: if (nc == null) {
098: return null;
099: }
100:
101: if (clean) {
102: map = (HashMap) map.clone();
103: names = (ArrayList) names.clone();
104: clean = false;
105: }
106: return (NameCarrier) map.remove(key);
107: }
108:
109: public Object clone() {
110: try {
111: final LazyNameMap lm = (LazyNameMap) super .clone();
112: lm.clean = true;
113: clean = true;
114: return lm;
115: } catch (CloneNotSupportedException e) {
116: throw new IllegalStateException(
117: "Clone failed for some reason");
118: }
119: }
120: }
|