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 | |
23 | import com.google.common.base.Objects; |
24 | import com.google.common.base.Objects.ToStringHelper; |
25 | |
26 | /** |
27 | * Connection information to connect to a server with VNC. |
28 | * |
29 | * @author Adam Lowe |
30 | * @see <a href="https://customer.glesys.com/api.php?a=doc#server_console" /> |
31 | */ |
32 | public class Console { |
33 | |
34 | public static Builder<?> builder() { |
35 | return new ConcreteBuilder(); |
36 | } |
37 | |
38 | public Builder<?> toBuilder() { |
39 | return new ConcreteBuilder().fromConsole(this); |
40 | } |
41 | |
42 | public abstract static class Builder<T extends Builder<T>> { |
43 | protected abstract T self(); |
44 | |
45 | protected String host; |
46 | protected int port; |
47 | protected String protocol; |
48 | protected String password; |
49 | |
50 | /** |
51 | * @see Console#getHost() |
52 | */ |
53 | public T host(String host) { |
54 | this.host = checkNotNull(host, "host"); |
55 | return self(); |
56 | } |
57 | |
58 | /** |
59 | * @see Console#getPort() |
60 | */ |
61 | public T port(int port) { |
62 | this.port = port; |
63 | return self(); |
64 | } |
65 | |
66 | /** |
67 | * @see Console#getProtocol() |
68 | */ |
69 | public T protocol(String protocol) { |
70 | this.protocol = checkNotNull(protocol, "protocol"); |
71 | return self(); |
72 | } |
73 | |
74 | /** |
75 | * @see Console#getPassword() |
76 | */ |
77 | public T password(String password) { |
78 | this.password = checkNotNull(password, "password"); |
79 | return self(); |
80 | } |
81 | |
82 | public Console build() { |
83 | return new Console(host, port, protocol, password); |
84 | } |
85 | |
86 | public T fromConsole(Console in) { |
87 | return this.host(in.getHost()).port(in.getPort()).protocol(in.getProtocol()).password(in.getPassword()); |
88 | } |
89 | } |
90 | |
91 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
92 | @Override |
93 | protected ConcreteBuilder self() { |
94 | return this; |
95 | } |
96 | } |
97 | |
98 | private final String host; |
99 | private final int port; |
100 | private final String protocol; |
101 | private final String password; |
102 | |
103 | @ConstructorProperties({ |
104 | "host", "port", "protocol", "password" |
105 | }) |
106 | protected Console(String host, int port, String protocol, String password) { |
107 | this.host = checkNotNull(host, "host"); |
108 | this.port = port; |
109 | this.protocol = checkNotNull(protocol, "protocol"); |
110 | this.password = checkNotNull(password, "password"); |
111 | } |
112 | |
113 | /** |
114 | * @return the host name to use to connect to the server |
115 | */ |
116 | public String getHost() { |
117 | return this.host; |
118 | } |
119 | |
120 | /** |
121 | * @return the port to use to connect to the server |
122 | */ |
123 | public int getPort() { |
124 | return this.port; |
125 | } |
126 | |
127 | /** |
128 | * @return the protocol to use to connect to the server |
129 | */ |
130 | public String getProtocol() { |
131 | return this.protocol; |
132 | } |
133 | |
134 | /** |
135 | * @return the password to use to connect to the server |
136 | */ |
137 | public String getPassword() { |
138 | return this.password; |
139 | } |
140 | |
141 | @Override |
142 | public int hashCode() { |
143 | return Objects.hashCode(host, port, protocol); |
144 | } |
145 | |
146 | @Override |
147 | public boolean equals(Object obj) { |
148 | if (this == obj) return true; |
149 | if (obj == null || getClass() != obj.getClass()) return false; |
150 | Console that = Console.class.cast(obj); |
151 | return Objects.equal(this.host, that.host) |
152 | && Objects.equal(this.port, that.port) |
153 | && Objects.equal(this.protocol, that.protocol); |
154 | } |
155 | |
156 | protected ToStringHelper string() { |
157 | return Objects.toStringHelper("").add("host", host).add("port", port).add("protocol", protocol) |
158 | .add("password", password); |
159 | } |
160 | |
161 | @Override |
162 | public String toString() { |
163 | return string().toString(); |
164 | } |
165 | |
166 | } |