⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uuid.java

📁 一个简单实用的开源C++消息中间件SAFMQ - [软件开发] - [开源 消息中间件 SAFMQ ] 2006-11-23 在很多网络应用中
💻 JAVA
字号:
/*
 Copyright 2005 Matthew J. Battey

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software distributed
	under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
	CONDITIONS OF ANY KIND, either express or implied. See the License for the
	specific language governing permissions and limitations under the License.


This software implements a Java interface to SAFMQ (see http://safmq.sourceforge.net).

Created on Mar 21, 2005
*/
package com.safmq;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 An object to represent a universally unique identifier (UUID).
*/
public class UUID {
	public int		d1 = 0;
	public short	d2 = 0;
	public short	d3 = 0;
	public byte		d4[] = new byte[8];
	
	/**
	Default Constructor
	*/
	public UUID() {
		for(int x=0;x<d4.length;x++)
			d4[x] = 0;
	}
	
	int getSize() {
		return Safmq.SIZE_INT + Safmq.SIZE_SHORT + Safmq.SIZE_SHORT + d4.length;	
	}
	
	/**
	Constructs the UUID from the passed values
	*/
	public UUID(int d1, short d2, short d3, byte d4[]) {
		this.d1 = d1;
		this.d2 = d2;
		this.d3 = d3;
		for(int x=0;x<d4.length;x++)
			this.d4[x] = d4[x];
	}
	
	void write(DataOutput out) throws IOException {
		out.writeInt(d1);
		out.writeShort(d2);
		out.writeShort(d3);
		out.write(d4);
	}

	void read(DataInput in) throws IOException {
		d1 = in.readInt();
		d2 = in.readShort();
		d3 = in.readShort();
		in.readFully(d4);
	}
	
	/**
	Makes a clone copy of this object
	@return a duplicated copy of this UUID object
	*/
	public Object clone() {
		return new UUID(d1,d2,d3,d4);
	}
	
	/**
	Compares this instance of UUID to another object.
	@return true If this instance is equal to the passed instance,
					wherein the passed instance is also a UUID.
	*/
	public boolean equals(Object o) {
		if (o instanceof UUID) {
			UUID u = (UUID)o;
			return this.d1 == u.d1 &&
					this.d2 == u.d2 &&
					this.d3 == u.d3 &&
					this.d4[0] == u.d4[0] &&
					this.d4[1] == u.d4[1] &&
					this.d4[2] == u.d4[2] &&
					this.d4[3] == u.d4[3] &&
					this.d4[4] == u.d4[4] &&
					this.d4[5] == u.d4[5] &&
					this.d4[6] == u.d4[6] &&
					this.d4[7] == u.d4[7];
		}
		return false;
	}
	
	/**
	Prepares a string representation of this instance of UUID.
	@return A string representation of the UUID.
	*/
	public String toString() {
		String zeros="00000000";
		
		String d1 = Integer.toHexString(this.d1);
		String d2 = Integer.toHexString(this.d2 & 0x0FFFF);
		String d3 = Integer.toHexString(this.d3 & 0x0FFFF);
		String d4_0 = Integer.toHexString(d4[0] & 0x0FF);
		String d4_1 = Integer.toHexString(d4[1] & 0x0FF);
		String res;
		
		res = zeros.substring(0,8-d1.length()) + d1 + "-" 
				+ zeros.substring(0,4-d2.length()) + d2 + "-"
				+ zeros.substring(0,4-d3.length()) + d3 + "-"
				+ zeros.substring(0,2-d4_0.length()) + d4_0
				+ zeros.substring(0,2-d4_1.length()) + d4_1 + "-";
		
		for(int x=2;x<d4.length;x++) {
			String t;
			t = Integer.toHexString(d4[x] & 0x0FF);
			res += zeros.substring(0,2-t.length()) + t;
 		}
		return res;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -