1 11 package org.eclipse.core.internal.registry; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.InvalidRegistryObjectException; 15 16 public class Contribution implements KeyedElement { 20 static final int[] EMPTY_CHILDREN = new int[] {0, 0}; 21 22 protected ExtensionRegistry registry; 24 25 final protected String contributorId; 27 28 private String defaultNamespace = null; 30 31 protected boolean persist; 33 34 private int[] children = EMPTY_CHILDREN; 40 static final public byte EXTENSION_POINT = 0; 41 static final public byte EXTENSION = 1; 42 43 protected Contribution(String contributorId, ExtensionRegistry registry, boolean persist) { 44 this.contributorId = contributorId; 45 this.registry = registry; 46 this.persist = persist; 47 } 48 49 void mergeContribution(Contribution addContribution) { 50 Assert.isTrue(contributorId.equals(addContribution.contributorId)); 51 Assert.isTrue(registry == addContribution.registry); 52 53 if (shouldPersist() != addContribution.shouldPersist()) 60 persist = true; 61 62 int[] existing = getRawChildren(); 63 int[] addition = addContribution.getRawChildren(); 64 65 int extensionPoints = existing[EXTENSION_POINT] + addition[EXTENSION_POINT]; 66 int extensions = existing[EXTENSION] + addition[EXTENSION]; 67 int[] allChildren = new int[2 + extensionPoints + extensions]; 68 69 allChildren[EXTENSION_POINT] = extensionPoints; 70 System.arraycopy(existing, 2, allChildren, 2, existing[EXTENSION_POINT]); 71 System.arraycopy(addition, 2, allChildren, 2 + existing[EXTENSION_POINT], addition[EXTENSION_POINT]); 72 allChildren[EXTENSION] = extensions; 73 System.arraycopy(existing, 2 + existing[EXTENSION_POINT], allChildren, 2 + extensionPoints, existing[EXTENSION]); 74 System.arraycopy(addition, 2 + addition[EXTENSION_POINT], allChildren, 2 + extensionPoints + existing[EXTENSION], addition[EXTENSION]); 75 76 children = allChildren; 77 } 78 79 void setRawChildren(int[] children) { 80 this.children = children; 81 } 82 83 protected String getContributorId() { 84 return contributorId; 85 } 86 87 protected int[] getRawChildren() { 88 return children; 89 } 90 91 protected int[] getExtensions() { 92 int[] results = new int[children[EXTENSION]]; 93 System.arraycopy(children, 2 + children[EXTENSION_POINT], results, 0, children[EXTENSION]); 94 return results; 95 } 96 97 protected int[] getExtensionPoints() { 98 int[] results = new int[children[EXTENSION_POINT]]; 99 System.arraycopy(children, 2, results, 0, children[EXTENSION_POINT]); 100 return results; 101 } 102 103 public String getDefaultNamespace() { 104 if (defaultNamespace == null) 105 defaultNamespace = registry.getObjectManager().getContributor(contributorId).getName(); 106 return defaultNamespace; 107 } 108 109 public String toString() { 110 return "Contribution: " + contributorId + " in namespace" + getDefaultNamespace(); } 112 113 public int getKeyHashCode() { 115 return getKey().hashCode(); 116 } 117 118 public Object getKey() { 119 return contributorId; 120 } 121 122 public boolean compare(KeyedElement other) { 123 return contributorId.equals(((Contribution) other).contributorId); 124 } 125 126 public boolean shouldPersist() { 127 return persist; 128 } 129 130 public void unlinkChild(int id) { 131 int index = -1; 133 for (int i = 2; i < children.length; i++) { 134 if (children[i] == id) { 135 index = i; 136 break; 137 } 138 } 139 if (index == -1) 140 throw new InvalidRegistryObjectException(); 141 142 int[] result = new int[children.length - 1]; 144 System.arraycopy(children, 0, result, 0, index); 145 System.arraycopy(children, index + 1, result, index, children.length - index - 1); 146 147 if (index < children[EXTENSION_POINT] + 2) 149 result[EXTENSION_POINT]--; 150 else 151 result[EXTENSION]--; 152 153 children = result; 154 } 155 156 159 public boolean isEmpty() { 160 return (children[EXTENSION_POINT] == 0 || children[EXTENSION] == 0); 161 } 162 163 168 public boolean hasChild(int id) { 169 for (int i = 2; i < children.length; i++) { 170 if (children[i] == id) 171 return true; 172 } 173 return false; 174 } 175 } 176 | Popular Tags |