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