1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.aws.ec2.model.security;
17
18 import static com.google.common.collect.Lists.newArrayList;
19 import static org.kuali.common.util.ListUtils.equalElements;
20
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
24
25 import org.hibernate.validator.constraints.NotEmpty;
26 import org.kuali.common.core.build.ValidatingBuilder;
27 import org.kuali.common.core.validate.annotation.IdiotProofImmutable;
28 import org.kuali.common.core.validate.annotation.ValidPort;
29 import org.kuali.common.util.ObjectUtils;
30
31 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
32 import com.google.common.base.Objects;
33 import com.google.common.collect.ComparisonChain;
34 import com.google.common.collect.ImmutableList;
35
36
37
38
39 @IdiotProofImmutable
40 @JsonDeserialize(builder = Permission.Builder.class)
41 public final class Permission {
42
43 @ValidPort
44 private final int port;
45
46 private final Protocol protocol;
47
48 @NotEmpty
49 private final ImmutableList<String> cidrNotations;
50
51 public static Permission create(int port) {
52 return builder(port).build();
53 }
54
55 public static Builder builder(int port) {
56 return new Builder(port);
57 }
58
59 public static class Builder extends ValidatingBuilder<Permission> {
60
61
62 private final int port;
63
64
65 private List<String> cidrNotations = newArrayList(CIDR.ANY.getNotation());
66 private Protocol protocol = Protocol.TCP;
67
68 public Builder(int port) {
69 this.port = port;
70 }
71
72 public Builder withCidrNotations(List<String> cidrNotations) {
73 this.cidrNotations = cidrNotations;
74 return this;
75 }
76
77 public Builder withProtocol(Protocol protocol) {
78 this.protocol = protocol;
79 return this;
80 }
81
82 @Override
83 public Permission build() {
84
85 this.cidrNotations = newArrayList(cidrNotations);
86 Collections.sort(cidrNotations);
87 return validate(new Permission(this));
88 }
89 }
90
91 private Permission(Builder builder) {
92 this.protocol = builder.protocol;
93 this.port = builder.port;
94 this.cidrNotations = ImmutableList.copyOf(builder.cidrNotations);
95 }
96
97 public Protocol getProtocol() {
98 return protocol;
99 }
100
101 public int getPort() {
102 return port;
103 }
104
105 public List<String> getCidrNotations() {
106 return cidrNotations;
107 }
108
109 @Override
110 public int hashCode() {
111 final int prime = 31;
112 int result = Objects.hashCode(port, protocol);
113 for (String notation : cidrNotations) {
114 result = result * prime + notation.hashCode();
115 }
116 return result;
117 }
118
119 @Override
120 public boolean equals(Object object) {
121
122
123 if (this == object) {
124 return true;
125 }
126
127
128 if (ObjectUtils.notEqual(this, object)) {
129 return false;
130 }
131
132
133 Permission a = this;
134 Permission b = (Permission) object;
135
136
137
138 return Objects.equal(a.port, b.port) && Objects.equal(a.protocol, b.protocol) && equalElements(a.cidrNotations, b.cidrNotations);
139
140 }
141
142
143 public enum DefaultComparator implements Comparator<Permission> {
144 INSTANCE;
145
146 @Override
147 public int compare(Permission one, Permission two) {
148 return ComparisonChain.start().compare(one.getPort(), two.getPort()).compare(one.getProtocol(), two.getProtocol()).result();
149 }
150 }
151
152 }