blob: 6b96e5ea88a6a24e1a62159e36cc759a45441432 [file] [log] [blame]
/*******************************************************************************
* Copyright (C) 2015, Obeo.
*
* 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
*******************************************************************************/
package org.eclipse.emf.compare.egit.internal.storage;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.team.core.variants.IResourceVariant;
// CHECKSTYLE:OFF
/**
* Base class of the git-related resource variants.
*
* @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
*/
public abstract class AbstractGitResourceVariant implements IResourceVariant {
/** Repository in which this variant's content will be accessed. */
protected final Repository repository;
/** Repository-relative path of this resource. */
protected final String path;
/**
* Whether this resource is a container or not in this particular variant. This may be different than the
* local resource's state (if there is a file/folder conflict for example).
*/
protected final boolean isContainer;
/** Object id of this variant in its repository. */
protected final ObjectId objectId;
/** Raw mode bits of this variant. */
protected final int rawMode;
/**
* @param repository
* Repository in which this variant's content will be accessed.
* @param path
* Repository-relative path of this resource.
* @param isContainer
* Whether this resource is a container or not in this particular variant.
* @param objectId
* Object id of this variant in its repository.
* @param rawMode
* Raw mode bits of this variant.
*/
protected AbstractGitResourceVariant(Repository repository, String path, boolean isContainer,
ObjectId objectId, int rawMode) {
this.repository = repository;
this.path = path;
this.isContainer = isContainer;
this.objectId = objectId;
this.rawMode = rawMode;
}
public String getName() {
int lastSeparator = path.lastIndexOf('/');
return path.substring(lastSeparator + 1);
}
public String getPath() {
return path;
}
public boolean isContainer() {
return isContainer;
}
public String getContentIdentifier() {
return objectId.name();
}
public byte[] asBytes() {
return objectId.name().getBytes();
}
/**
* @return the object id of this variant in its backing repository.
*/
public ObjectId getObjectId() {
return objectId;
}
/**
* @return the raw mode of this variant.
*/
public int getRawMode() {
return rawMode;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof AbstractGitResourceVariant) {
AbstractGitResourceVariant other = (AbstractGitResourceVariant)obj;
return this.path.equals(other.path) && this.repository.equals(other.repository)
&& this.objectId.equals(other.objectId);
}
return false;
}
@Override
public int hashCode() {
int hash = 37;
hash = 37 * hash + (path != null ? path.hashCode() : 0);
hash = 37 * hash + (repository != null ? repository.hashCode() : 0);
hash = 37 * hash + (objectId != null ? objectId.hashCode() : 0);
return hash;
}
}
// CHECKSTYLE:ON