2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.lib
.uno
.environments
.remote
;
21 import com
.sun
.star
.uno
.UnoRuntime
;
22 import java
.io
.UnsupportedEncodingException
;
23 import java
.math
.BigInteger
;
24 import java
.util
.Arrays
;
26 public final class ThreadId
{
27 public static ThreadId
createFresh() {
29 synchronized (PREFIX
) {
31 count
= count
.add(BigInteger
.ONE
);
34 return new ThreadId((PREFIX
+ c
).getBytes("UTF-8"));
35 } catch (UnsupportedEncodingException e
) {
36 throw new RuntimeException("this cannot happen: " + e
);
40 public ThreadId(byte[] id
) {
45 * Indicates whether some other object is equal to this one.
47 * @param obj the reference object with which to compare.
48 * @return <code>true</code> if this object is the same as the obj argument;
49 * <code>false</code> otherwise.
51 * @see java.lang.Object#equals
54 public boolean equals(Object obj
) {
55 return obj
instanceof ThreadId
56 && Arrays
.equals(id
, ((ThreadId
) obj
).id
);
60 * Returns a hash code value for the object.
62 * @return a hash code value for this object.
63 * @see java.lang.Object#hashCode
66 public int hashCode() {
69 // Same algorithm as java.util.List.hashCode (also see Java 1.5
70 // java.util.Arrays.hashCode(byte[])):
72 for (int i
= 0; i
< id
.length
; ++i
) {
81 * Returns a string representation of the object.
83 * @return a string representation of the object.
84 * @see java.lang.Object#toString
87 public String
toString() {
88 StringBuffer b
= new StringBuffer("[ThreadId:");
89 for (int i
= 0; i
< id
.length
; ++i
) {
90 String n
= Integer
.toHexString(id
[i
] & 0xFF);
91 if (n
.length() == 1) {
100 public byte[] getBytes() {
104 private static final String PREFIX
= "java:" + UnoRuntime
.getUniqueKey() + ":";
105 private static BigInteger count
= BigInteger
.ZERO
;
107 private final byte[] id
;
108 private int hash
= 0;