001: /*
002: * <copyright>
003: *
004: * Copyright 2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.util;
027:
028: import java.lang.ref.SoftReference;
029: import org.cougaar.bootstrap.SystemProperties;
030:
031: /** A hack for computing a complex object's toString as needed,
032: * but without keeping it around for ever.
033: * @property org.cougaar.util.ToStringMemo.cache If set to false (default is true)
034: * ToStringMemo will actually not cache toStrings at all, allowing
035: * profiling code to have visibility into the toString process.
036: **/
037: public abstract class ToStringMemo {
038: protected static final boolean isCaching = SystemProperties
039: .getBoolean("org.cougaar.util.ToStringMemo.cache", true);
040:
041: /** Implement this to be the actual toString implementation **/
042: protected abstract String generate();
043:
044: /** called to discard any cached toString information **/
045: public abstract void discard();
046:
047: /** return a cached value or call generate **/
048: public abstract String toString();
049:
050: /** The standard ToStringMemo implementation **/
051: public static abstract class SoftToStringMemo extends ToStringMemo {
052: private transient SoftReference memo = null;
053:
054: public final synchronized void discard() {
055: memo = null;
056: }
057:
058: public final synchronized String toString() {
059: if (memo != null) { // we've got a memo
060: String s = (String) memo.get();
061: if (s != null) { // and the memo isn't empty
062: return s; // return it
063: }
064: }
065: // otherwise, recompute the memo
066: String s = generate();
067: memo = new SoftReference(s);
068: return s;
069: }
070: }
071:
072: /** A version of ToStringMemo which doesn't really ever cache **/
073: public static abstract class UncachedToStringMemo extends
074: ToStringMemo {
075: public void discard() {
076: }
077:
078: public String toString() {
079: return generate();
080: };
081: }
082:
083: /** Construct a ToStringMemo which uses the parameter object's toString
084: * to build the memoized toString value.
085: **/
086: public static ToStringMemo getInstance(final Object gen) {
087: if (isCaching) {
088: return new SoftToStringMemo() {
089: protected String generate() {
090: return gen.toString();
091: }
092: };
093: } else {
094: return new UncachedToStringMemo() {
095: protected String generate() {
096: return gen.toString();
097: }
098: };
099: }
100: }
101: }
|