/** Returns a copy of this id with namespace set to null **/ public ComponentId withoutNamespace() { return new ComponentId(getName(), getVersion(), null); }
/** * Registers a component unless this registry is frozen. * This will succeed even if this component name and version is already registered. * * @throws IllegalStateException if this chain is frozen */ public void register(ComponentId id, COMPONENT component) { if (frozen) throw new IllegalStateException("Cannot modify a frozen component registry"); Map<String, Map<Version, COMPONENT>> componentVersionsByName = componentsByNameByNamespace.get(id.getNamespace()); if (componentVersionsByName == null) { componentVersionsByName = new LinkedHashMap<>(); componentsByNameByNamespace.put(id.getNamespace(), componentVersionsByName); } Map<Version, COMPONENT> componentVersions = componentVersionsByName.get(id.getName()); if (componentVersions == null) { componentVersions = new LinkedHashMap<>(); componentVersionsByName.put(id.getName(), componentVersions); } componentVersions.put(id.getVersion(), component); componentsById.put(id, component); }
/** * Unregisters a component unless this registry is frozen. * Note that the component is not deconstructed or otherwise modified in any way, this * is the responsiblity of the caller. * * @param id the id of the component to be unregistered * @return the component that was unregistered, or null if no such component was already registered */ public COMPONENT unregister(ComponentId id) { if (frozen) throw new IllegalStateException("Cannot modify a frozen component registry"); COMPONENT removed = componentsById.remove(id); if (removed != null) { //removed is non-null, so it must be present here as well: Map<String, Map<Version, COMPONENT>> componentVersionsByName = componentsByNameByNamespace.get(id.getNamespace()); Map<Version, COMPONENT> componentVersions = componentVersionsByName.get(id.getName()); COMPONENT removedInner = componentVersions.remove(id.getVersion()); assert (removedInner == removed); //clean up if (componentVersions.isEmpty()) { componentVersionsByName.remove(id.getName()); } if (componentVersionsByName.isEmpty()) { componentsByNameByNamespace.remove(id.getNamespace()); } } return removed; }
public ComponentSpecification toSpecification() { if (isAnonymous()) throw new RuntimeException("Can't generate a specification for an anonymous component id."); return new ComponentSpecification(getName(), getVersion().toSpecification(), getNamespace()); }
/** * Checks if a componentId matches a given spec */ public boolean matches(ComponentId id) { boolean versionMatch = getVersionSpecification().matches(id.getVersion()); return getName().equals(id.getName()) && versionMatch && namespaceMatch(id.getNamespace()); }
public ComponentId nestInNamespace(ComponentId namespace) { if (namespace == null) { return this; } else { ComponentId newNamespace = getNamespace() == null ? namespace : getNamespace().nestInNamespace(namespace); return new ComponentId(getName(), getVersion(), newNamespace); } }