1 | /* |
---|
2 | * Packages.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2007 Peter Graves <peter@armedbear.org> |
---|
5 | * $Id: Packages.java 12290 2009-11-30 22:28:50Z vvoutilainen $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | import java.util.ArrayList; |
---|
39 | import java.util.HashMap; |
---|
40 | import java.util.Iterator; |
---|
41 | import java.util.List; |
---|
42 | |
---|
43 | public final class Packages |
---|
44 | { |
---|
45 | private static final ArrayList<Package> packages = new ArrayList<Package>(); |
---|
46 | private static final HashMap<String,Package> map = new HashMap<String,Package>(); |
---|
47 | |
---|
48 | public static final synchronized Package createPackage(String name) |
---|
49 | { |
---|
50 | return createPackage(name, 0); |
---|
51 | } |
---|
52 | |
---|
53 | public static final synchronized Package createPackage(String name, int size) |
---|
54 | { |
---|
55 | Package pkg = (Package) map.get(name); |
---|
56 | if (pkg == null) |
---|
57 | { |
---|
58 | pkg = size != 0 ? new Package(name, size) : new Package(name); |
---|
59 | packages.add(pkg); |
---|
60 | map.put(name, pkg); |
---|
61 | } |
---|
62 | else |
---|
63 | Debug.trace("package " + name + " already exists"); |
---|
64 | return pkg; |
---|
65 | } |
---|
66 | |
---|
67 | public static final synchronized void addPackage(Package pkg) |
---|
68 | |
---|
69 | { |
---|
70 | final String name = pkg.getName(); |
---|
71 | if (map.get(name) != null) |
---|
72 | { |
---|
73 | error(new LispError("A package named " + name + " already exists.")); |
---|
74 | return; |
---|
75 | } |
---|
76 | packages.add(pkg); |
---|
77 | map.put(name, pkg); |
---|
78 | List nicknames = pkg.getNicknames(); |
---|
79 | if (nicknames != null) |
---|
80 | { |
---|
81 | for (Iterator it = nicknames.iterator(); it.hasNext();) |
---|
82 | { |
---|
83 | String nickname = (String) it.next(); |
---|
84 | addNickname(pkg, nickname); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | // Returns null if package doesn't exist. |
---|
90 | public static final synchronized Package findPackage(String name) |
---|
91 | { |
---|
92 | return (Package) map.get(name); |
---|
93 | } |
---|
94 | |
---|
95 | public static final synchronized Package makePackage(String name) |
---|
96 | |
---|
97 | { |
---|
98 | if (map.get(name) != null) |
---|
99 | { |
---|
100 | error(new LispError("A package named " + name + " already exists.")); |
---|
101 | // Not reached. |
---|
102 | return null; |
---|
103 | } |
---|
104 | Package pkg = new Package(name); |
---|
105 | packages.add(pkg); |
---|
106 | map.put(name, pkg); |
---|
107 | return pkg; |
---|
108 | } |
---|
109 | |
---|
110 | public static final synchronized void addNickname(Package pkg, String nickname) |
---|
111 | |
---|
112 | { |
---|
113 | Object obj = map.get(nickname); |
---|
114 | if (obj != null && obj != pkg) |
---|
115 | { |
---|
116 | error(new PackageError("A package named " + nickname + " already exists.")); |
---|
117 | return; |
---|
118 | } |
---|
119 | map.put(nickname, pkg); |
---|
120 | } |
---|
121 | |
---|
122 | // Removes name and nicknames from map, removes pkg from packages. |
---|
123 | public static final synchronized boolean deletePackage(Package pkg) |
---|
124 | { |
---|
125 | String name = pkg.getName(); |
---|
126 | if (name != null) |
---|
127 | { |
---|
128 | map.remove(name); |
---|
129 | List nicknames = pkg.getNicknames(); |
---|
130 | if (nicknames != null) |
---|
131 | { |
---|
132 | for (Iterator it = nicknames.iterator(); it.hasNext();) |
---|
133 | { |
---|
134 | String nickname = (String) it.next(); |
---|
135 | map.remove(nickname); |
---|
136 | } |
---|
137 | } |
---|
138 | packages.remove(pkg); |
---|
139 | return true; |
---|
140 | } |
---|
141 | return false; |
---|
142 | } |
---|
143 | |
---|
144 | public static final synchronized LispObject listAllPackages() |
---|
145 | { |
---|
146 | LispObject result = NIL; |
---|
147 | for (Iterator it = packages.iterator(); it.hasNext();) |
---|
148 | { |
---|
149 | Package pkg = (Package) it.next(); |
---|
150 | result = new Cons(pkg, result); |
---|
151 | } |
---|
152 | return result; |
---|
153 | } |
---|
154 | |
---|
155 | public static final synchronized Package[] getAllPackages() |
---|
156 | { |
---|
157 | Package[] array = new Package[packages.size()]; |
---|
158 | packages.toArray(array); |
---|
159 | return array; |
---|
160 | } |
---|
161 | } |
---|