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.ec2.domain; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.Set; |
24 | |
25 | import com.google.common.collect.ImmutableMultimap; |
26 | import com.google.common.collect.ImmutableSet; |
27 | import com.google.common.collect.Iterables; |
28 | import com.google.common.collect.LinkedHashMultimap; |
29 | import com.google.common.collect.Multimap; |
30 | import com.google.common.collect.Sets; |
31 | |
32 | /** |
33 | * |
34 | * @see <a href= |
35 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-IpPermissionType.html" |
36 | * /> |
37 | * @author Adrian Cole |
38 | */ |
39 | public class IpPermissionImpl implements IpPermission { |
40 | public static Builder builder() { |
41 | return new Builder(); |
42 | } |
43 | |
44 | public static class Builder { |
45 | private int fromPort; |
46 | private int toPort; |
47 | private IpProtocol ipProtocol; |
48 | private Multimap<String, String> userIdGroupPairs = LinkedHashMultimap.create(); |
49 | private Set<String> groupIds = Sets.newLinkedHashSet(); |
50 | private Set<String> ipRanges = Sets.newLinkedHashSet(); |
51 | |
52 | public Builder fromPort(int fromPort) { |
53 | this.fromPort = fromPort; |
54 | return this; |
55 | } |
56 | |
57 | public Builder toPort(int toPort) { |
58 | this.fromPort = toPort; |
59 | return this; |
60 | } |
61 | |
62 | public Builder ipProtocol(IpProtocol ipProtocol) { |
63 | this.ipProtocol = ipProtocol; |
64 | return this; |
65 | } |
66 | |
67 | public Builder userIdGroupPair(String userId, String groupNameOrId) { |
68 | this.userIdGroupPairs.put(userId, groupNameOrId); |
69 | return this; |
70 | } |
71 | |
72 | public Builder userIdGroupPairs(Multimap<String, String> userIdGroupPairs) { |
73 | this.userIdGroupPairs.putAll(userIdGroupPairs); |
74 | return this; |
75 | } |
76 | |
77 | public Builder ipRange(String ipRange) { |
78 | this.ipRanges.add(ipRange); |
79 | return this; |
80 | } |
81 | |
82 | public Builder ipRanges(Iterable<String> ipRanges) { |
83 | Iterables.addAll(this.ipRanges, ipRanges); |
84 | return this; |
85 | } |
86 | |
87 | public Builder groupId(String groupId) { |
88 | this.groupIds.add(groupId); |
89 | return this; |
90 | } |
91 | |
92 | public Builder groupIds(Iterable<String> groupIds) { |
93 | Iterables.addAll(this.groupIds, groupIds); |
94 | return this; |
95 | } |
96 | |
97 | public IpPermission build() { |
98 | return new IpPermissionImpl(ipProtocol, fromPort, toPort, userIdGroupPairs, groupIds, ipRanges); |
99 | } |
100 | } |
101 | |
102 | private final int fromPort; |
103 | private final int toPort; |
104 | private final Multimap<String, String> userIdGroupPairs; |
105 | private final Set<String> groupIds; |
106 | private final IpProtocol ipProtocol; |
107 | private final Set<String> ipRanges; |
108 | |
109 | public IpPermissionImpl(IpProtocol ipProtocol, int fromPort, int toPort, |
110 | Multimap<String, String> userIdGroupPairs, Iterable<String> groupIds, Iterable<String> ipRanges) { |
111 | this.fromPort = fromPort; |
112 | this.toPort = toPort; |
113 | this.userIdGroupPairs = ImmutableMultimap.copyOf(checkNotNull(userIdGroupPairs, "userIdGroupPairs")); |
114 | this.ipProtocol = checkNotNull(ipProtocol, "ipProtocol"); |
115 | this.groupIds = ImmutableSet.copyOf(checkNotNull(groupIds, "groupIds")); |
116 | this.ipRanges = ImmutableSet.copyOf(checkNotNull(ipRanges, "ipRanges")); |
117 | } |
118 | |
119 | /** |
120 | * {@inheritDoc} |
121 | */ |
122 | public int compareTo(IpPermission o) { |
123 | return (this == o) ? 0 : getIpProtocol().compareTo(o.getIpProtocol()); |
124 | } |
125 | |
126 | /** |
127 | * {@inheritDoc} |
128 | */ |
129 | @Override |
130 | public int getFromPort() { |
131 | return fromPort; |
132 | } |
133 | |
134 | /** |
135 | * {@inheritDoc} |
136 | */ |
137 | @Override |
138 | public int getToPort() { |
139 | return toPort; |
140 | } |
141 | |
142 | /** |
143 | * {@inheritDoc} |
144 | */ |
145 | @Override |
146 | public Multimap<String, String> getUserIdGroupPairs() { |
147 | return userIdGroupPairs; |
148 | } |
149 | |
150 | /** |
151 | * {@inheritDoc} |
152 | */ |
153 | @Override |
154 | public Set<String> getGroupIds() { |
155 | return groupIds; |
156 | } |
157 | |
158 | /** |
159 | * {@inheritDoc} |
160 | */ |
161 | @Override |
162 | public IpProtocol getIpProtocol() { |
163 | return ipProtocol; |
164 | } |
165 | |
166 | /** |
167 | * {@inheritDoc} |
168 | */ |
169 | @Override |
170 | public Set<String> getIpRanges() { |
171 | return ipRanges; |
172 | } |
173 | |
174 | @Override |
175 | public int hashCode() { |
176 | final int prime = 31; |
177 | int result = 1; |
178 | result = prime * result + fromPort; |
179 | result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode()); |
180 | result = prime * result + ((ipProtocol == null) ? 0 : ipProtocol.hashCode()); |
181 | result = prime * result + ((ipRanges == null) ? 0 : ipRanges.hashCode()); |
182 | result = prime * result + toPort; |
183 | result = prime * result + ((userIdGroupPairs == null) ? 0 : userIdGroupPairs.hashCode()); |
184 | return result; |
185 | } |
186 | |
187 | @Override |
188 | public boolean equals(Object obj) { |
189 | if (this == obj) |
190 | return true; |
191 | if (obj == null) |
192 | return false; |
193 | if (getClass() != obj.getClass()) |
194 | return false; |
195 | IpPermissionImpl other = (IpPermissionImpl) obj; |
196 | if (fromPort != other.fromPort) |
197 | return false; |
198 | if (groupIds == null) { |
199 | if (other.groupIds != null) |
200 | return false; |
201 | } else if (!groupIds.equals(other.groupIds)) |
202 | return false; |
203 | if (ipProtocol != other.ipProtocol) |
204 | return false; |
205 | if (ipRanges == null) { |
206 | if (other.ipRanges != null) |
207 | return false; |
208 | } else if (!ipRanges.equals(other.ipRanges)) |
209 | return false; |
210 | if (toPort != other.toPort) |
211 | return false; |
212 | if (userIdGroupPairs == null) { |
213 | if (other.userIdGroupPairs != null) |
214 | return false; |
215 | } else if (!userIdGroupPairs.equals(other.userIdGroupPairs)) |
216 | return false; |
217 | return true; |
218 | } |
219 | |
220 | @Override |
221 | public String toString() { |
222 | return "[fromPort=" + fromPort + ", toPort=" + toPort + ", userIdGroupPairs=" + userIdGroupPairs + ", groupIds=" |
223 | + groupIds + ", ipProtocol=" + ipProtocol + ", ipRanges=" + ipRanges + "]"; |
224 | } |
225 | |
226 | } |