bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / lib / uno / environments / remote / ThreadId.java
blob24d1572f866cf8675b78a40111457dad2d850fc7
1 /*
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() {
28 BigInteger c;
29 synchronized (PREFIX) {
30 c = count;
31 count = count.add(BigInteger.ONE);
33 try {
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) {
41 this.id = id;
44 /**
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
53 @Override
54 public boolean equals(Object obj) {
55 return obj instanceof ThreadId
56 && Arrays.equals(id, ((ThreadId) obj).id);
59 /**
60 * Returns a hash code value for the object.
62 * @return a hash code value for this object.
63 * @see java.lang.Object#hashCode
65 @Override
66 public int hashCode() {
67 int h = hash;
68 if (h == 0) {
69 // Same algorithm as java.util.List.hashCode (also see Java 1.5
70 // java.util.Arrays.hashCode(byte[])):
71 h = 1;
72 for (int i = 0; i < id.length; ++i) {
73 h = 31 * h + id[i];
75 hash = h;
77 return h;
80 /**
81 * Returns a string representation of the object.
83 * @return a string representation of the object.
84 * @see java.lang.Object#toString
86 @Override
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) {
92 b.append('0');
94 b.append(n);
96 b.append(']');
97 return b.toString();
100 public byte[] getBytes() {
101 return id;
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;