UID.java 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***********************************************************************************************************************
 *
 * Copyright (C) 2010-2014 by the Stratosphere project (http://stratosphere.eu)
 *
 * 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.
 *
 **********************************************************************************************************************/
package eu.stratosphere.streaming.api.streamrecord;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
20
import java.io.ObjectOutputStream;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Random;

import eu.stratosphere.core.io.IOReadableWritable;

public class UID implements IOReadableWritable, Serializable {
	private static final long serialVersionUID = 1L;

	private ByteBuffer uid;
	private static Random random = new Random();

	public UID() {
		uid = ByteBuffer.allocate(20);
	}

	// TODO: consider sequential ids
	public UID(int channelID) {
		byte[] uuid = new byte[16];
		random.nextBytes(uuid);
		uid = ByteBuffer.allocate(20).putInt(channelID).put(uuid);
	}

	UID(byte[] id) {
		// TODO: throw wrong length exception
		uid = ByteBuffer.wrap(id);
	}

	public int getChannelId() {
		uid.position(0);
		return uid.getInt();
	}

	public byte[] getGeneratedId() {
		uid.position(4);
		return uid.slice().array();
	}

	public byte[] getId() {
		uid.position(0);
		return uid.array();
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.write(uid.array());
	}
69 70 71 72 73 74 75 76 77 78 79 80
	
	
	private void writeObject(ObjectOutputStream stream)
	            throws IOException {
	        stream.write(uid.array());
	    }
	 private void readObject(java.io.ObjectInputStream stream)
	            throws IOException, ClassNotFoundException {
		 	byte[] uidA = new byte[20];
	        stream.read(uidA);
	        uid = ByteBuffer.allocate(20).put(uidA);
	    }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	@Override
	public void read(DataInput in) throws IOException {
		byte[] uidByteArray = new byte[20];
		in.readFully(uidByteArray, 0, 20);
		uid = ByteBuffer.wrap(uidByteArray);
	}

	@Override
	public String toString() {
		return getChannelId() + "-" + Long.toHexString(uid.getLong(4)) + "-" + Long.toHexString(uid.getLong(12));
	}

	@Override
	public int hashCode() {
		return Arrays.hashCode(getId());
	}

	@Override
	public boolean equals(Object obj) {
		try {
			UID other = (UID) obj;
			return Arrays.equals(this.getId(), other.getId());
		} catch (ClassCastException e) {
			return false;
		}
	}

	public UID copy() {
		return new UID(Arrays.copyOf(uid.array(), 20));
	}
}