1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.sjavac;
  27 
  28 import java.io.File;
  29 import java.net.URI;
  30 import java.util.HashMap;
  31 import java.util.Map;
  32 import java.util.Set;
  33 
  34 import com.sun.tools.sjavac.pubapi.PubApi;
  35 
  36 /**
  37  * The module is the root of a set of packages/sources/artifacts.
  38  * At the moment there is only one module in use, the empty/no-name/default module.
  39  *
  40  *  <p><b>This is NOT part of any supported API.
  41  *  If you write code that depends on this, you do so at your own risk.
  42  *  This code and its internal interfaces are subject to change or
  43  *  deletion without notice.</b>
  44  */
  45 public class Module implements Comparable<Module> {
  46     private String name;
  47     private String dirname;
  48     private Map<String,Package> packages = new HashMap<>();
  49     private Map<String,Source> sources = new HashMap<>();
  50     private Map<String,File> artifacts = new HashMap<>();
  51 
  52     public Module(String n, String dn) {
  53         name = n;
  54         dirname = n;
  55     }
  56 
  57     public String name() { return name; }
  58     public String dirname() { return dirname; }
  59     public Map<String,Package> packages() { return packages; }
  60     public Map<String,Source> sources() { return sources; }
  61     public Map<String,File> artifacts() { return artifacts; }
  62 
  63     @Override
  64     public boolean equals(Object o) {
  65         return (o instanceof Module) && name.equals(((Module)o).name);
  66     }
  67 
  68     @Override
  69     public int hashCode() {
  70         return name.hashCode();
  71     }
  72 
  73     @Override
  74     public int compareTo(Module o) {
  75         return name.compareTo(o.name);
  76     }
  77 
  78     public void save(StringBuilder b) {
  79         b.append("M ").append(name).append(":").append("\n");
  80         Package.savePackages(packages, b);
  81     }
  82 
  83     public static Module load(String l) {
  84         int cp = l.indexOf(':',2);
  85         if (cp == -1) return null;
  86         String name = l.substring(2,cp);
  87         return new Module(name, "");
  88     }
  89 
  90     public static void saveModules(Map<String,Module> ms, StringBuilder b) {
  91         for (Module m : ms.values()) {
  92             m.save(b);
  93         }
  94     }
  95 
  96     public void addPackage(Package p) {
  97         packages.put(p.name(), p);
  98     }
  99 
 100     public Package lookupPackage(String pkg) {
 101         // See JDK-8071904
 102         Package p = packages.get(pkg);
 103         if (p == null) {
 104             p = new Package(this, pkg);
 105             packages.put(pkg, p);
 106         }
 107         return p;
 108     }
 109 
 110     public void addSource(String pkg, Source src) {
 111         Package p = lookupPackage(pkg);
 112         src.setPackage(p);
 113         p.addSource(src);
 114         sources.put(src.file().getPath(), src);
 115     }
 116 
 117     public Source lookupSource(String path) {
 118         return sources.get(path);
 119     }
 120 
 121     public void addArtifacts(String pkg, Set<URI> as) {
 122         Package p = lookupPackage(pkg);
 123         for (URI u : as) {
 124             p.addArtifact(new File(u));
 125         }
 126     }
 127 
 128     public void setDependencies(String pkg, Map<String, Set<String>> deps, boolean cp) {
 129         lookupPackage(pkg).setDependencies(deps, cp);
 130     }
 131 
 132     public void setPubapi(String pkg, PubApi ps) {
 133         Package p = lookupPackage(pkg);
 134         p.setPubapi(ps);
 135     }
 136 
 137     public boolean hasPubapiChanged(String pkg, PubApi newPubApi) {
 138         Package p = lookupPackage(pkg);
 139         return p.hasPubApiChanged(newPubApi);
 140     }
 141 }