1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.uno.environments.remote;
25 
26 import com.sun.star.uno.UnoRuntime;
27 import java.io.UnsupportedEncodingException;
28 import java.math.BigInteger;
29 import java.util.Arrays;
30 
31 public final class ThreadId {
createFresh()32     public static ThreadId createFresh() {
33         BigInteger c;
34         synchronized (PREFIX) {
35             c = count;
36             count = count.add(BigInteger.ONE);
37         }
38         try {
39             return new ThreadId((PREFIX + c).getBytes("UTF-8"));
40         } catch (UnsupportedEncodingException e) {
41             throw new RuntimeException("this cannot happen: " + e);
42         }
43     }
44 
ThreadId(byte[] id)45     public ThreadId(byte[] id) {
46         this.id = id;
47     }
48 
equals(Object obj)49     public boolean equals(Object obj) {
50         return obj instanceof ThreadId
51             && Arrays.equals(id, ((ThreadId) obj).id);
52     }
53 
hashCode()54     public int hashCode() {
55         int h = hash;
56         if (h == 0) {
57             // Same algorithm as java.util.List.hashCode (also see Java 1.5
58             // java.util.Arrays.hashCode(byte[])):
59             h = 1;
60             for (int i = 0; i < id.length; ++i) {
61                 h = 31 * h + id[i];
62             }
63             hash = h;
64         }
65         return h;
66     }
67 
toString()68     public String toString() {
69         StringBuffer b = new StringBuffer("[ThreadId:");
70         for (int i = 0; i < id.length; ++i) {
71             String n = Integer.toHexString(id[i] & 0xFF);
72             if (n.length() == 1) {
73                 b.append('0');
74             }
75             b.append(n);
76         }
77         b.append(']');
78         return b.toString();
79     }
80 
getBytes()81     public byte[] getBytes() {
82         return id;
83     }
84 
85     private static final String PREFIX
86     = "java:" + UnoRuntime.getUniqueKey() + ":";
87     private static BigInteger count = BigInteger.ZERO;
88 
89     private byte[] id;
90     private int hash = 0;
91 }
92