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 static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.io.Serializable; |
24 | import java.net.URI; |
25 | import java.util.Map; |
26 | |
27 | import org.jclouds.javax.annotation.Nullable; |
28 | |
29 | import org.jclouds.domain.Location; |
30 | import org.jclouds.domain.ResourceMetadata; |
31 | |
32 | import com.google.common.collect.Maps; |
33 | |
34 | /** |
35 | * Idpayload of the object |
36 | * |
37 | * @author Adrian Cole |
38 | */ |
39 | public abstract class ResourceMetadataImpl<T extends Enum<T>> implements ResourceMetadata<T>, Serializable { |
40 | |
41 | /** The serialVersionUID */ |
42 | private static final long serialVersionUID = -280558162576368264L; |
43 | |
44 | @Nullable |
45 | private final String providerId; |
46 | @Nullable |
47 | private final String name; |
48 | @Nullable |
49 | private final Location location; |
50 | @Nullable |
51 | private final URI uri; |
52 | private final Map<String, String> userMetadata = Maps.newLinkedHashMap(); |
53 | |
54 | public ResourceMetadataImpl(@Nullable String providerId, @Nullable String name, @Nullable Location location, |
55 | @Nullable URI uri, Map<String, String> userMetadata) { |
56 | this.providerId = providerId; |
57 | this.name = name; |
58 | this.location = location; |
59 | this.uri = uri; |
60 | this.userMetadata.putAll(checkNotNull(userMetadata, "userMetadata")); |
61 | } |
62 | |
63 | /** |
64 | * {@inheritDoc} |
65 | */ |
66 | @Override |
67 | public int compareTo(ResourceMetadata<T> o) { |
68 | if (getName() == null) |
69 | return -1; |
70 | return (this == o) ? 0 : getName().compareTo(o.getName()); |
71 | } |
72 | |
73 | /** |
74 | * {@inheritDoc} |
75 | */ |
76 | @Override |
77 | public String getProviderId() { |
78 | return providerId; |
79 | } |
80 | |
81 | /** |
82 | * {@inheritDoc} |
83 | */ |
84 | @Override |
85 | public String getName() { |
86 | return name; |
87 | } |
88 | |
89 | /** |
90 | * {@inheritDoc} |
91 | */ |
92 | @Override |
93 | public Location getLocation() { |
94 | return location; |
95 | } |
96 | |
97 | /** |
98 | * {@inheritDoc} |
99 | */ |
100 | @Override |
101 | public URI getUri() { |
102 | return uri; |
103 | } |
104 | |
105 | /** |
106 | * {@inheritDoc} |
107 | */ |
108 | @Override |
109 | public Map<String, String> getUserMetadata() { |
110 | return userMetadata; |
111 | } |
112 | |
113 | @Override |
114 | public String toString() { |
115 | return "[type=" + getType() + ", providerId=" + providerId + ", name=" + name + ", location=" + location |
116 | + ", uri=" + uri + ", userMetadata=" + userMetadata + "]"; |
117 | } |
118 | |
119 | @Override |
120 | public int hashCode() { |
121 | final int prime = 31; |
122 | int result = 1; |
123 | result = prime * result + ((providerId == null) ? 0 : providerId.hashCode()); |
124 | result = prime * result + ((location == null) ? 0 : location.hashCode()); |
125 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
126 | result = prime * result + ((uri == null) ? 0 : uri.hashCode()); |
127 | return result; |
128 | } |
129 | |
130 | @Override |
131 | public boolean equals(Object obj) { |
132 | if (this == obj) |
133 | return true; |
134 | if (obj == null) |
135 | return false; |
136 | if (getClass() != obj.getClass()) |
137 | return false; |
138 | ResourceMetadataImpl<?> other = (ResourceMetadataImpl<?>) obj; |
139 | if (providerId == null) { |
140 | if (other.providerId != null) |
141 | return false; |
142 | } else if (!providerId.equals(other.providerId)) |
143 | return false; |
144 | if (location == null) { |
145 | if (other.location != null) |
146 | return false; |
147 | } else if (!location.equals(other.location)) |
148 | return false; |
149 | if (name == null) { |
150 | if (other.name != null) |
151 | return false; |
152 | } else if (!name.equals(other.name)) |
153 | return false; |
154 | if (uri == null) { |
155 | if (other.uri != null) |
156 | return false; |
157 | } else if (!uri.equals(other.uri)) |
158 | return false; |
159 | return true; |
160 | } |
161 | |
162 | } |