/*******************************************************************************
 * Copyright (c) 2005-2010 VecTrace (Zingo Andersen) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * john	implementation
 *******************************************************************************/
package com.vectrace.MercurialEclipse.model;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;

import com.vectrace.MercurialEclipse.exception.HgException;
import com.vectrace.MercurialEclipse.team.cache.LocalChangesetCache;

/**
 *
 */
public abstract class HgRevisionResource extends HgResource implements IChangeSetHolder {

	/**
	 * If a local resource then then null
	 */
	protected final JHgChangeSet changeset;

	// constructors

	/**
	 * @param root the HgRoot, not null
	 * @param changeset is null if and only if resource != null, or this is a NullHgFile
	 * @param path relative path to HgRoot, not null
	 */
	public HgRevisionResource(HgRoot root, String changeset, IPath path) throws HgException {
		super(root, path.removeTrailingSeparator());

		LocalChangesetCache cache = LocalChangesetCache.getInstance();

		this.changeset = cache.get(root, changeset);

		Assert.isNotNull(this.changeset);
	}

	/**
	 * @param root the HgRoot, not null
	 * @param changeset =null if and only if wResource != null or this is a NullHgFile
	 * @param path relative path to HgRoot, not null
	 */
	public HgRevisionResource(HgRoot root, JHgChangeSet changeset, IPath path) {
		super(root, path.removeTrailingSeparator());

		this.changeset = changeset;
	}

	// operations

	public final JHgChangeSet getChangeSet() {
		return changeset;
	}

	/**
	 * @see com.vectrace.MercurialEclipse.model.IHgResource#getName()
	 */
	public String getName() {
		String name = path.lastSegment();
		if(name == null) {
			return root.getIPath().lastSegment();
		}
		return name;
	}

	/**
	 * @see com.vectrace.MercurialEclipse.model.IHgResource#isReadOnly()
	 */
	public final boolean isReadOnly() {
		return true;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((changeset == null) ? 0 : changeset.hashCode());
		return result;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		HgRevisionResource other = (HgRevisionResource) obj;
		if (changeset == null) {
			if (other.changeset != null) {
				return false;
			}
		} else if (!changeset.equals(other.changeset)) {
			return false;
		}
		return true;
	}
}

@@ -636,16 +636,53 @@
 IPath;%0A%0A
+import com.aragost.javahg.Changeset;%0A
 import c
@@ -733,16 +733,16 @@
 eption;%0A
-
 import c
@@ -918,37 +918,34 @@
 %09 * 
-If a local resource then then
+Not null. TODO: verify not
  nul
@@ -1395,24 +1395,130 @@
 nstance();%0A%0A
+%09%09if (Changeset.NULL_ID.equals(changeset)) %7B%0A%09%09%09this.changeset = JHgChangeSet.makeNull(root);%0A%09%09%7D else %7B%0A%09
 %09%09this.chang
@@ -1551,16 +1551,20 @@
 ngeset);
+%0A%09%09%7D
 %0A%0A%09%09Asse
@@ -1904,16 +1904,16 @@
 or());%0A%0A
-
 %09%09this.c
@@ -1929,24 +1929,98 @@
 = changeset;
+%0A%0A%09%09// TODO: Assert.isNotNull(this.changeset);%0A%09%09assert changeset != null;
 %0A%09%7D%0A%0A%09// ope

Fix exception comparing at null revision. Fixes #371