1 19 package org.netbeans.mdr.persistence; 20 21 import java.io.*; 22 import java.lang.ref.WeakReference ; 23 import java.text.*; 24 import java.util.*; 25 import org.netbeans.mdr.persistence.*; 26 27 30 public final class MOFID extends Object { 31 32 public static final int LENGTH = 8; 33 public static final int STRING_LENGTH = 16; 34 35 private long serialNumber; 36 private WeakReference string; 37 private String storageId = null; 38 39 40 public MOFID(Storage storage) { 41 this.storageId = storage.getStorageId (); 42 this.serialNumber = storage.getSerialNumber (); 43 } 44 45 49 public MOFID(long serialNumber, String storageId) { 50 this.serialNumber = serialNumber; 51 if (storageId == null) 52 throw new IllegalArgumentException (); 53 else 54 this.storageId = storageId; 55 } 56 57 59 public long getSerialNumber() { 60 return serialNumber; 61 } 62 63 66 public String getStorageID() { 67 return storageId; 68 } 69 70 72 public boolean equals(Object o) { 73 if (o == this) return true; 74 if (!(o instanceof MOFID)) 75 return false; 76 if (this.serialNumber != ((MOFID)o).serialNumber) 77 return false; 78 return this.getStorageID().equals (((MOFID)o).getStorageID()); 79 } 80 81 82 83 85 public int hashCode() { 86 return (int)serialNumber; 87 } 88 89 private static char makeStringBuffer[]; 90 93 public synchronized static String makeString(String prefix, long id) { 94 int size = prefix.length(); 95 int totalSize = size + 1 + STRING_LENGTH; 96 if (makeStringBuffer == null || makeStringBuffer.length < totalSize) { 97 makeStringBuffer = new char[totalSize]; 98 } 99 prefix.getChars(0, size, makeStringBuffer, 0); 100 makeStringBuffer[size] = ':'; 101 fromLong(id, makeStringBuffer, size + 1); 102 return new String (makeStringBuffer, 0, totalSize); 103 } 104 105 108 public String toString() { 109 String result = string == null ? null : (String ) string.get(); 110 if (result == null) { 111 result = makeString(storageId, serialNumber); 112 string = new WeakReference (result); 113 } 114 return result; 115 } 116 117 private static int[] longwords; 118 123 public synchronized static void fromLong(long value, char buffer[], int offset) { 124 int b; 125 if (longwords == null) 126 longwords = new int[2]; 127 longwords[0] = (int)((value >> 32) & 0xFFFFFFFF); 128 longwords[1] = (int)(value & 0xFFFFFFFF); 129 for (int j = 0; j < 2; j++) { 130 for (int shift = 28; shift >= 0; shift -= 4) { 131 b = (int)((longwords[j] >> shift) & 0xF); 132 133 if (b <= 9) 134 b += 0x30; 135 else 136 b += 0x37; 137 138 buffer[offset++] = (char)b; 139 } 140 } 141 } 142 143 public static MOFID fromString(String mofId) { 144 int pos = mofId.lastIndexOf(':'); 145 if (pos >= 0) try { 146 MOFID result = new MOFID(Long.parseLong(mofId.substring(pos + 1), 16), mofId.substring(0, pos)); 147 result.string = new WeakReference (mofId); 148 return result; 149 } catch (NumberFormatException e) { 150 } catch (IndexOutOfBoundsException e) { 151 } 152 return null; 153 } 154 } 155 | Popular Tags |