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.processing.statistics.metadata;
  25 
  26 import sun.evtracing.parser.metadata.JavaClass;
  27 
  28 public class JavaObject {
  29 
  30         private static class SpecialJavaObject extends JavaObject {
  31                 static final int IDENTITY_HASH = 0;
  32                 public SpecialJavaObject(JavaClass clazz) {
  33                         super(IDENTITY_HASH, clazz);
  34                 }
  35                 @Override
  36                 public boolean equals(Object obj) {
  37                         return (this == obj);
  38                 }
  39         };
  40         public static final JavaObject UNKNOWN = new SpecialJavaObject(JavaClass.UNKNOWN);
  41         public static final JavaObject UNCONTENDED = new SpecialJavaObject(JavaClass.UNCONTENDED);
  42 
  43         public static JavaObject get(int object, JavaClass clazz) {
  44                 if (object == SpecialJavaObject.IDENTITY_HASH) {
  45                         if (clazz == JavaClass.UNKNOWN) {
  46                                 return JavaObject.UNKNOWN;
  47                         }
  48                         if (clazz == JavaClass.UNCONTENDED) {
  49                                 return JavaObject.UNCONTENDED;
  50                         }
  51                 }
  52                 return new JavaObject(object, clazz);
  53         }
  54 
  55         private final int object;
  56         private final JavaClass clazz;
  57 
  58         private JavaObject(int object, JavaClass clazz) {
  59                 assert clazz != null;
  60                 this.object = object;
  61                 this.clazz = clazz;
  62         }
  63 
  64         public int identityHash() {
  65                 return object;
  66         }
  67 
  68         public JavaClass clazz() {
  69                 return clazz;
  70         }
  71 
  72         @Override
  73         public int hashCode() {
  74                 return object * 31 + clazz.hashCode();
  75         }
  76 
  77         @Override
  78         public boolean equals(Object obj) {
  79                 if (obj == this) {
  80                         return true;
  81                 }
  82                 if (obj instanceof JavaObject) {
  83                         JavaObject other = (JavaObject) obj;
  84                         return (object == other.object && clazz.equals(other.clazz));
  85                 }
  86                 return false;
  87         }
  88 
  89         @Override
  90         public String toString() {
  91                 return clazz.prettyName(true) + " @" + Integer.toHexString(object);
  92         }
  93 }