| 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 | * Represents an ip address used by a server. | 
| 28 | * | 
| 29 | * @author Adam Lowe | 
| 30 | * @see Server | 
| 31 | * @see ServerDetails | 
| 32 | */ | 
| 33 | public class Ip { | 
| 34 |  | 
| 35 | public static Builder<?> builder() { | 
| 36 | return new ConcreteBuilder(); | 
| 37 | } | 
| 38 |  | 
| 39 | public Builder<?> toBuilder() { | 
| 40 | return new ConcreteBuilder().fromIp(this); | 
| 41 | } | 
| 42 |  | 
| 43 | public abstract static class Builder<T extends Builder<T>> { | 
| 44 | protected abstract T self(); | 
| 45 |  | 
| 46 | protected String ip; | 
| 47 | protected int version; | 
| 48 | protected double cost; | 
| 49 | protected String currency; | 
| 50 |  | 
| 51 | /** | 
| 52 | * @see Ip#getIp() | 
| 53 | */ | 
| 54 | public T ip(String ip) { | 
| 55 | this.ip = checkNotNull(ip, "ip"); | 
| 56 | return self(); | 
| 57 | } | 
| 58 |  | 
| 59 | /** | 
| 60 | * @see Ip#getVersion() | 
| 61 | */ | 
| 62 | protected T version(int version) { | 
| 63 | this.version = version; | 
| 64 | return self(); | 
| 65 | } | 
| 66 |  | 
| 67 | /** | 
| 68 | * @see Ip#getVersion() | 
| 69 | */ | 
| 70 | public T version4() { | 
| 71 | return version(4); | 
| 72 | } | 
| 73 |  | 
| 74 | /** | 
| 75 | * @see Ip#getVersion() | 
| 76 | */ | 
| 77 | public T version6() { | 
| 78 | return version(6); | 
| 79 | } | 
| 80 |  | 
| 81 | /** | 
| 82 | * @see Ip#getCost() | 
| 83 | */ | 
| 84 | public T cost(double cost) { | 
| 85 | this.cost = cost; | 
| 86 | return self(); | 
| 87 | } | 
| 88 |  | 
| 89 | /** | 
| 90 | * @see Ip#getCurrency() | 
| 91 | */ | 
| 92 | public T currency(String currency) { | 
| 93 | this.currency = currency; | 
| 94 | return self(); | 
| 95 | } | 
| 96 |  | 
| 97 | public Ip build() { | 
| 98 | return new Ip(ip, version, cost, currency); | 
| 99 | } | 
| 100 |  | 
| 101 | public T fromIp(Ip in) { | 
| 102 | return this.ip(in.getIp()).version(in.getVersion()).cost(in.getCost()); | 
| 103 | } | 
| 104 | } | 
| 105 |  | 
| 106 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { | 
| 107 | @Override | 
| 108 | protected ConcreteBuilder self() { | 
| 109 | return this; | 
| 110 | } | 
| 111 | } | 
| 112 |  | 
| 113 | private final String ip; | 
| 114 | private final int version; | 
| 115 | private final double cost; | 
| 116 | private final String currency; | 
| 117 |  | 
| 118 | @ConstructorProperties({ | 
| 119 | "ipaddress", "version", "cost", "currency" | 
| 120 | }) | 
| 121 | protected Ip(String ip, int version, double cost, String currency) { | 
| 122 | this.ip = checkNotNull(ip, "ip"); | 
| 123 | this.version = version; | 
| 124 | this.cost = cost; | 
| 125 | this.currency = checkNotNull(currency, "currency"); | 
| 126 | } | 
| 127 |  | 
| 128 | /** | 
| 129 | * @return the IP version, ex. 4 | 
| 130 | */ | 
| 131 | public String getIp() { | 
| 132 | return this.ip; | 
| 133 | } | 
| 134 |  | 
| 135 | /** | 
| 136 | * @return the ip address of the new server | 
| 137 | */ | 
| 138 | public int getVersion() { | 
| 139 | return this.version; | 
| 140 | } | 
| 141 |  | 
| 142 | /** | 
| 143 | * @return the cost of the ip address allocated to the new server | 
| 144 | * @see #getCurrency() | 
| 145 | */ | 
| 146 | public double getCost() { | 
| 147 | return this.cost; | 
| 148 | } | 
| 149 |  | 
| 150 | /** | 
| 151 | * @return the currency of the cost | 
| 152 | * @see #getCost() | 
| 153 | */ | 
| 154 | public String getCurrency() { | 
| 155 | return currency; | 
| 156 | } | 
| 157 |  | 
| 158 | @Override | 
| 159 | public int hashCode() { | 
| 160 | return Objects.hashCode(ip, version, cost); | 
| 161 | } | 
| 162 |  | 
| 163 | @Override | 
| 164 | public boolean equals(Object obj) { | 
| 165 | if (this == obj) return true; | 
| 166 | if (obj == null || getClass() != obj.getClass()) return false; | 
| 167 | Ip that = Ip.class.cast(obj); | 
| 168 | return Objects.equal(this.ip, that.ip) | 
| 169 | && Objects.equal(this.version, that.version) | 
| 170 | && Objects.equal(this.cost, that.cost) | 
| 171 | && Objects.equal(this.currency, that.currency); | 
| 172 | } | 
| 173 |  | 
| 174 | protected ToStringHelper string() { | 
| 175 | return Objects.toStringHelper("") | 
| 176 | .add("ip", ip).add("version", version).add("cost", cost).add("currency", currency); | 
| 177 | } | 
| 178 |  | 
| 179 | @Override | 
| 180 | public String toString() { | 
| 181 | return string().toString(); | 
| 182 | } | 
| 183 |  | 
| 184 | } |