Module.java revision 2593:035b01d356ee
143105Sdfr/*
243105Sdfr * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
343105Sdfr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
443105Sdfr *
543105Sdfr * This code is free software; you can redistribute it and/or modify it
643105Sdfr * under the terms of the GNU General Public License version 2 only, as
743105Sdfr * published by the Free Software Foundation.  Oracle designates this
843105Sdfr * particular file as subject to the "Classpath" exception as provided
943105Sdfr * by Oracle in the LICENSE file that accompanied this code.
1043105Sdfr *
1143105Sdfr * This code is distributed in the hope that it will be useful, but WITHOUT
1243105Sdfr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1343105Sdfr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1443105Sdfr * version 2 for more details (a copy is included in the LICENSE file that
1543105Sdfr * accompanied this code).
1643105Sdfr *
1743105Sdfr * You should have received a copy of the GNU General Public License version
1843105Sdfr * 2 along with this work; if not, write to the Free Software Foundation,
1943105Sdfr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2043105Sdfr *
2143105Sdfr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2243105Sdfr * or visit www.oracle.com if you need additional information or have any
2343105Sdfr * questions.
2443105Sdfr */
2543105Sdfr
2643105Sdfrpackage com.sun.tools.sjavac;
27116181Sobrien
28116181Sobrienimport java.io.File;
29116181Sobrienimport java.net.URI;
3043105Sdfrimport java.util.HashMap;
3143105Sdfrimport java.util.List;
3243105Sdfrimport java.util.Map;
3343105Sdfrimport java.util.Set;
3443105Sdfr
35129880Sphk/**
3643105Sdfr * The module is the root of a set of packages/sources/artifacts.
3743105Sdfr * At the moment there is only one module in use, the empty/no-name/default module.
3847400Sdfr *
3947400Sdfr *  <p><b>This is NOT part of any supported API.
40159541Simp *  If you write code that depends on this, you do so at your own risk.
4143105Sdfr *  This code and its internal interfaces are subject to change or
42147271Smarius *  deletion without notice.</b>
43147271Smarius */
4443105Sdfrpublic class Module implements Comparable<Module> {
4543105Sdfr    private String name;
4643105Sdfr    private String dirname;
4743105Sdfr    private Map<String,Package> packages = new HashMap<>();
48147271Smarius    private Map<String,Source> sources = new HashMap<>();
49147271Smarius    private Map<String,File> artifacts = new HashMap<>();
50212413Savg
51188160Simp    public Module(String n, String dn) {
52216492Sjhb        name = n;
53216492Sjhb        dirname = n;
54216492Sjhb    }
55216492Sjhb
56216492Sjhb    public String name() { return name; }
5743105Sdfr    public String dirname() { return dirname; }
58147271Smarius    public Map<String,Package> packages() { return packages; }
59147271Smarius    public Map<String,Source> sources() { return sources; }
60147271Smarius    public Map<String,File> artifacts() { return artifacts; }
6147296Syokota
6247296Syokota    @Override
6343105Sdfr    public boolean equals(Object o) {
64147271Smarius        return (o instanceof Module) && name.equals(((Module)o).name);
6543105Sdfr    }
6643105Sdfr
6743105Sdfr    @Override
6883147Syokota    public int hashCode() {
69216492Sjhb        return name.hashCode();
70216492Sjhb    }
7143105Sdfr
7243105Sdfr    @Override
7383147Syokota    public int compareTo(Module o) {
7483147Syokota        return name.compareTo(o.name);
7583147Syokota    }
7643105Sdfr
7743105Sdfr    public void save(StringBuilder b) {
7843105Sdfr        b.append("M ").append(name).append(":").append("\n");
7943105Sdfr        Package.savePackages(packages, b);
8043105Sdfr    }
8143105Sdfr
82147271Smarius    public static Module load(String l) {
8343105Sdfr        int cp = l.indexOf(':',2);
84147271Smarius        if (cp == -1) return null;
8543105Sdfr        String name = l.substring(2,cp);
8643105Sdfr        return new Module(name, "");
8743105Sdfr    }
8858271Syokota
8958271Syokota    public static void saveModules(Map<String,Module> ms, StringBuilder b)
90238916Sjhb    {
91233794Sjkim        for (Module m : ms.values()) {
9258271Syokota            m.save(b);
9358271Syokota        }
9458271Syokota    }
9543105Sdfr
96147271Smarius    public void addPackage(Package p) {
9743105Sdfr        packages.put(p.name(), p);
9858271Syokota    }
9958271Syokota
10083147Syokota    public Package lookupPackage(String pkg) {
10183147Syokota        Package p = packages.get(pkg);
10258271Syokota        if (p == null) {
10358271Syokota            p = new Package(this, pkg);
104207354Ssobomax            packages.put(pkg, p);
105158041Ssobomax        }
106158041Ssobomax        return p;
107158041Ssobomax    }
108158041Ssobomax
109158041Ssobomax    public void addSource(String pkg, Source src) {
11043105Sdfr        Package p = lookupPackage(pkg);
11158271Syokota        src.setPackage(p);
11258271Syokota        p.addSource(src);
11358271Syokota        sources.put(src.file().getPath(), src);
11447618Sdfr    }
11558271Syokota
11658271Syokota    public Source lookupSource(String path) {
11783147Syokota        return sources.get(path);
11883147Syokota    }
11983147Syokota
12083147Syokota    public void addArtifacts(String pkg, Set<URI> as) {
12183147Syokota        Package p = lookupPackage(pkg);
122160091Sjkim        for (URI u : as) {
123160091Sjkim            p.addArtifact(new File(u));
124160091Sjkim        }
125160091Sjkim    }
126160091Sjkim
12783147Syokota    public void setDependencies(String pkg, Set<String> deps) {
12883147Syokota        Package p = lookupPackage(pkg);
12947400Sdfr        p.setDependencies(deps);
13083147Syokota    }
13183147Syokota
132160091Sjkim    public void setPubapi(String pkg, List<String> ps) {
133160091Sjkim        Package p = lookupPackage(pkg);
134160091Sjkim        p.setPubapi(ps);
135160091Sjkim    }
136160091Sjkim
13783147Syokota    public boolean hasPubapiChanged(String pkg, List<String> ps) {
138127135Snjl        Package p = lookupPackage(pkg);
13958271Syokota        return p.hasPubapiChanged(ps);
14047400Sdfr    }
14183147Syokota}
14283147Syokota