| 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 | * Operating system template |
| 28 | * |
| 29 | * @author Adam Lowe |
| 30 | * @see <a href= "https://customer.glesys.com/api.php?a=doc#server_templates" /> |
| 31 | */ |
| 32 | public class OSTemplate { |
| 33 | |
| 34 | public static Builder<?> builder() { |
| 35 | return new ConcreteBuilder(); |
| 36 | } |
| 37 | |
| 38 | public Builder<?> toBuilder() { |
| 39 | return new ConcreteBuilder().fromOSTemplate(this); |
| 40 | } |
| 41 | |
| 42 | public abstract static class Builder<T extends Builder<T>> { |
| 43 | protected abstract T self(); |
| 44 | |
| 45 | protected String name; |
| 46 | protected int minDiskSize; |
| 47 | protected int minMemSize; |
| 48 | protected String os; |
| 49 | protected String platform; |
| 50 | |
| 51 | /** |
| 52 | * @see OSTemplate#getName() |
| 53 | */ |
| 54 | public T name(String name) { |
| 55 | this.name = checkNotNull(name, "name"); |
| 56 | return self(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @see OSTemplate#getMinDiskSize() |
| 61 | */ |
| 62 | public T minDiskSize(int minDiskSize) { |
| 63 | this.minDiskSize = minDiskSize; |
| 64 | return self(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @see OSTemplate#getMinMemSize() |
| 69 | */ |
| 70 | public T minMemSize(int minMemSize) { |
| 71 | this.minMemSize = minMemSize; |
| 72 | return self(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @see OSTemplate#getOs() |
| 77 | */ |
| 78 | public T os(String os) { |
| 79 | this.os = checkNotNull(os, "os"); |
| 80 | return self(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @see OSTemplate#getPlatform() |
| 85 | */ |
| 86 | public T platform(String platform) { |
| 87 | this.platform = checkNotNull(platform, "platform"); |
| 88 | return self(); |
| 89 | } |
| 90 | |
| 91 | public OSTemplate build() { |
| 92 | return new OSTemplate(name, minDiskSize, minMemSize, os, platform); |
| 93 | } |
| 94 | |
| 95 | public T fromOSTemplate(OSTemplate in) { |
| 96 | return this.name(in.getName()) |
| 97 | .minDiskSize(in.getMinDiskSize()) |
| 98 | .minMemSize(in.getMinMemSize()) |
| 99 | .os(in.getOs()) |
| 100 | .platform(in.getPlatform()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
| 105 | @Override |
| 106 | protected ConcreteBuilder self() { |
| 107 | return this; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private final String name; |
| 112 | private final int minDiskSize; |
| 113 | private final int minMemSize; |
| 114 | private final String os; |
| 115 | private final String platform; |
| 116 | |
| 117 | @ConstructorProperties({ |
| 118 | "name", "minimumdisksize", "minimummemorysize", "operatingsystem", "platform" |
| 119 | }) |
| 120 | protected OSTemplate(String name, int minDiskSize, int minMemSize, String os, String platform) { |
| 121 | this.name = checkNotNull(name, "name"); |
| 122 | this.minDiskSize = minDiskSize; |
| 123 | this.minMemSize = minMemSize; |
| 124 | this.os = checkNotNull(os, "os"); |
| 125 | this.platform = checkNotNull(platform, "platform"); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | */ |
| 130 | public String getName() { |
| 131 | return this.name; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return the minimum allowed disk size in GB |
| 136 | * @see org.jclouds.glesys.domain.AllowedArgumentsForCreateServer#getDiskSizesInGB() |
| 137 | */ |
| 138 | public int getMinDiskSize() { |
| 139 | return this.minDiskSize; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return the minimum allowed memory size in MB |
| 144 | * @see org.jclouds.glesys.domain.AllowedArgumentsForCreateServer#getMemorySizesInMB() |
| 145 | */ |
| 146 | public int getMinMemSize() { |
| 147 | return this.minMemSize; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return the name of the operating system type ex. "linux" |
| 152 | */ |
| 153 | public String getOs() { |
| 154 | return this.os; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return the name of the platform this template is available in, ex. "Xen" |
| 159 | */ |
| 160 | public String getPlatform() { |
| 161 | return this.platform; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public int hashCode() { |
| 166 | return Objects.hashCode(name, platform); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean equals(Object obj) { |
| 171 | if (this == obj) return true; |
| 172 | if (obj == null || getClass() != obj.getClass()) return false; |
| 173 | OSTemplate that = OSTemplate.class.cast(obj); |
| 174 | return Objects.equal(this.name, that.name) |
| 175 | && Objects.equal(this.platform, that.platform); |
| 176 | } |
| 177 | |
| 178 | protected ToStringHelper string() { |
| 179 | return Objects.toStringHelper("") |
| 180 | .add("name", name).add("minDiskSize", minDiskSize).add("minMemSize", minMemSize).add("os", os).add("platform", platform); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public String toString() { |
| 185 | return string().toString(); |
| 186 | } |
| 187 | |
| 188 | } |