1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bugreport.stacks;
18 
19 import java.util.ArrayList;
20 
21 /**
22  * Represents an instance of a held lock (java monitor object) in a thread.
23  */
24 public class LockSnapshot implements Comparable<LockSnapshot> {
25     public static final int LOCKED = 1;
26     public static final int WAITING = 2;
27     public static final int SLEEPING = 4;
28     public static final int BLOCKED = 8;
29 
30     public static final int ANY = LOCKED | WAITING | SLEEPING | BLOCKED;
31 
32     public int type;
33     public String address;
34     public String packageName;
35     public String className;
36     public int threadId = -1;
37 
LockSnapshot()38     public LockSnapshot() {
39     }
40 
LockSnapshot(LockSnapshot that)41     public LockSnapshot(LockSnapshot that) {
42         this.type = that.type;
43         this.address = that.address;
44         this.packageName = that.packageName;
45         this.className = that.className;
46         this.threadId = that.threadId;
47     }
48 
49     @Override
clone()50     public LockSnapshot clone() {
51         return new LockSnapshot(this);
52     }
53 
equals(LockSnapshot that)54     public boolean equals(LockSnapshot that) {
55         return this.address == that.address
56                 || (this.address != null && that.address != null
57                     && this.address.equals(that.address));
58     }
59 
60     @Override
hashCode()61     public int hashCode() {
62         int hash = 7;
63         if (this.address != null) {
64             hash = hash * 31 + this.address.hashCode();
65         }
66         return hash;
67     }
68 
compareTo(LockSnapshot that)69     public int compareTo(LockSnapshot that) {
70         if (this.address == that.address) {
71             return 0;
72         } else if (this.address == null) {
73             return -1;
74         } else if (that.address == null) {
75             return 1;
76         } else {
77             return this.address.compareTo(that.address);
78         }
79     }
80 
81 }
82 
83