| 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.domain.internal; |
| 20 | |
| 21 | import java.io.Serializable; |
| 22 | import java.net.URI; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.jclouds.domain.Location; |
| 26 | import org.jclouds.domain.MutableResourceMetadata; |
| 27 | import org.jclouds.domain.ResourceMetadata; |
| 28 | |
| 29 | import com.google.common.collect.Maps; |
| 30 | |
| 31 | /** |
| 32 | * Used to construct new resources or modify existing ones. |
| 33 | * |
| 34 | * @author Adrian Cole |
| 35 | */ |
| 36 | public class MutableResourceMetadataImpl<T extends Enum<T>> implements MutableResourceMetadata<T>, |
| 37 | Serializable { |
| 38 | |
| 39 | /** The serialVersionUID */ |
| 40 | private static final long serialVersionUID = -280558162576368264L; |
| 41 | |
| 42 | private T type; |
| 43 | private String id; |
| 44 | private String name; |
| 45 | private Location location; |
| 46 | private URI uri; |
| 47 | private Map<String, String> userMetadata; |
| 48 | |
| 49 | public MutableResourceMetadataImpl() { |
| 50 | userMetadata = Maps.newLinkedHashMap(); |
| 51 | } |
| 52 | |
| 53 | public MutableResourceMetadataImpl(ResourceMetadata<T> from) { |
| 54 | this.type = from.getType(); |
| 55 | this.id = from.getProviderId(); |
| 56 | this.name = from.getName(); |
| 57 | this.location = from.getLocation(); |
| 58 | this.uri = from.getUri(); |
| 59 | this.userMetadata = from.getUserMetadata(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * {@inheritDoc} |
| 64 | */ |
| 65 | @Override |
| 66 | public int compareTo(ResourceMetadata<T> o) { |
| 67 | if (getName() == null) |
| 68 | return -1; |
| 69 | return (this == o) ? 0 : getName().compareTo(o.getName()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * {@inheritDoc} |
| 74 | */ |
| 75 | @Override |
| 76 | public T getType() { |
| 77 | return type; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritDoc} |
| 82 | */ |
| 83 | @Override |
| 84 | public String getName() { |
| 85 | return name; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | */ |
| 91 | @Override |
| 92 | public String getProviderId() { |
| 93 | return id; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * {@inheritDoc} |
| 98 | */ |
| 99 | @Override |
| 100 | public URI getUri() { |
| 101 | return uri; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritDoc} |
| 106 | */ |
| 107 | @Override |
| 108 | public Map<String, String> getUserMetadata() { |
| 109 | return userMetadata; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * {@inheritDoc} |
| 114 | */ |
| 115 | @Override |
| 116 | public void setName(String name) { |
| 117 | this.name = name; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * {@inheritDoc} |
| 122 | */ |
| 123 | @Override |
| 124 | public void setType(T type) { |
| 125 | this.type = type; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * {@inheritDoc} |
| 130 | */ |
| 131 | @Override |
| 132 | public void setUserMetadata(Map<String, String> userMetadata) { |
| 133 | this.userMetadata = userMetadata; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * {@inheritDoc} |
| 138 | */ |
| 139 | @Override |
| 140 | public void setId(String id) { |
| 141 | this.id = id; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * {@inheritDoc} |
| 146 | */ |
| 147 | @Override |
| 148 | public void setUri(URI uri) { |
| 149 | this.uri = uri; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * {@inheritDoc} |
| 154 | */ |
| 155 | @Override |
| 156 | public void setLocation(Location location) { |
| 157 | this.location = location; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * {@inheritDoc} |
| 162 | */ |
| 163 | @Override |
| 164 | public Location getLocation() { |
| 165 | return location; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public String toString() { |
| 170 | return "[type=" + type + ", id=" + id + ", name=" + name + ", location=" + location |
| 171 | + ", uri=" + uri + ", userMetadata=" + userMetadata + "]"; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public int hashCode() { |
| 176 | final int prime = 31; |
| 177 | int result = 1; |
| 178 | result = prime * result + ((id == null) ? 0 : id.hashCode()); |
| 179 | result = prime * result + ((location == null) ? 0 : location.hashCode()); |
| 180 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 181 | result = prime * result + ((type == null) ? 0 : type.hashCode()); |
| 182 | result = prime * result + ((uri == null) ? 0 : uri.hashCode()); |
| 183 | return result; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public boolean equals(Object obj) { |
| 188 | if (this == obj) |
| 189 | return true; |
| 190 | if (obj == null) |
| 191 | return false; |
| 192 | if (!(obj instanceof MutableResourceMetadata<?>)) |
| 193 | return false; |
| 194 | MutableResourceMetadata<?> other = (MutableResourceMetadata<?>) obj; |
| 195 | if (id == null) { |
| 196 | if (other.getProviderId() != null) |
| 197 | return false; |
| 198 | } else if (!id.equals(other.getProviderId())) |
| 199 | return false; |
| 200 | if (location == null) { |
| 201 | if (other.getLocation() != null) |
| 202 | return false; |
| 203 | } else if (!location.equals(other.getLocation())) |
| 204 | return false; |
| 205 | if (name == null) { |
| 206 | if (other.getName() != null) |
| 207 | return false; |
| 208 | } else if (!name.equals(other.getName())) |
| 209 | return false; |
| 210 | if (type == null) { |
| 211 | if (other.getType() != null) |
| 212 | return false; |
| 213 | } else if (!type.equals(other.getType())) |
| 214 | return false; |
| 215 | if (uri == null) { |
| 216 | if (other.getUri() != null) |
| 217 | return false; |
| 218 | } else if (!uri.equals(other.getUri())) |
| 219 | return false; |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | } |