| 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 org.jclouds.javax.annotation.Nullable; |
| 24 | |
| 25 | import com.google.common.base.Objects; |
| 26 | import com.google.common.base.Objects.ToStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * DNS record data. |
| 30 | * |
| 31 | * @author Adam Lowe |
| 32 | * @see <a href= "https://customer.glesys.com/api.php?a=doc#domain_list_records" /> |
| 33 | */ |
| 34 | public class DomainRecord { |
| 35 | |
| 36 | public static Builder<?> builder() { |
| 37 | return new ConcreteBuilder(); |
| 38 | } |
| 39 | |
| 40 | public Builder<?> toBuilder() { |
| 41 | return new ConcreteBuilder().fromDomainRecord(this); |
| 42 | } |
| 43 | |
| 44 | public abstract static class Builder<T extends Builder<T>> { |
| 45 | protected abstract T self(); |
| 46 | |
| 47 | protected String id; |
| 48 | protected String domainname; |
| 49 | protected String host; |
| 50 | protected String type; |
| 51 | protected String data; |
| 52 | protected int ttl; |
| 53 | |
| 54 | /** |
| 55 | * @see DomainRecord#getId() |
| 56 | */ |
| 57 | public T id(String id) { |
| 58 | this.id = checkNotNull(id, "id"); |
| 59 | return self(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @see DomainRecord#getname() |
| 64 | */ |
| 65 | public T domainname(String domainname) { |
| 66 | this.domainname = checkNotNull(domainname, "domainname"); |
| 67 | return self(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @see DomainRecord#getHost() |
| 72 | */ |
| 73 | public T host(String host) { |
| 74 | this.host = checkNotNull(host, "host"); |
| 75 | return self(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @see DomainRecord#getType() |
| 80 | */ |
| 81 | public T type(String type) { |
| 82 | this.type = checkNotNull(type, "type"); |
| 83 | return self(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @see DomainRecord#getData() |
| 88 | */ |
| 89 | public T data(String data) { |
| 90 | this.data = checkNotNull(data, "data"); |
| 91 | return self(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @see DomainRecord#getTtl() |
| 96 | */ |
| 97 | public T ttl(int ttl) { |
| 98 | this.ttl = ttl; |
| 99 | return self(); |
| 100 | } |
| 101 | |
| 102 | public DomainRecord build() { |
| 103 | return new DomainRecord(id, domainname, host, type, data, ttl); |
| 104 | } |
| 105 | |
| 106 | public T fromDomainRecord(DomainRecord in) { |
| 107 | return this.id(in.getId()) |
| 108 | .domainname(in.getname()) |
| 109 | .host(in.getHost()) |
| 110 | .type(in.getType()) |
| 111 | .data(in.getData()) |
| 112 | .ttl(in.getTtl()); |
| 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 domainname; |
| 125 | private final String host; |
| 126 | private final String type; |
| 127 | private final String data; |
| 128 | private final int ttl; |
| 129 | |
| 130 | @ConstructorProperties({ |
| 131 | "recordid", "domainname", "host", "type", "data", "ttl" |
| 132 | }) |
| 133 | protected DomainRecord(@Nullable String id, String domainname, String host, String type, @Nullable String data, int ttl) { |
| 134 | this.id = id; |
| 135 | this.domainname = checkNotNull(domainname, "domainname"); |
| 136 | this.host = checkNotNull(host, "host"); |
| 137 | this.type = checkNotNull(type, "type"); |
| 138 | this.data = data; |
| 139 | this.ttl = ttl; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return the id of the record used to modify it via the API |
| 144 | * @see org.jclouds.glesys.features.DomainApi |
| 145 | */ |
| 146 | public String getId() { |
| 147 | return this.id; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return the zone content of the record |
| 152 | */ |
| 153 | public String getname() { |
| 154 | return this.domainname; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return the host content of the record |
| 159 | */ |
| 160 | public String getHost() { |
| 161 | return this.host; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @return the type of the record, ex. "A" |
| 166 | */ |
| 167 | public String getType() { |
| 168 | return this.type; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @return the data content of the record |
| 173 | */ |
| 174 | @Nullable |
| 175 | public String getData() { |
| 176 | return this.data; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @return the TTL/Time-to-live for the record |
| 181 | */ |
| 182 | public int getTtl() { |
| 183 | return this.ttl; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public int hashCode() { |
| 188 | return Objects.hashCode(id); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean equals(Object obj) { |
| 193 | if (this == obj) return true; |
| 194 | if (obj == null || getClass() != obj.getClass()) return false; |
| 195 | DomainRecord that = DomainRecord.class.cast(obj); |
| 196 | return Objects.equal(this.id, that.id); |
| 197 | } |
| 198 | |
| 199 | protected ToStringHelper string() { |
| 200 | return Objects.toStringHelper("") |
| 201 | .add("id", id).add("domainname", domainname).add("host", host).add("type", type).add("data", data) |
| 202 | .add("ttl", ttl); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public String toString() { |
| 207 | return string().toString(); |
| 208 | } |
| 209 | |
| 210 | } |