xref: /AOO41X/main/jurt/com/sun/star/lib/uno/environments/remote/ThreadId.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.lib.uno.environments.remote;
29 
30 import com.sun.star.uno.UnoRuntime;
31 import java.io.UnsupportedEncodingException;
32 import java.math.BigInteger;
33 import java.util.Arrays;
34 
35 public final class ThreadId {
36     public static ThreadId createFresh() {
37         BigInteger c;
38         synchronized (PREFIX) {
39             c = count;
40             count = count.add(BigInteger.ONE);
41         }
42         try {
43             return new ThreadId((PREFIX + c).getBytes("UTF-8"));
44         } catch (UnsupportedEncodingException e) {
45             throw new RuntimeException("this cannot happen: " + e);
46         }
47     }
48 
49     public ThreadId(byte[] id) {
50         this.id = id;
51     }
52 
53     public boolean equals(Object obj) {
54         return obj instanceof ThreadId
55             && Arrays.equals(id, ((ThreadId) obj).id);
56     }
57 
58     public int hashCode() {
59         int h = hash;
60         if (h == 0) {
61             // Same algorithm as java.util.List.hashCode (also see Java 1.5
62             // java.util.Arrays.hashCode(byte[])):
63             h = 1;
64             for (int i = 0; i < id.length; ++i) {
65                 h = 31 * h + id[i];
66             }
67             hash = h;
68         }
69         return h;
70     }
71 
72     public String toString() {
73         StringBuffer b = new StringBuffer("[ThreadId:");
74         for (int i = 0; i < id.length; ++i) {
75             String n = Integer.toHexString(id[i] & 0xFF);
76             if (n.length() == 1) {
77                 b.append('0');
78             }
79             b.append(n);
80         }
81         b.append(']');
82         return b.toString();
83     }
84 
85     public byte[] getBytes() {
86         return id;
87     }
88 
89     private static final String PREFIX
90     = "java:" + UnoRuntime.getUniqueKey() + ":";
91     private static BigInteger count = BigInteger.ZERO;
92 
93     private byte[] id;
94     private int hash = 0;
95 }
96