001: /*
002: * Packages.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: Packages.java,v 1.6 2003/11/15 11:03:30 beedlem 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: throw new ConditionThrowable(new LispError(
055: "a package named " + name + " already exists"));
056: packages.add(pkg);
057: map.put(name, pkg);
058: List nicknames = pkg.getNicknames();
059: if (nicknames != null) {
060: for (Iterator it = nicknames.iterator(); it.hasNext();) {
061: String nickname = (String) it.next();
062: addNickname(pkg, nickname);
063: }
064: }
065: }
066:
067: // Returns null if package doesn't exist.
068: public static final synchronized Package findPackage(String name) {
069: return (Package) map.get(name);
070: }
071:
072: public static final synchronized Package makePackage(String name)
073: throws ConditionThrowable {
074: if (map.get(name) != null)
075: throw new ConditionThrowable(new LispError(
076: "a package named " + name + " already exists"));
077: Package pkg = new Package(name);
078: packages.add(pkg);
079: map.put(name, pkg);
080: return pkg;
081: }
082:
083: public static final synchronized void addNickname(Package pkg,
084: String nickname) throws ConditionThrowable {
085: if (map.get(nickname) != null)
086: throw new ConditionThrowable(new PackageError(
087: "a package named " + nickname + " already exists"));
088: map.put(nickname, pkg);
089: }
090:
091: // Removes name and nicknames from map, removes pkg from packages.
092: public static final synchronized boolean deletePackage(Package pkg) {
093: String name = pkg.getName();
094: if (name != null) {
095: map.remove(name);
096: List nicknames = pkg.getNicknames();
097: if (nicknames != null) {
098: for (Iterator it = nicknames.iterator(); it.hasNext();) {
099: String nickname = (String) it.next();
100: map.remove(nickname);
101: }
102: }
103: packages.remove(pkg);
104: return true;
105: }
106: return false;
107: }
108:
109: public static final synchronized LispObject listAllPackages() {
110: LispObject result = NIL;
111: for (Iterator it = packages.iterator(); it.hasNext();) {
112: Package pkg = (Package) it.next();
113: result = new Cons(pkg, result);
114: }
115: return result;
116: }
117:
118: public static final synchronized Package[] getAllPackages() {
119: Package[] array = new Package[packages.size()];
120: packages.toArray(array);
121: return array;
122: }
123: }
|