1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | * contributor license agreements. See the NOTICE file distributed with |
4 | * this work for additional information regarding copyright ownership. |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | * (the "License"); you may not use this file except in compliance with |
7 | * the License. You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | package org.jclouds.glesys.domain; |
18 | |
19 | import static com.google.common.base.Preconditions.checkNotNull; |
20 | |
21 | import java.beans.ConstructorProperties; |
22 | import java.util.Set; |
23 | |
24 | import com.google.common.annotations.Beta; |
25 | import com.google.common.base.Objects; |
26 | import com.google.common.base.Objects.ToStringHelper; |
27 | import com.google.common.collect.ImmutableSet; |
28 | |
29 | /** |
30 | * Structure containing all information about e-mail addresses for a GleSYS account |
31 | * |
32 | * @author Adam Lowe |
33 | * @see <a href="https://customer.glesys.com/api.php?a=doc#email_overview" /> |
34 | */ |
35 | //TODO: find a better name for this class |
36 | @Beta |
37 | public class EmailOverview { |
38 | |
39 | public static Builder<?> builder() { |
40 | return new ConcreteBuilder(); |
41 | } |
42 | |
43 | public Builder<?> toBuilder() { |
44 | return new ConcreteBuilder().fromEmailOverview(this); |
45 | } |
46 | |
47 | public abstract static class Builder<T extends Builder<T>> { |
48 | protected abstract T self(); |
49 | |
50 | protected EmailOverviewSummary summary; |
51 | protected Set<EmailOverviewDomain> domains = ImmutableSet.of(); |
52 | |
53 | /** |
54 | * @see EmailOverview#getSummary() |
55 | */ |
56 | public T summary(EmailOverviewSummary summary) { |
57 | this.summary = checkNotNull(summary, "summary"); |
58 | return self(); |
59 | } |
60 | |
61 | /** |
62 | * @see EmailOverview#gets() |
63 | */ |
64 | public T domains(Set<EmailOverviewDomain> domains) { |
65 | this.domains = ImmutableSet.copyOf(checkNotNull(domains, "domains")); |
66 | return self(); |
67 | } |
68 | |
69 | public T domains(EmailOverviewDomain... in) { |
70 | return domains(ImmutableSet.copyOf(in)); |
71 | } |
72 | |
73 | public EmailOverview build() { |
74 | return new EmailOverview(summary, domains); |
75 | } |
76 | |
77 | public T fromEmailOverview(EmailOverview in) { |
78 | return this.summary(in.getSummary()).domains(in.gets()); |
79 | } |
80 | } |
81 | |
82 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
83 | @Override |
84 | protected ConcreteBuilder self() { |
85 | return this; |
86 | } |
87 | } |
88 | |
89 | private final EmailOverviewSummary summary; |
90 | private final Set<EmailOverviewDomain> domains; |
91 | |
92 | @ConstructorProperties({ |
93 | "summary", "domains" |
94 | }) |
95 | protected EmailOverview(EmailOverviewSummary summary, Set<EmailOverviewDomain> domains) { |
96 | this.summary = checkNotNull(summary, "summary"); |
97 | this.domains = ImmutableSet.copyOf(checkNotNull(domains, "domains")); |
98 | } |
99 | |
100 | /** |
101 | * @return summary information about the account |
102 | */ |
103 | public EmailOverviewSummary getSummary() { |
104 | return this.summary; |
105 | } |
106 | |
107 | /** |
108 | * @return the set of detailed information about the e-mail addresses and aliases for each domain |
109 | */ |
110 | public Set<EmailOverviewDomain> gets() { |
111 | return this.domains; |
112 | } |
113 | |
114 | @Override |
115 | public int hashCode() { |
116 | return Objects.hashCode(summary, domains); |
117 | } |
118 | |
119 | @Override |
120 | public boolean equals(Object obj) { |
121 | if (this == obj) return true; |
122 | if (obj == null || getClass() != obj.getClass()) return false; |
123 | EmailOverview that = EmailOverview.class.cast(obj); |
124 | return Objects.equal(this.summary, that.summary) |
125 | && Objects.equal(this.domains, that.domains); |
126 | } |
127 | |
128 | protected ToStringHelper string() { |
129 | return Objects.toStringHelper("") |
130 | .add("summary", summary).add("domains", domains); |
131 | } |
132 | |
133 | @Override |
134 | public String toString() { |
135 | return string().toString(); |
136 | } |
137 | |
138 | } |