source: branches/0.17.x/abcl/src/org/armedbear/lisp/Packages.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/*
2 * Packages.java
3 *
4 * Copyright (C) 2002-2007 Peter Graves <peter@armedbear.org>
5 * $Id: Packages.java 12254 2009-11-06 20:07:54Z ehuelsmann $
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
34package org.armedbear.lisp;
35
36import java.util.ArrayList;
37import java.util.HashMap;
38import java.util.Iterator;
39import java.util.List;
40
41public final class Packages extends Lisp
42{
43  private static final ArrayList<Package> packages = new ArrayList<Package>();
44  private static final HashMap<String,Package> map = new HashMap<String,Package>();
45
46  public static final synchronized Package createPackage(String name)
47  {
48    return createPackage(name, 0);
49  }
50
51  public static final synchronized Package createPackage(String name, int size)
52  {
53    Package pkg = (Package) map.get(name);
54    if (pkg == null)
55      {
56        pkg = size != 0 ? new Package(name, size) : new Package(name);
57        packages.add(pkg);
58        map.put(name, pkg);
59      }
60    else
61      Debug.trace("package " + name + " already exists");
62    return pkg;
63  }
64
65  public static final synchronized void addPackage(Package pkg)
66
67  {
68    final String name = pkg.getName();
69    if (map.get(name) != null)
70      {
71        error(new LispError("A package named " + name + " already exists."));
72        return;
73      }
74    packages.add(pkg);
75    map.put(name, pkg);
76    List nicknames = pkg.getNicknames();
77    if (nicknames != null)
78      {
79        for (Iterator it = nicknames.iterator(); it.hasNext();)
80          {
81            String nickname = (String) it.next();
82            addNickname(pkg, nickname);
83          }
84      }
85  }
86
87  // Returns null if package doesn't exist.
88  public static final synchronized Package findPackage(String name)
89  {
90    return (Package) map.get(name);
91  }
92
93  public static final synchronized Package makePackage(String name)
94
95  {
96    if (map.get(name) != null)
97      {
98        error(new LispError("A package named " + name + " already exists."));
99        // Not reached.
100        return null;
101      }
102    Package pkg = new Package(name);
103    packages.add(pkg);
104    map.put(name, pkg);
105    return pkg;
106  }
107
108  public static final synchronized void addNickname(Package pkg, String nickname)
109
110  {
111    Object obj = map.get(nickname);
112    if (obj != null && obj != pkg)
113      {
114        error(new PackageError("A package named " + nickname + " already exists."));
115        return;
116      }
117    map.put(nickname, pkg);
118  }
119
120  // Removes name and nicknames from map, removes pkg from packages.
121  public static final synchronized boolean deletePackage(Package pkg)
122  {
123    String name = pkg.getName();
124    if (name != null)
125      {
126        map.remove(name);
127        List nicknames = pkg.getNicknames();
128        if (nicknames != null)
129          {
130            for (Iterator it = nicknames.iterator(); it.hasNext();)
131              {
132                String nickname = (String) it.next();
133                map.remove(nickname);
134              }
135          }
136        packages.remove(pkg);
137        return true;
138      }
139    return false;
140  }
141
142  public static final synchronized LispObject listAllPackages()
143  {
144    LispObject result = NIL;
145    for (Iterator it = packages.iterator(); it.hasNext();)
146      {
147        Package pkg = (Package) it.next();
148        result = new Cons(pkg, result);
149      }
150    return result;
151  }
152
153  public static final synchronized Package[] getAllPackages()
154  {
155    Package[] array = new Package[packages.size()];
156    packages.toArray(array);
157    return array;
158  }
159}
Note: See TracBrowser for help on using the repository browser.