tikhomirov@20: /*
tikhomirov@74:  * Copyright (c) 2010-2011 TMate Software Ltd
tikhomirov@74:  *  
tikhomirov@74:  * This program is free software; you can redistribute it and/or modify
tikhomirov@74:  * it under the terms of the GNU General Public License as published by
tikhomirov@74:  * the Free Software Foundation; version 2 of the License.
tikhomirov@74:  *
tikhomirov@74:  * This program is distributed in the hope that it will be useful,
tikhomirov@74:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@74:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
tikhomirov@74:  * GNU General Public License for more details.
tikhomirov@74:  *
tikhomirov@74:  * For information on how to redistribute this software under
tikhomirov@74:  * the terms of a license other than GNU General Public License
tikhomirov@102:  * contact TMate Software at support@hg4j.com
tikhomirov@0:  */
tikhomirov@74: package org.tmatesoft.hg.core;
tikhomirov@0: 
tikhomirov@74: import static org.tmatesoft.hg.internal.DigestHelper.toHexString;
tikhomirov@31: 
tikhomirov@24: import java.util.Arrays;
tikhomirov@24: 
tikhomirov@266: import org.tmatesoft.hg.internal.DigestHelper;
tikhomirov@266: 
tikhomirov@20: 
tikhomirov@0: 
tikhomirov@0: /**
tikhomirov@74:  * A 20-bytes (40 characters) long hash value to identify a revision.
tikhomirov@74:  * @see http://mercurial.selenic.com/wiki/Nodeid
tikhomirov@74:  *
tikhomirov@74:  * @author Artem Tikhomirov
tikhomirov@74:  * @author TMate Software Ltd.
tikhomirov@0:  *
tikhomirov@0:  */
tikhomirov@170: public final class Nodeid implements Comparable<Nodeid> {
tikhomirov@0: 	
tikhomirov@74: 	/**
tikhomirov@74: 	 * <b>nullid</b>, empty root revision.
tikhomirov@74: 	 */
tikhomirov@31: 	public static final Nodeid NULL = new Nodeid(new byte[20], false);
tikhomirov@74: 
tikhomirov@0: 	private final byte[] binaryData; 
tikhomirov@0: 
tikhomirov@24: 	/**
tikhomirov@74: 	 * @param binaryRepresentation - array of exactly 20 bytes
tikhomirov@148: 	 * @param shallClone - true if array is subject to future modification and shall be copied, not referenced
tikhomirov@148: 	 * @throws IllegalArgumentException if supplied binary representation doesn't correspond to 20 bytes of sha1 digest 
tikhomirov@24: 	 */
tikhomirov@28: 	public Nodeid(byte[] binaryRepresentation, boolean shallClone) {
tikhomirov@0: 		// 5 int fields => 32 bytes
tikhomirov@197: 		// byte[20] => 48 bytes (16 bytes is Nodeid with one field, 32 bytes for byte[20] 
tikhomirov@24: 		if (binaryRepresentation == null || binaryRepresentation.length != 20) {
tikhomirov@24: 			throw new IllegalArgumentException();
tikhomirov@24: 		}
tikhomirov@197: 		/*
tikhomirov@197: 		 * byte[].clone() is not reflected when ran with -agentlib:hprof=heap=sites
tikhomirov@197: 		 * thus not to get puzzled why there are N Nodeids and much less byte[] instances,
tikhomirov@197: 		 * may use following code to see N byte[] as well.
tikhomirov@197: 		 *
tikhomirov@197: 		if (shallClone) {
tikhomirov@197: 			binaryData = new byte[20];
tikhomirov@197: 			System.arraycopy(binaryRepresentation, 0, binaryData, 0, 20);
tikhomirov@197: 		} else {
tikhomirov@197: 			binaryData = binaryRepresentation;
tikhomirov@197: 		}
tikhomirov@197: 		*/
tikhomirov@197: 		binaryData = shallClone ? binaryRepresentation.clone() : binaryRepresentation;
tikhomirov@0: 	}
tikhomirov@0: 
tikhomirov@24: 	@Override
tikhomirov@24: 	public int hashCode() {
tikhomirov@46: 		// digest (part thereof) seems to be nice candidate for the hashCode
tikhomirov@46: 		byte[] b = binaryData;
tikhomirov@46: 		return b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF);
tikhomirov@24: 	}
tikhomirov@24: 	
tikhomirov@24: 	@Override
tikhomirov@24: 	public boolean equals(Object o) {
tikhomirov@24: 		if (o instanceof Nodeid) {
tikhomirov@125: 			return this == o || equalsTo(((Nodeid) o).binaryData);
tikhomirov@24: 		}
tikhomirov@24: 		return false;
tikhomirov@22: 	}
tikhomirov@22: 
tikhomirov@22: 	public boolean equalsTo(byte[] buf) {
tikhomirov@24: 		return Arrays.equals(this.binaryData, buf);
tikhomirov@22: 	}
tikhomirov@22: 	
tikhomirov@170: 	public int compareTo(Nodeid o) {
tikhomirov@170: 		if (this == o) {
tikhomirov@170: 			return 0;
tikhomirov@170: 		}
tikhomirov@170: 		for (int i = 0; i < 20; i++) {
tikhomirov@170: 			if (binaryData[i] != o.binaryData[i]) {
tikhomirov@170: 				return binaryData[i] < o.binaryData[i] ? -1 : 1;
tikhomirov@170: 			}
tikhomirov@170: 		}
tikhomirov@170: 		return 0;
tikhomirov@170: 	}
tikhomirov@244: 
tikhomirov@244: 	/**
tikhomirov@244: 	 * Complete string representation of this Nodeid.
tikhomirov@244: 	 */
tikhomirov@0: 	@Override
tikhomirov@0: 	public String toString() {
tikhomirov@34: 		// XXX may want to output just single 0 for the NULL id?
tikhomirov@31: 		return toHexString(binaryData, 0, binaryData.length);
tikhomirov@0: 	}
tikhomirov@31: 
tikhomirov@31: 	public String shortNotation() {
tikhomirov@31: 		return toHexString(binaryData, 0, 6);
tikhomirov@31: 	}
tikhomirov@34: 	
tikhomirov@34: 	public boolean isNull() {
tikhomirov@34: 		if (this == NULL) {
tikhomirov@34: 			return true;
tikhomirov@34: 		}
tikhomirov@34: 		for (int i = 0; i < 20; i++) {
tikhomirov@34: 			if (this.binaryData[i] != 0) {
tikhomirov@34: 				return false;
tikhomirov@34: 			}
tikhomirov@34: 		}
tikhomirov@34: 		return true;
tikhomirov@34: 	}
tikhomirov@34: 
tikhomirov@74: 	// copy 
tikhomirov@74: 	public byte[] toByteArray() {
tikhomirov@41: 		return binaryData.clone();
tikhomirov@41: 	}
tikhomirov@41: 
tikhomirov@148: 	/**
tikhomirov@148: 	 * Factory for {@link Nodeid Nodeids}.
tikhomirov@148: 	 * Primary difference with cons is handling of NULL id (this method returns constant) and control over array 
tikhomirov@148: 	 * duplication - this method always makes a copy of an array passed
tikhomirov@148: 	 * @param binaryRepresentation - byte array of a length at least offset + 20
tikhomirov@148: 	 * @param offset - index in the array to start from
tikhomirov@148: 	 * @throws IllegalArgumentException when arguments don't select 20 bytes
tikhomirov@148: 	 */
tikhomirov@34: 	public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) {
tikhomirov@34: 		if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) {
tikhomirov@34: 			throw new IllegalArgumentException();
tikhomirov@34: 		}
tikhomirov@34: 		int i = 0;
tikhomirov@34: 		while (i < 20 && binaryRepresentation[offset+i] == 0) i++;
tikhomirov@34: 		if (i == 20) {
tikhomirov@34: 			return NULL;
tikhomirov@34: 		}
tikhomirov@34: 		if (offset == 0 && binaryRepresentation.length == 20) {
tikhomirov@34: 			return new Nodeid(binaryRepresentation, true);
tikhomirov@34: 		}
tikhomirov@34: 		byte[] b = new byte[20]; // create new instance if no other reasonable guesses possible
tikhomirov@34: 		System.arraycopy(binaryRepresentation, offset, b, 0, 20);
tikhomirov@34: 		return new Nodeid(b, false);
tikhomirov@34: 	}
tikhomirov@31: 
tikhomirov@148: 	/**
tikhomirov@148: 	 * Parse encoded representation.
tikhomirov@148: 	 * 
tikhomirov@148: 	 * @param asciiRepresentation - encoded form of the Nodeid.
tikhomirov@148: 	 * @return object representation
tikhomirov@148: 	 * @throws IllegalArgumentException when argument doesn't match encoded form of 20-bytes sha1 digest. 
tikhomirov@148: 	 */
tikhomirov@78: 	public static Nodeid fromAscii(String asciiRepresentation) {
tikhomirov@78: 		if (asciiRepresentation.length() != 40) {
tikhomirov@78: 			throw new IllegalArgumentException();
tikhomirov@78: 		}
tikhomirov@78: 		// XXX is better impl for String possible?
tikhomirov@262: 		return fromAscii(asciiRepresentation.toCharArray(), 0, 40);
tikhomirov@78: 	}
tikhomirov@261: 	
tikhomirov@148: 	/**
tikhomirov@148: 	 * Parse encoded representation. Similar to {@link #fromAscii(String)}.
tikhomirov@148: 	 */
tikhomirov@0: 	public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) {
tikhomirov@24: 		if (length != 40) {
tikhomirov@24: 			throw new IllegalArgumentException();
tikhomirov@24: 		}
tikhomirov@24: 		byte[] data = new byte[20];
tikhomirov@266: 		boolean zeroBytes = DigestHelper.ascii2bin(asciiRepresentation, offset, length, data);
tikhomirov@31: 		if (zeroBytes) {
tikhomirov@31: 			return NULL;
tikhomirov@0: 		}
tikhomirov@28: 		return new Nodeid(data, false);
tikhomirov@0: 	}
tikhomirov@262: 	
tikhomirov@262: 	public static Nodeid fromAscii(char[] asciiRepresentation, int offset, int length) {
tikhomirov@262: 		byte[] b = new byte[length];
tikhomirov@262: 		for (int i = 0; i < b.length; i++) {
tikhomirov@262: 			b[i] = (byte) asciiRepresentation[offset+i];
tikhomirov@262: 		}
tikhomirov@262: 		return fromAscii(b, 0, b.length);
tikhomirov@262: 	}
tikhomirov@0: }