1 19 package edu.umd.cs.findbugs.ba.constant; 20 21 import edu.umd.cs.findbugs.annotations.Nullable; 22 23 30 public class Constant { 31 private final Object value; 32 33 36 public static final Constant NOT_CONSTANT = new Constant(null); 37 38 43 public Constant(@Nullable Object value) { 44 this.value = value; 45 } 46 47 52 public boolean isConstant() { 53 return value != null; 54 } 55 56 61 public boolean isConstantString() { 62 return isConstant() && (value instanceof String ); 63 } 64 65 70 public String getConstantString() { 71 return (String ) value; 72 } 73 74 79 public boolean isConstantInteger() { 80 return isConstant() && (value instanceof Integer ); 81 } 82 83 84 89 public int getConstantInt() { 90 return ((Integer ) value).intValue(); 91 } 92 93 94 101 public static Constant merge(Constant a, Constant b) { 102 if (!a.isConstant() || !b.isConstant()) 103 return NOT_CONSTANT; 104 if (a.value.getClass() != b.value.getClass() || !a.value.equals(b.value)) 105 return NOT_CONSTANT; 106 return a; 107 } 108 109 @Override 111 public boolean equals(Object obj) { 112 if (obj == null || obj.getClass() != this.getClass()) 113 return false; 114 Constant other = (Constant) obj; 115 if (other.value == this.value) 116 return true; 117 else if (other.value == null || this.value == null) 118 return false; 119 else 120 return this.value.equals(other.value); 121 } 122 123 @Override 125 public int hashCode() { 126 return (value == null) ? 123 : value.hashCode(); 127 } 128 129 @Override 130 public String toString() { 131 if (!isConstant()) { 132 return "-"; 133 } else { 134 return "<" + value.toString() + ">"; 135 } 136 } 137 } 138 | Popular Tags |