KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > MOFID


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence;
20
21 import java.io.*;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.text.*;
24 import java.util.*;
25 import org.netbeans.mdr.persistence.*;
26
27 /**
28  * MOFID implemented as a UUID for the repository plus a long for the ID.
29  */

30 public final class MOFID extends Object JavaDoc {
31
32     public static final int LENGTH = 8;
33     public static final int STRING_LENGTH = 16;
34
35     private long serialNumber;
36     private WeakReference JavaDoc string;
37     private String JavaDoc storageId = null;
38
39
40     public MOFID(Storage storage) {
41         this.storageId = storage.getStorageId ();
42         this.serialNumber = storage.getSerialNumber ();
43     }
44
45     /** This constructor is just for internal use by storage
46      * to create MOFID after deserialization and should be
47      * newer called by user code.
48      */

49     public MOFID(long serialNumber, String JavaDoc storageId) {
50         this.serialNumber = serialNumber;
51         if (storageId == null)
52             throw new IllegalArgumentException JavaDoc ();
53         else
54             this.storageId = storageId;
55     }
56
57     /** get the serial number
58     */

59     public long getSerialNumber() {
60         return serialNumber;
61     }
62     
63     /** Getter for storage ID.
64      * @return ID of the home storage of this MOFID.
65      */

66     public String JavaDoc getStorageID() {
67         return storageId;
68     }
69
70     /** compare MOFIDs as values
71     */

72     public boolean equals(Object JavaDoc 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     /*** Helper Methods ***/
82     
83     /** MOFIDs with equal values will hash equal
84     */

85     public int hashCode() {
86     return (int)serialNumber;
87     }
88
89     private static char makeStringBuffer[];
90     /**
91      * Construct the external form of a MOFID
92      */

93     public synchronized static String JavaDoc makeString(String JavaDoc 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 JavaDoc(makeStringBuffer, 0, totalSize);
103     }
104
105     /**
106      * Convert to external form.
107      */

108     public String JavaDoc toString() {
109         String JavaDoc result = string == null ? null : (String JavaDoc) string.get();
110         if (result == null) {
111             result = makeString(storageId, serialNumber);
112             string = new WeakReference JavaDoc(result);
113         }
114         return result;
115     }
116
117     private static int[] longwords;
118     /** Convert long to 16 hex digits, to be placed in a character array.
119     * @param value value to convert to hex digits
120     * @param buffer array into which to place hex digits
121     * @param beginning offset in array
122     */

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 JavaDoc 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 JavaDoc(mofId);
148             return result;
149         } catch (NumberFormatException JavaDoc e) {
150         } catch (IndexOutOfBoundsException JavaDoc e) {
151         }
152         return null;
153     }
154 }
155
Popular Tags