001: /*
002: * Packages.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: Packages.java,v 1.12 2003/12/13 00:28:08 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: public final class Packages extends Lisp {
030: private static final ArrayList packages = new ArrayList();
031: private static final HashMap map = new HashMap();
032:
033: public static final synchronized Package createPackage(String name) {
034: return createPackage(name, 0);
035: }
036:
037: public static final synchronized Package createPackage(String name,
038: int size) {
039: Package pkg = (Package) map.get(name);
040: if (pkg == null) {
041: pkg = size != 0 ? new Package(name, size) : new Package(
042: name);
043: packages.add(pkg);
044: map.put(name, pkg);
045: } else
046: Debug.trace("package " + name + " already exists");
047: return pkg;
048: }
049:
050: public static final synchronized void addPackage(Package pkg)
051: throws ConditionThrowable {
052: final String name = pkg.getName();
053: if (map.get(name) != null) {
054: signal(new LispError("a package named " + name
055: + " already exists"));
056: return;
057: }
058: packages.add(pkg);
059: map.put(name, pkg);
060: List nicknames = pkg.getNicknames();
061: if (nicknames != null) {
062: for (Iterator it = nicknames.iterator(); it.hasNext();) {
063: String nickname = (String) it.next();
064: addNickname(pkg, nickname);
065: }
066: }
067: }
068:
069: // Returns null if package doesn't exist.
070: public static final synchronized Package findPackage(String name) {
071: return (Package) map.get(name);
072: }
073:
074: public static final synchronized Package makePackage(String name)
075: throws ConditionThrowable {
076: if (map.get(name) != null) {
077: signal(new LispError("a package named " + name
078: + " already exists"));
079: // Not reached.
080: return null;
081: }
082: Package pkg = new Package(name);
083: packages.add(pkg);
084: map.put(name, pkg);
085: return pkg;
086: }
087:
088: public static final synchronized void addNickname(Package pkg,
089: String nickname) throws ConditionThrowable {
090: if (map.get(nickname) != null) {
091: signal(new PackageError("a package named " + nickname
092: + " already exists"));
093: return;
094: }
095: map.put(nickname, pkg);
096: }
097:
098: // Removes name and nicknames from map, removes pkg from packages.
099: public static final synchronized boolean deletePackage(Package pkg) {
100: String name = pkg.getName();
101: if (name != null) {
102: map.remove(name);
103: List nicknames = pkg.getNicknames();
104: if (nicknames != null) {
105: for (Iterator it = nicknames.iterator(); it.hasNext();) {
106: String nickname = (String) it.next();
107: map.remove(nickname);
108: }
109: }
110: packages.remove(pkg);
111: return true;
112: }
113: return false;
114: }
115:
116: public static final synchronized LispObject listAllPackages() {
117: LispObject result = NIL;
118: for (Iterator it = packages.iterator(); it.hasNext();) {
119: Package pkg = (Package) it.next();
120: result = new Cons(pkg, result);
121: }
122: return result;
123: }
124:
125: public static final synchronized Package[] getAllPackages() {
126: Package[] array = new Package[packages.size()];
127: packages.toArray(array);
128: return array;
129: }
130: }
|