package org.eclipse.core.internal.registry;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
public class Contribution implements KeyedElement {
static final int[] EMPTY_CHILDREN = new int[] {0, 0};
protected ExtensionRegistry registry;
final protected String contributorId;
private String defaultNamespace = null;
protected boolean persist;
private int[] children = EMPTY_CHILDREN;
static final public byte EXTENSION_POINT = 0;
static final public byte EXTENSION = 1;
protected Contribution(String contributorId, ExtensionRegistry registry, boolean persist) {
this.contributorId = contributorId;
this.registry = registry;
this.persist = persist;
}
void mergeContribution(Contribution addContribution) {
Assert.isTrue(contributorId.equals(addContribution.contributorId));
Assert.isTrue(registry == addContribution.registry);
if (shouldPersist() != addContribution.shouldPersist())
persist = true;
int[] existing = getRawChildren();
int[] addition = addContribution.getRawChildren();
int extensionPoints = existing[EXTENSION_POINT] + addition[EXTENSION_POINT];
int extensions = existing[EXTENSION] + addition[EXTENSION];
int[] allChildren = new int[2 + extensionPoints + extensions];
allChildren[EXTENSION_POINT] = extensionPoints;
System.arraycopy(existing, 2, allChildren, 2, existing[EXTENSION_POINT]);
System.arraycopy(addition, 2, allChildren, 2 + existing[EXTENSION_POINT], addition[EXTENSION_POINT]);
allChildren[EXTENSION] = extensions;
System.arraycopy(existing, 2 + existing[EXTENSION_POINT], allChildren, 2 + extensionPoints, existing[EXTENSION]);
System.arraycopy(addition, 2 + addition[EXTENSION_POINT], allChildren, 2 + extensionPoints + existing[EXTENSION], addition[EXTENSION]);
children = allChildren;
}
void setRawChildren(int[] children) {
this.children = children;
}
protected String getContributorId() {
return contributorId;
}
protected int[] getRawChildren() {
return children;
}
protected int[] getExtensions() {
int[] results = new int[children[EXTENSION]];
System.arraycopy(children, 2 + children[EXTENSION_POINT], results, 0, children[EXTENSION]);
return results;
}
protected int[] getExtensionPoints() {
int[] results = new int[children[EXTENSION_POINT]];
System.arraycopy(children, 2, results, 0, children[EXTENSION_POINT]);
return results;
}
public String getDefaultNamespace() {
if (defaultNamespace == null)
defaultNamespace = registry.getObjectManager().getContributor(contributorId).getName();
return defaultNamespace;
}
@Override
public String toString() {
return "Contribution: " + contributorId + " in namespace" + getDefaultNamespace();
}
@Override
public int getKeyHashCode() {
return getKey().hashCode();
}
@Override
public Object getKey() {
return contributorId;
}
@Override
public boolean compare(KeyedElement other) {
return contributorId.equals(((Contribution) other).contributorId);
}
public boolean shouldPersist() {
return persist;
}
public void unlinkChild(int id) {
int index = -1;
for (int i = 2; i < children.length; i++) {
if (children[i] == id) {
index = i;
break;
}
}
if (index == -1)
throw new InvalidRegistryObjectException();
int[] result = new int[children.length - 1];
System.arraycopy(children, 0, result, 0, index);
System.arraycopy(children, index + 1, result, index, children.length - index - 1);
if (index < children[EXTENSION_POINT] + 2)
result[EXTENSION_POINT]--;
else
result[EXTENSION]--;
children = result;
}
public boolean isEmpty() {
return (children[EXTENSION_POINT] == 0 || children[EXTENSION] == 0);
}
public boolean hasChild(int id) {
for (int i = 2; i < children.length; i++) {
if (children[i] == id)
return true;
}
return false;
}
}