1 /*
   2  * Copyright 2007-2008 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package sun.module.repository;
  27 
  28 import java.module.ModuleArchiveInfo;
  29 import java.module.Version;
  30 
  31 /**
  32  * Represents the information about a single module as described by the schema
  33  * <tt>java.module.RepositoryMetada.xml</tt>.
  34  * @since 1.7
  35  */
  36 public class URLModuleInfo {
  37     // These fields are a reflection of the information in a
  38     // repository-metadata.xml file.  They're not private so they can
  39     // be accessed by subclasses e.g. {@code MetadataXMLReader.MutableURLModuleInfo}.
  40     String name;
  41     Version version;
  42     String platform;
  43     String arch;
  44     String path;
  45 
  46     /**
  47      * Having this constructor restricts its use to subclasses in this package,
  48      * specifically to {@code MetadataXMLReader.MutableURLModuleInfo}.
  49      **/
  50     URLModuleInfo() { }
  51 
  52     URLModuleInfo(URLModuleInfo other) {
  53         this.name = other.name;
  54         this.version = other.version;
  55         this.platform = other.platform;
  56         this.arch = other.arch;
  57         this.path = other.path;
  58     }
  59 
  60     URLModuleInfo(String name, Version version, String platform, String arch, String path) {
  61         if ((platform == null ^ arch == null)) {
  62             throw new IllegalArgumentException(
  63                 "module platform and arch must be either both provided, or neither provided");
  64         }
  65 
  66         if (name == null) {
  67             throw new IllegalArgumentException(
  68                 "name must not be null");
  69         }
  70 
  71         if (version == null) {
  72             throw new IllegalArgumentException(
  73                 "version must not be null");
  74         }
  75 
  76         this.name = name;
  77         this.version = version;
  78         this.platform = platform;
  79         this.arch = arch;
  80         this.path = path;
  81     }
  82 
  83     URLModuleInfo(ModuleArchiveInfo mai) {
  84         name = mai.getName();
  85         version = mai.getVersion();
  86         platform = mai.getPlatform();
  87         arch = mai.getArch();
  88     }
  89 
  90     public String getName() {
  91         return name;
  92     }
  93 
  94     public Version getVersion() {
  95         return version;
  96     }
  97 
  98     public String getPlatform() {
  99         return platform;
 100     }
 101 
 102     public String getArch() {
 103         return arch;
 104     }
 105 
 106     public String getPath() {
 107         return path;
 108     }
 109 
 110     public String getCanonicalizedPath()  {
 111         if (path != null) {
 112             return path;
 113         } else {
 114             if (getPlatform() != null && getArch() != null) {
 115                 return getName() + "/" + getVersion() + "/"
 116                    + getPlatform() + "-" + getArch();
 117             } else {
 118                 return getName() + "/" + getVersion();
 119             }
 120         }
 121     }
 122 
 123     /** Two URLModuleInfo's are equal iff all fields are equal. */
 124     public boolean equals(Object other) {
 125         if (other == null || !(other instanceof URLModuleInfo)) {
 126             return false;
 127         }
 128 
 129         URLModuleInfo mi = (URLModuleInfo) other;
 130         if (!name.equals(mi.name) || !version.equals(mi.version)) {
 131             return false;
 132         }
 133 
 134         if (platform == null) {
 135             if (mi.platform != null) {
 136                 return false;
 137             }
 138         } else if (!platform.equals(mi.platform)) {
 139             return false;
 140         }
 141 
 142         if (arch == null) {
 143             if (mi.arch != null) {
 144                 return false;
 145             }
 146         } else if (!arch.equals(mi.arch)) {
 147             return false;
 148         }
 149 
 150         // Note that path is not compared on purpose, because it is
 151         // a property of where the module lives, not a property of
 152         // the module itself.
 153 
 154         return true;
 155     }
 156 
 157     /** A URLModuleInfo's hash code is based on all fields except {@code path}. */
 158     public int hashCode() {
 159         int rc = name.hashCode();
 160         rc = 31 * rc + version.hashCode();
 161         rc = 31 * rc + (platform == null ? 0 : platform.hashCode());
 162         rc = 31 * rc + (arch == null ? 0 : arch.hashCode());
 163 
 164         // Note that path is not used on purpose, because it is
 165         // a property of where the module lives, not a property of
 166         // the module itself.
 167 
 168         return rc;
 169     }
 170 
 171     public String toString() {
 172         StringBuilder builder = new StringBuilder();
 173 
 174         builder.append("URLModuleInfo[name=");
 175         builder.append(name);
 176         builder.append(",version=");
 177         builder.append(version);
 178         if (platform != null) {
 179             builder.append(",platform=");
 180             builder.append(platform);
 181         }
 182         if (arch != null) {
 183             builder.append(",arch=");
 184             builder.append(arch);
 185         }
 186         if (path != null) {
 187             builder.append(",path=");
 188             builder.append(path);
 189         }
 190         builder.append("]");
 191         return builder.toString();
 192     }
 193 }