001: /*
002: * ErrorTable.java - part of the CodeAid plugin.
003: * Copyright (C) 1999 Jason Ginchereau
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: //package codeaid.info;
021: package org.acm.seguin.completer.info;
022:
023: // Collections API
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.LinkedList;
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.util.SortedSet;
030: import java.util.TreeSet;
031:
032: import errorlist.DefaultErrorSource;
033: import errorlist.ErrorSource;
034: import org.gjt.sp.util.Log;
035:
036: /**
037: * Contains a list of error information stored in <code>ErrorInfo</code>
038: * objects. All methods are thread-safe.
039: *
040: * @author Jason Ginchereau
041: **/
042: public final class ErrorTable {
043: private static Map staticInstanceTable = new HashMap();
044: private static final Object DEFAULT_KEY = new String();
045:
046: /**
047: * Gets a static instance of a <code>ErrorTable</code> that can
048: * be shared by everyone with the same key.
049: **/
050: public static synchronized ErrorTable getInstance(Object key) {
051: ErrorTable instance;
052: if ((instance = (ErrorTable) staticInstanceTable.get(key)) == null) {
053: staticInstanceTable.put(key, instance = new ErrorTable());
054: }
055: return instance;
056: }
057:
058: /**
059: * Gets a static instance of a <code>ErrorTable</code> using
060: * the default key.
061: **/
062: public static ErrorTable getInstance() {
063: return getInstance(DEFAULT_KEY);
064: }
065:
066: private SortedSet errorList;
067:
068: private static ErrorSource errorSource;
069:
070: public/*synchronized*/static void setErrorSource(
071: ErrorSource errSource) {
072: errorSource = errSource;
073: Log.log(Log.DEBUG, ErrorTable.class, "errorSource: "
074: + ((errSource == null) ? "null" : errSource.getClass()
075: .getName()));
076: }
077:
078: public ErrorTable() {
079: errorList = new TreeSet();
080: }
081:
082: public synchronized boolean put(ErrorInfo error) {
083: notify();
084: // Log.log(Log.DEBUG, this, error.toString() + " errorSource: " + ((errorSource == null) ? "null" : errorSource.getClass().getName()));
085: if (errorSource != null
086: && errorSource instanceof DefaultErrorSource) {
087: int line = error.getSourceLocation().getLine() - 1;
088: int column = error.getSourceLocation().getColumn() - 1;
089: int endColumn = column;
090: if (error.getSourceLocation().getLine() == error
091: .getSourceLocation().getEndLine()) {
092: if (error.getSourceLocation().getEndColumn() > 0) {
093: endColumn = error.getSourceLocation()
094: .getEndColumn();
095: }
096: }
097: ((DefaultErrorSource) errorSource).addError(
098: ErrorSource.WARNING, error.getSourceLocation()
099: .getPath(), line, column, endColumn, error
100: .getDescription()
101: + " "
102: + error.getSourceLocation().toString());
103: }
104: return errorList.add(error);
105: }
106:
107: public synchronized void replace(String path, ErrorTable newTable) {
108: for (Iterator i = errorList.iterator(); i.hasNext();) {
109: ErrorInfo ei = (ErrorInfo) i.next();
110: String epath = ei.getSourceLocation().getPath();
111: if ((epath == null && path == null)
112: || (epath != null && path != null && epath
113: .equals(path))) {
114: i.remove();
115: }
116: }
117: errorList.addAll(newTable.errorList);
118: notify();
119: }
120:
121: public synchronized boolean remove(ErrorInfo error) {
122: notify();
123: return errorList.remove(error);
124: }
125:
126: public synchronized void clear() {
127: errorList.clear();
128: Log.log(Log.DEBUG, this , "Clearing Errors");
129: if (errorSource != null
130: && errorSource instanceof DefaultErrorSource) {
131: ((DefaultErrorSource) errorSource).clear();
132: }
133: notify();
134: }
135:
136: public synchronized List getErrors() {
137: return new LinkedList(errorList);
138: }
139: }
|