package org.hive2hive.core.model;

import java.io.Serializable;

import org.hive2hive.core.security.H2HEncryptionUtil;

/**
 * Holds meta data of a chunk in the DHT
 * 
 * @author Seppi
 */
public class MetaChunk implements Serializable {

	private static final long serialVersionUID = -2463290285291070943L;

	private final String chunkId;
	private final byte[] chunkHash;
	private final int index;

	public MetaChunk(String chunkId, byte[] chunkHash, int index) {
		this.chunkId = chunkId;
		this.chunkHash = chunkHash;
		this.index = index;
	}

	/**
	 * The id of a chunk. In a 'small' file, this id serves as the location key. In a 'large' file, this id is
	 * used to ask other clients.
	 * 
	 * @return the chunk id
	 */
	public String getChunkId() {
		return chunkId;
	}

	/**
	 * The hash of the chunk. In a 'small' file, this hash is generated by TomP2P and required here to re-key
	 * a chunk at sharing. In a 'large' file, this hash is used to verify the data sent from another peer.
	 * 
	 * @return the hash
	 */
	public byte[] getChunkHash() {
		return chunkHash;
	}

	/**
	 * The index of the chunk, used to contatenate the file later
	 * 
	 * @return the index
	 */
	public int getIndex() {
		return index;
	}

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

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;

		if (obj instanceof MetaChunk) {
			MetaChunk other = (MetaChunk) obj;
			return other.getChunkId().equalsIgnoreCase(chunkId) && other.getIndex() == index
					&& H2HEncryptionUtil.compareMD5(chunkHash, other.getChunkHash());
		}

		return false;
	}

}

@@ -59,16 +59,42 @@
 izable;%0D
+%0Aimport java.util.Arrays;%0D
 %0A%0D%0Aimpor
@@ -1347,25 +1347,22 @@
 %09return 
-chunkHash
+Arrays
 .hashCod
@@ -1363,16 +1363,25 @@
 ashCode(
+chunkHash
 );%0D%0A%09%7D%0D%0A

fix MetaChunk's hashCode to be semantic equivalent to equals