1 19 20 package edu.umd.cs.findbugs.ba.obl; 21 22 33 public class Path { 34 private static final int DEFAULT_CAPACITY = 8; 35 private static final int INVALID_HASH_CODE = -1; 36 37 private int[] blockIdList; 38 private int length; 39 private int cachedHashCode; 40 41 public Path() { 42 this.blockIdList = new int[DEFAULT_CAPACITY]; 43 this.length = 0; 44 invalidate(); 45 } 46 47 public void append(int id) { 48 grow(length); 49 blockIdList[length] = id; 50 ++length; 51 invalidate(); 52 } 53 54 public int getBlockIdAt(int index) { 55 return blockIdList[index]; 56 } 57 58 public int getLength() { 59 return length; 60 } 61 62 67 public Path duplicate() { 68 Path dup = new Path(); 69 dup.copyFrom(this); 70 return dup; 71 } 72 73 78 public void copyFrom(Path other) { 79 grow(other.length - 1); 80 System.arraycopy(other.blockIdList, 0, this.blockIdList, 0, other.length); 81 this.length = other.length; 82 this.cachedHashCode = other.cachedHashCode; 83 } 84 85 private void invalidate() { 86 this.cachedHashCode = INVALID_HASH_CODE; 87 } 88 89 @Override 90 public int hashCode() { 91 if (cachedHashCode == INVALID_HASH_CODE) { 92 int value = 0; 93 for (int i = 0; i < this.length; ++i) { 94 value += (i * 1009 * blockIdList[i]); 95 } 96 cachedHashCode = value; 97 } 98 return cachedHashCode; 99 } 100 101 @Override 102 public boolean equals(Object o) { 103 if (o == null || o.getClass() != this.getClass()) 104 return false; 105 Path other = (Path) o; 106 if (this.length != other.length) 107 return false; 108 for (int i = 0; i < this.length; ++i) { 109 if (this.blockIdList[i] != other.blockIdList[i]) 110 return false; 111 } 112 return true; 113 } 114 115 private static final String SYMBOLS = 116 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()"; 117 118 @Override 119 public String toString() { 120 StringBuffer buf = new StringBuffer (); 121 for (int i = 0; i < length; ++i) { 122 int block = blockIdList[i]; 123 if (block < SYMBOLS.length()) 124 buf.append(SYMBOLS.charAt(block)); 125 else 126 buf.append("'" + block + "'"); 127 } 128 return buf.toString(); 129 } 130 131 private void grow(int index) { 132 if (index >= blockIdList.length) { 133 int newLen = blockIdList.length; 134 do { 135 newLen *= 2; 136 } while (index >= newLen); 137 138 int[] arr = new int[newLen]; 139 System.arraycopy(this.blockIdList, 0, arr, 0, length); 140 this.blockIdList = arr; 141 } 142 } 143 } 144 145 | Popular Tags |