| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.aws.ec2.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.*; |
| 22 | |
| 23 | import org.jclouds.aws.ec2.util.TagFilters.ResourceType; |
| 24 | |
| 25 | import com.google.common.base.Objects; |
| 26 | import com.google.common.collect.ComparisonChain; |
| 27 | |
| 28 | /** |
| 29 | * @see <a href= "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-TagSetItemType.html" /> |
| 30 | * @author grkvlt@apache.org |
| 31 | */ |
| 32 | public class Tag implements Comparable<Tag> { |
| 33 | public static Builder builder() { |
| 34 | return new Builder(); |
| 35 | } |
| 36 | |
| 37 | public static class Builder { |
| 38 | private String resourceId; |
| 39 | private ResourceType resourceType; |
| 40 | private String key; |
| 41 | private String value; |
| 42 | |
| 43 | public void clear() { |
| 44 | this.resourceId = null; |
| 45 | this.resourceType = null; |
| 46 | this.key = null; |
| 47 | this.value = null; |
| 48 | } |
| 49 | |
| 50 | public Builder resourceId(String resourceId) { |
| 51 | this.resourceId = resourceId; |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | public Builder resourceType(ResourceType resourceType) { |
| 56 | this.resourceType = resourceType; |
| 57 | return this; |
| 58 | } |
| 59 | |
| 60 | public Builder key(String key) { |
| 61 | this.key = key; |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | public Builder value(String value) { |
| 66 | this.value = value; |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | public Tag build() { |
| 71 | return new Tag(resourceId, resourceType, key, value); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private final String resourceId; |
| 76 | private final ResourceType resourceType; |
| 77 | private final String key; |
| 78 | private final String value; |
| 79 | |
| 80 | public Tag(String resourceId, ResourceType resourceType, String key, String value) { |
| 81 | this.resourceId = checkNotNull(resourceId, "resourceId"); |
| 82 | this.resourceType = checkNotNull(resourceType, "resourceType"); |
| 83 | this.key = checkNotNull(key, "key"); |
| 84 | this.value = checkNotNull(value, "value"); |
| 85 | } |
| 86 | |
| 87 | public String getResourceId() { |
| 88 | return resourceId; |
| 89 | } |
| 90 | |
| 91 | public ResourceType getResourceType() { |
| 92 | return resourceType; |
| 93 | } |
| 94 | |
| 95 | public String getKey() { |
| 96 | return key; |
| 97 | } |
| 98 | |
| 99 | public String getValue() { |
| 100 | return value; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int compareTo(Tag t) { |
| 105 | return ComparisonChain.start() |
| 106 | .compare(resourceId, t.resourceId) |
| 107 | .compare(resourceType, t.resourceType) |
| 108 | .compare(key, t.key) |
| 109 | .compare(value, t.value) |
| 110 | .result(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public int hashCode() { |
| 115 | return Objects.hashCode(resourceId, resourceType, key, value); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean equals(Object obj) { |
| 120 | if (this == obj) |
| 121 | return true; |
| 122 | if (obj == null) |
| 123 | return false; |
| 124 | if (getClass() != obj.getClass()) |
| 125 | return false; |
| 126 | Tag other = (Tag) obj; |
| 127 | if (resourceId == null) { |
| 128 | if (other.resourceId != null) |
| 129 | return false; |
| 130 | } else if (!resourceId.equals(other.resourceId)) |
| 131 | return false; |
| 132 | if (resourceType == null) { |
| 133 | if (other.resourceType != null) |
| 134 | return false; |
| 135 | } else if (!resourceType.equals(other.resourceType)) |
| 136 | return false; |
| 137 | if (value == null) { |
| 138 | if (other.value != null) |
| 139 | return false; |
| 140 | } else if (!value.equals(other.value)) |
| 141 | return false; |
| 142 | if (key == null) { |
| 143 | if (other.key != null) |
| 144 | return false; |
| 145 | } else if (!key.equals(other.key)) |
| 146 | return false; |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public String toString() { |
| 152 | return "[resourceId=" + resourceId + ", resourceType=" + resourceType + ", key=" + key + ", value=" + value + "]"; |
| 153 | } |
| 154 | } |