1 /* 2 * Copyright (C) 2017 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.ahat.heapdump; 18 19 import java.util.Objects; 20 21 /** DiffedFieldValue is used by the DiffedField class to return the result of 22 * diffing two collections of fields. 23 */ 24 public class DiffedFieldValue { 25 public final String name; 26 public final String type; 27 public final Value current; 28 public final Value baseline; 29 30 public final Status status; 31 32 public static enum Status { 33 ADDED, // The current field has no matching baseline value. 34 MATCHED, // The current field has a matching baseline value. 35 DELETED // The baseline field has no matching current value. 36 }; 37 38 /** 39 * Return a DiffedFieldValue where there is both a current and baseline. 40 */ matched(FieldValue current, FieldValue baseline)41 public static DiffedFieldValue matched(FieldValue current, FieldValue baseline) { 42 return new DiffedFieldValue(current.name, 43 current.type, 44 current.value, 45 baseline.value, 46 Status.MATCHED); 47 } 48 49 /** 50 * Return a DiffedFieldValue where there is no baseline. 51 */ added(FieldValue current)52 public static DiffedFieldValue added(FieldValue current) { 53 return new DiffedFieldValue(current.name, current.type, current.value, null, Status.ADDED); 54 } 55 56 /** 57 * Return a DiffedFieldValue where there is no current. 58 */ deleted(FieldValue baseline)59 public static DiffedFieldValue deleted(FieldValue baseline) { 60 return new DiffedFieldValue(baseline.name, baseline.type, null, baseline.value, Status.DELETED); 61 } 62 DiffedFieldValue(String name, String type, Value current, Value baseline, Status status)63 private DiffedFieldValue(String name, String type, Value current, Value baseline, Status status) { 64 this.name = name; 65 this.type = type; 66 this.current = current; 67 this.baseline = baseline; 68 this.status = status; 69 } 70 71 @Override equals(Object otherObject)72 public boolean equals(Object otherObject) { 73 if (otherObject instanceof DiffedFieldValue) { 74 DiffedFieldValue other = (DiffedFieldValue)otherObject; 75 return name.equals(other.name) 76 && type.equals(other.type) 77 && Objects.equals(current, other.current) 78 && Objects.equals(baseline, other.baseline) 79 && Objects.equals(status, other.status); 80 } 81 return false; 82 } 83 84 @Override toString()85 public String toString() { 86 switch (status) { 87 case ADDED: 88 return "(" + name + " " + type + " +" + current + ")"; 89 90 case MATCHED: 91 return "(" + name + " " + type + " " + current + " " + baseline + ")"; 92 93 case DELETED: 94 return "(" + name + " " + type + " -" + baseline + ")"; 95 96 default: 97 // There are no other members. 98 throw new AssertionError("unsupported enum member"); 99 } 100 } 101 } 102