1 /*
   2  * Copyright (c) 2005, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.jittester;
  25 
  26 public abstract class BuiltInType extends Type {
  27 
  28     private static class BuiltInTypeCapacityHelper {
  29 
  30         private static final String builtInTypes[] = {"boolean", "byte", "short", "char", "int", "long", "float", "double"};
  31 
  32         private static int getIndexFor(String typeName) {
  33             for (int i = 0; i < builtInTypes.length; i++) {
  34                 if (typeName.compareTo(builtInTypes[i]) == 0) {
  35                     return i;
  36                 }
  37             }
  38             return -1;
  39         }
  40 
  41         public static int compare(String typeName1, String typeName2) {
  42             int i1 = getIndexFor(typeName1);
  43             int i2 = getIndexFor(typeName2);
  44 
  45             return i1 - i2;
  46         }
  47     }
  48 
  49     protected BuiltInType(String name) {
  50         super(name);
  51     }
  52 
  53     @Override
  54     public boolean canImplicitlyCastTo(Type type) {
  55         if (equals(type)) {
  56             return true;
  57         }
  58         try {
  59             BuiltInType t = (BuiltInType) type;
  60             // You cannot impicitly cast anything to char or boolean
  61             if (t.getName().compareTo("boolean") == 0 || t.getName().compareTo("char") == 0) {
  62                 return false;
  63             }
  64             if (t.isMoreCapaciousThan(this)) {
  65                 return true;
  66             }
  67         } catch (Exception e) {
  68         }
  69         return false;
  70     }
  71 
  72     @Override
  73     public boolean canExplicitlyCastTo(Type t) {
  74         if (equals(t)) {
  75             return true;
  76         }
  77         try {
  78             BuiltInType _t = (BuiltInType) t;
  79             if (_t.getName().compareTo("boolean") != 0) {
  80                 return true;
  81             }
  82         } catch (Exception e) {
  83         }
  84         return false;
  85     }
  86 
  87     public boolean isMoreCapaciousThan(BuiltInType t) {
  88         return BuiltInTypeCapacityHelper.compare(this.getName(), t.getName()) > 0;
  89     }
  90 
  91     @Override
  92     public boolean canCompareTo(Type t) {
  93         return true;
  94     }
  95 
  96     @Override
  97     public boolean canEquateTo(Type t) {
  98         return true;
  99     }
 100 }