aQute.bnd.osgi.resource.CapReq Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of biz.aQute.bndlib Show documentation
Show all versions of biz.aQute.bndlib Show documentation
bndlib: A Swiss Army Knife for OSGi
package aQute.bnd.osgi.resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.osgi.resource.Capability;
import org.osgi.resource.Namespace;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
class CapReq {
static enum MODE {
Capability, Requirement
}
private final MODE mode;
private final String namespace;
private final Resource resource;
private final Map directives;
private final Map attributes;
CapReq(MODE mode, String namespace, Resource resource, Map directives,
Map attributes) {
this.mode = mode;
this.namespace = namespace;
this.resource = resource;
this.directives = new HashMap(directives);
this.attributes = new HashMap(attributes);
}
public String getNamespace() {
return namespace;
}
public Map getDirectives() {
return Collections.unmodifiableMap(directives);
}
public Map getAttributes() {
return Collections.unmodifiableMap(attributes);
}
public Resource getResource() {
return resource;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((attributes == null) ? 0 : attributes.hashCode());
result = prime * result + ((directives == null) ? 0 : directives.hashCode());
result = prime * result + ((mode == null) ? 0 : mode.hashCode());
result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
result = prime * result + ((resource == null) ? 0 : resource.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (obj instanceof CapReq)
return equalsNative((CapReq) obj);
else if ((mode == MODE.Capability) && (obj instanceof Capability))
return equalsCap((Capability) obj);
else if ((mode == MODE.Requirement) && (obj instanceof Requirement))
return equalsReq((Requirement) obj);
return false;
}
private boolean equalsCap(Capability other) {
if (namespace == null) {
if (other.getNamespace() != null)
return false;
} else if (!namespace.equals(other.getNamespace()))
return false;
if (attributes == null) {
if (other.getAttributes() != null)
return false;
} else if (!attributes.equals(other.getAttributes()))
return false;
if (directives == null) {
if (other.getDirectives() != null)
return false;
} else if (!directives.equals(other.getDirectives()))
return false;
if (resource == null) {
if (other.getResource() != null)
return false;
} else if (!resource.equals(other.getResource()))
return false;
return true;
}
private boolean equalsNative(CapReq other) {
if (mode != other.mode)
return false;
if (namespace == null) {
if (other.namespace != null)
return false;
} else if (!namespace.equals(other.namespace))
return false;
if (attributes == null) {
if (other.attributes != null)
return false;
} else if (!attributes.equals(other.attributes))
return false;
if (directives == null) {
if (other.directives != null)
return false;
} else if (!directives.equals(other.directives))
return false;
if (resource == null) {
if (other.resource != null)
return false;
} else if (!resource.equals(other.resource))
return false;
return true;
}
private boolean equalsReq(Requirement other) {
if (namespace == null) {
if (other.getNamespace() != null)
return false;
} else if (!namespace.equals(other.getNamespace()))
return false;
if (attributes == null) {
if (other.getAttributes() != null)
return false;
} else if (!attributes.equals(other.getAttributes()))
return false;
if (directives == null) {
if (other.getDirectives() != null)
return false;
} else if (!directives.equals(other.getDirectives()))
return false;
if (resource == null) {
if (other.getResource() != null)
return false;
} else if (!resource.equals(other.getResource()))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (mode == MODE.Capability) {
Object value = attributes.get(namespace);
builder.append(namespace).append('=').append(value);
} else {
String filter = directives.get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
builder.append(filter);
if (Namespace.RESOLUTION_OPTIONAL.equals(directives.get(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE))) {
builder.append("%OPT");
}
}
return builder.toString();
}
protected void toString(StringBuilder sb) {
sb.append("[").append(namespace).append("]");
sb.append(attributes);
sb.append(directives);
}
}