| 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.CaseFormat; |
| 24 | import com.google.common.base.Objects; |
| 25 | import com.google.common.base.Objects.ToStringHelper; |
| 26 | |
| 27 | /** |
| 28 | * Listing of a server. |
| 29 | * |
| 30 | * @author Adrian Cole |
| 31 | * @see <a href= "https://customer.glesys.com/api.php?a=doc#server_list" /> |
| 32 | */ |
| 33 | public class Server { |
| 34 | |
| 35 | /** |
| 36 | */ |
| 37 | public static enum State { |
| 38 | |
| 39 | RUNNING, LOCKED, STOPPED, UNRECOGNIZED; |
| 40 | |
| 41 | public String value() { |
| 42 | return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name()); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public String toString() { |
| 47 | return value(); |
| 48 | } |
| 49 | |
| 50 | public static State fromValue(String state) { |
| 51 | try { |
| 52 | return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(state, "state"))); |
| 53 | } catch (IllegalArgumentException e) { |
| 54 | return UNRECOGNIZED; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public static Builder<?> builder() { |
| 60 | return new ConcreteBuilder(); |
| 61 | } |
| 62 | |
| 63 | public Builder<?> toBuilder() { |
| 64 | return new ConcreteBuilder().fromServer(this); |
| 65 | } |
| 66 | |
| 67 | public abstract static class Builder<T extends Builder<T>> { |
| 68 | protected abstract T self(); |
| 69 | |
| 70 | protected String id; |
| 71 | protected String hostname; |
| 72 | protected String datacenter; |
| 73 | protected String platform; |
| 74 | |
| 75 | /** |
| 76 | * @see Server#getId() |
| 77 | */ |
| 78 | public T id(String id) { |
| 79 | this.id = checkNotNull(id, "id"); |
| 80 | return self(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @see Server#getHostname() |
| 85 | */ |
| 86 | public T hostname(String hostname) { |
| 87 | this.hostname = checkNotNull(hostname, "hostname"); |
| 88 | return self(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @see Server#getDatacenter() |
| 93 | */ |
| 94 | public T datacenter(String datacenter) { |
| 95 | this.datacenter = checkNotNull(datacenter, "datacenter"); |
| 96 | return self(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @see Server#getPlatform() |
| 101 | */ |
| 102 | public T platform(String platform) { |
| 103 | this.platform = checkNotNull(platform, "platform"); |
| 104 | return self(); |
| 105 | } |
| 106 | |
| 107 | public Server build() { |
| 108 | return new Server(id, hostname, datacenter, platform); |
| 109 | } |
| 110 | |
| 111 | public T fromServer(Server in) { |
| 112 | return this.id(in.getId()).hostname(in.getHostname()).datacenter(in.getDatacenter()).platform(in.getPlatform()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
| 117 | @Override |
| 118 | protected ConcreteBuilder self() { |
| 119 | return this; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private final String id; |
| 124 | private final String hostname; |
| 125 | private final String datacenter; |
| 126 | private final String platform; |
| 127 | |
| 128 | @ConstructorProperties({ |
| 129 | "serverid", "hostname", "datacenter", "platform" |
| 130 | }) |
| 131 | protected Server(String id, String hostname, String datacenter, String platform) { |
| 132 | this.id = checkNotNull(id, "id"); |
| 133 | this.hostname = checkNotNull(hostname, "hostname"); |
| 134 | this.datacenter = checkNotNull(datacenter, "datacenter"); |
| 135 | this.platform = checkNotNull(platform, "platform"); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return the generated id of the server |
| 140 | */ |
| 141 | public String getId() { |
| 142 | return this.id; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return the hostname of the server |
| 147 | */ |
| 148 | public String getHostname() { |
| 149 | return this.hostname; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @return platform running the server (ex. {@code OpenVZ}) |
| 154 | */ |
| 155 | public String getDatacenter() { |
| 156 | return this.datacenter; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return the datacenter the server exists in (ex. {@code Falkenberg}) |
| 161 | */ |
| 162 | public String getPlatform() { |
| 163 | return this.platform; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public int hashCode() { |
| 168 | return Objects.hashCode(id); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public boolean equals(Object obj) { |
| 173 | if (this == obj) return true; |
| 174 | if (obj == null || getClass() != obj.getClass()) return false; |
| 175 | Server that = Server.class.cast(obj); |
| 176 | return Objects.equal(this.id, that.id); |
| 177 | } |
| 178 | |
| 179 | protected ToStringHelper string() { |
| 180 | return Objects.toStringHelper("").add("id", id).add("hostname", hostname).add("datacenter", datacenter) |
| 181 | .add("platform", platform); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public String toString() { |
| 186 | return string().toString(); |
| 187 | } |
| 188 | |
| 189 | } |