1 /*
   2  * Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
   3  * 
   4  * This file is part of the Lock Contention Tracing Subsystem for the HotSpot
   5  * Virtual Machine, which is developed at Christian Doppler Laboratory on
   6  * Monitoring and Evolution of Very-Large-Scale Software Systems. Please
   7  * contact us at <http://mevss.jku.at/> if you need additional information
   8  * or have any questions.
   9  *
  10  * This code is free software; you can redistribute it and/or modify it
  11  * under the terms of the GNU General Public License version 2 only, as
  12  * published by the Free Software Foundation.
  13  *
  14  * This code is distributed in the hope that it will be useful, but WITHOUT
  15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17  * version 2 for more details (a copy is included in the LICENSE file that
  18  * accompanied this code).
  19  *
  20  * You should have received a copy of the GNU General Public License version
  21  * 2 along with this work. If not, see <http://www.gnu.org/licenses/>.
  22  *
  23  */ 
  24 package sun.evtracing.parser.metadata;
  25 
  26 public class JavaClass {
  27 
  28         public static final long SPECIAL_ID = 0;
  29         private static class SpecialJavaClass extends JavaClass {
  30                 public SpecialJavaClass(String name) {
  31                         super(SPECIAL_ID, 0, name);
  32                 }
  33                 @Override
  34                 public boolean isSet() {
  35                         return true;
  36                 }
  37                 @Override
  38                 public void set(long classLoader, String name) {
  39                         throw new UnsupportedOperationException();
  40                 }
  41                 @Override
  42                 public boolean isSpecial() {
  43                         return true;
  44                 }
  45                 @Override
  46                 public boolean equals(Object obj) {
  47                         return (this == obj);
  48                 }
  49         }
  50         public static final JavaClass UNKNOWN = new SpecialJavaClass("(class unknown)");
  51         public static final JavaClass UNCONTENDED = new SpecialJavaClass("(uncontended)");
  52 
  53         private final long id;
  54         private long classLoader;
  55         private String name;
  56 
  57         public JavaClass(long id) {
  58                 this(id, 0, "(name unknown)");
  59         }
  60 
  61         private JavaClass(long id, long classLoader, String name) {
  62                 this.id = id;
  63                 this.classLoader = classLoader;
  64                 this.name = name;
  65         }
  66         
  67         public boolean isSpecial() {
  68                 return false;
  69         }
  70 
  71         public long classLoader() {
  72                 assert isSet();
  73                 return classLoader;
  74         }
  75 
  76         public String name() {
  77                 return name;
  78         }
  79 
  80         public String prettyName(boolean compact) {
  81                 return JvmNamePrettyPrinter.prettyClassName(name, compact);
  82         }
  83 
  84         public boolean isSet() {
  85                 return (this.classLoader != 0);
  86         }
  87 
  88         public void set(long classLoader, String name) {
  89                 assert (classLoader != 0 && name != null);
  90                 assert !isSet() : "set allowed only once";
  91 
  92                 this.classLoader = classLoader;
  93                 this.name = name;
  94         }
  95 
  96         @Override
  97         public boolean equals(Object obj) {
  98                 if (this == obj) {
  99                         return true;
 100                 }
 101                 if (obj instanceof JavaClass) {
 102                         JavaClass other = (JavaClass) obj;
 103                         if (id == other.id) {
 104                                 assert (!isSet() || !other.isSet() || (classLoader == other.classLoader && name.equals(other.name)));
 105                                 return true;
 106                         }
 107                 }
 108                 return false;
 109         }
 110 
 111         @Override
 112         public int hashCode() {
 113                 return Long.hashCode(id);
 114         }
 115 
 116         @Override
 117         public String toString() {
 118                 return prettyName(false);
 119         }
 120 }