1 /*
   2  * Copyright (c) 2012, 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 org.openjdk.jigsaw;
  27 
  28 /**
  29  * <p> Architecture of a module-file, or module library </p>
  30  */
  31 
  32 public class ModuleArchitecture {  // ## find a better name
  33 
  34     private static final String WILDCARD = "";
  35 
  36     public static final ModuleArchitecture ANY =
  37             new ModuleArchitecture(WILDCARD, WILDCARD);
  38 
  39     private final String os;
  40     private final String arch;
  41 
  42     private ModuleArchitecture(String os, String arch) {
  43         this.os = os == null ? WILDCARD : os;
  44         this.arch = arch == null ? WILDCARD : arch;
  45     }
  46 
  47     public static ModuleArchitecture create(String os,  String arch) {
  48         return new ModuleArchitecture(os, arch);
  49     }
  50 
  51     public static ModuleArchitecture createFromOS(String os) {
  52         return new ModuleArchitecture(os, WILDCARD);
  53     }
  54 
  55     public static ModuleArchitecture createFromArch(String arch) {
  56         return new ModuleArchitecture(WILDCARD, arch);
  57     }
  58 
  59     /**
  60      * Determines if the given ModuleArchitecture matches this.
  61      */
  62     public boolean matches(ModuleArchitecture modArch) {
  63         if (!equalOrAny(os, modArch.os))
  64             return false;
  65         if (!equalOrAny(arch, modArch.arch))
  66             return false;
  67 
  68         return true;
  69     }
  70 
  71     // case-sensitive comparison
  72     private boolean equalOrAny(String s1, String s2) {
  73         if (s1.equals(WILDCARD))
  74             return true;
  75         if (s2.equals(WILDCARD))
  76             return true;
  77         if (s1.equals(s2))
  78             return true;
  79 
  80         return false;
  81     }
  82 
  83     @Override
  84     public boolean equals(Object ob) {
  85         if (!(ob instanceof ModuleArchitecture))
  86             return false;
  87         ModuleArchitecture that = (ModuleArchitecture)ob;
  88         if (!(os.equals(that.os)))
  89             return false;
  90         if (!(arch.equals(that.arch)))
  91             return false;
  92 
  93         return true;
  94     }
  95 
  96     @Override
  97     public int hashCode() {
  98         return (os.hashCode() * 41 + arch.hashCode());
  99     }
 100 
 101     public String os() {
 102         return os;
 103     }
 104 
 105     public String arch() {
 106         return arch;
 107     }
 108 
 109     @Override
 110     public String toString() {
 111         if (this.equals(ANY))
 112             return "ANY";
 113         if (arch.equals(WILDCARD))
 114             return os;
 115         if (os.equals(WILDCARD))
 116             return arch;
 117         return os + '-' + arch;
 118     }
 119 }