001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.util.Map;
029:
030: /**
031: * Wrapper around a Throwable for stack-based equality.
032: */
033: public final class StackElements {
034:
035: private final Throwable throwable;
036: private StackTraceElement[] elements;
037: private int _hc;
038:
039: public static final StackElements getStackElements(Map cache) {
040: return getStackElements(cache, new Throwable());
041: }
042:
043: public static final StackElements getStackElements(Map cache,
044: Throwable t) {
045: return getStackElements(cache, new StackElements(t));
046: }
047:
048: public static final StackElements getStackElements(Map cache,
049: StackElements se) {
050: synchronized (cache) {
051: StackElements cached_se = (StackElements) cache.get(se);
052: if (cached_se == null) {
053: cache.put(se, se);
054: } else {
055: se = cached_se;
056: }
057: }
058: return se;
059: }
060:
061: public StackElements(Throwable throwable) {
062: this .throwable = throwable;
063: if (throwable == null) {
064: throw new IllegalArgumentException("null throwable");
065: }
066: }
067:
068: public Throwable getThrowable() {
069: return throwable;
070: }
071:
072: private StackTraceElement[] getStackTrace() {
073: // cache the array, otherwise each access is a clone
074: if (elements == null) {
075: elements = throwable.getStackTrace();
076: }
077: return elements;
078: }
079:
080: public int hashCode() {
081: if (_hc == 0) {
082: _hc = 1;
083: StackTraceElement[] st = getStackTrace();
084: for (int i = 0, n = st.length; i < n; i++) {
085: _hc = 31 * _hc + st[i].hashCode();
086: }
087: }
088: return _hc;
089: }
090:
091: public boolean equals(Object o) {
092: if (o == this ) {
093: return true;
094: } else if (!(o instanceof StackElements)) {
095: return false;
096: }
097: StackTraceElement[] a_st = getStackTrace();
098: StackTraceElement[] b_st = ((StackElements) o).getStackTrace();
099: if (a_st.length != b_st.length) {
100: return false;
101: }
102: for (int i = 0, n = a_st.length; i < n; i++) {
103: if (!a_st[i].equals(b_st[i])) {
104: return false;
105: }
106: }
107: return true;
108: }
109:
110: public String toString() {
111: return throwable.toString();
112: }
113: }
|