Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 1 | package aQute.lib.deployer.obr; |
| 2 | |
| 3 | import java.util.Collections; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | public class Capability { |
| 8 | |
| 9 | private final String name; |
| 10 | private final List<Property> properties; |
| 11 | |
| 12 | private Capability(String name, List<Property> properties) { |
| 13 | this.name = name; |
| 14 | this.properties = properties; |
| 15 | } |
| 16 | |
| 17 | public static class Builder { |
| 18 | private String name; |
| 19 | private final List<Property> properties = new LinkedList<Property>(); |
| 20 | |
| 21 | public Builder setName(String name) { |
| 22 | this.name = name; |
| 23 | return this; |
| 24 | } |
| 25 | |
| 26 | public Builder addProperty(Property property) { |
| 27 | this.properties.add(property); |
| 28 | return this; |
| 29 | } |
| 30 | |
| 31 | public Capability build() { |
| 32 | if (name == null) throw new IllegalStateException("'name' field is not initialised."); |
| 33 | return new Capability(name, Collections.unmodifiableList(properties)); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public String getName() { |
| 38 | return name; |
| 39 | } |
| 40 | |
| 41 | public List<Property> getProperties() { |
| 42 | return properties; |
| 43 | } |
| 44 | |
| 45 | public Property findProperty(String propertyName) { |
| 46 | assert propertyName != null; |
| 47 | for (Property prop : properties) { |
| 48 | if (propertyName.equals(prop.getName())) |
| 49 | return prop; |
| 50 | } |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String toString() { |
| 56 | StringBuilder builder = new StringBuilder(); |
| 57 | builder.append("Capability [name=").append(name).append(", properties=").append(properties).append("]"); |
| 58 | return builder.toString(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public int hashCode() { |
| 63 | final int prime = 31; |
| 64 | int result = 1; |
| 65 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 66 | result = prime * result |
| 67 | + ((properties == null) ? 0 : properties.hashCode()); |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public boolean equals(Object obj) { |
| 73 | if (this == obj) |
| 74 | return true; |
| 75 | if (obj == null) |
| 76 | return false; |
| 77 | if (getClass() != obj.getClass()) |
| 78 | return false; |
| 79 | Capability other = (Capability) obj; |
| 80 | if (name == null) { |
| 81 | if (other.name != null) |
| 82 | return false; |
| 83 | } else if (!name.equals(other.name)) |
| 84 | return false; |
| 85 | if (properties == null) { |
| 86 | if (other.properties != null) |
| 87 | return false; |
| 88 | } else if (!properties.equals(other.properties)) |
| 89 | return false; |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | } |