| 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 | import java.util.Set; |
| 23 | |
| 24 | import com.google.common.base.Objects; |
| 25 | import com.google.common.base.Objects.ToStringHelper; |
| 26 | import com.google.common.collect.ImmutableSet; |
| 27 | |
| 28 | /** |
| 29 | * Represents the allowed arguments for a certain server resource type (such as |
| 30 | * disksize, memorysize, cpucores, and transfer). |
| 31 | * <p/> |
| 32 | * This is a composite type consisting of both the set of allowed units for the |
| 33 | * resource type as well as the cost per unit. |
| 34 | * |
| 35 | * @see AllowedArgumentsForCreateServer |
| 36 | * @author Peter Gardfjäll |
| 37 | */ |
| 38 | public class AllowedArguments { |
| 39 | public static Builder builder() { |
| 40 | return new Builder(); |
| 41 | } |
| 42 | |
| 43 | public Builder toBuilder() { |
| 44 | return new Builder().fromAllowedArgument(this); |
| 45 | } |
| 46 | |
| 47 | public static class Builder { |
| 48 | |
| 49 | protected Cost costPerUnit; |
| 50 | protected Set<Integer> allowedUnits; |
| 51 | |
| 52 | /** |
| 53 | * @see AllowedArguments#getCostPerUnit() |
| 54 | */ |
| 55 | public Builder costPerUnit(Cost costPerUnit) { |
| 56 | this.costPerUnit = costPerUnit; |
| 57 | return this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @see AllowedArguments#getAllowedUnits() |
| 62 | */ |
| 63 | public Builder allowedUnits(Set<Integer> allowedUnits) { |
| 64 | this.allowedUnits = ImmutableSet.copyOf(checkNotNull(allowedUnits, |
| 65 | "allowedUnits")); |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | public Builder allowedUnits(Integer... allowedUnits) { |
| 70 | return allowedUnits(ImmutableSet.copyOf(allowedUnits)); |
| 71 | } |
| 72 | |
| 73 | public AllowedArguments build() { |
| 74 | return new AllowedArguments(this.costPerUnit, this.allowedUnits); |
| 75 | } |
| 76 | |
| 77 | public Builder fromAllowedArgument(AllowedArguments in) { |
| 78 | return this.costPerUnit(in.getCostPerUnit()).allowedUnits( |
| 79 | in.getAllowedUnits()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private final Cost costPerUnit; |
| 84 | private final Set<Integer> allowedUnits; |
| 85 | |
| 86 | @ConstructorProperties({ "costperunit", "units" }) |
| 87 | protected AllowedArguments(Cost costPerUnit, Set<Integer> units) { |
| 88 | this.costPerUnit = checkNotNull(costPerUnit, "costPerUnit"); |
| 89 | this.allowedUnits = ImmutableSet.copyOf(checkNotNull(units, |
| 90 | "allowedUnits")); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @return the cost per unit. |
| 95 | */ |
| 96 | public Cost getCostPerUnit() { |
| 97 | return this.costPerUnit; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return the set of allowed units for the resource type. |
| 102 | */ |
| 103 | public Set<Integer> getAllowedUnits() { |
| 104 | return this.allowedUnits; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
| 109 | return Objects.hashCode(this.costPerUnit, this.allowedUnits); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean equals(Object obj) { |
| 114 | if (this == obj) { |
| 115 | return true; |
| 116 | } |
| 117 | if ((obj == null) || (getClass() != obj.getClass())) { |
| 118 | return false; |
| 119 | } |
| 120 | AllowedArguments that = AllowedArguments.class.cast(obj); |
| 121 | return Objects.equal(this.costPerUnit, that.costPerUnit) |
| 122 | && Objects.equal(this.allowedUnits, that.allowedUnits); |
| 123 | } |
| 124 | |
| 125 | protected ToStringHelper string() { |
| 126 | return Objects.toStringHelper("").add("costPerUnit", this.costPerUnit) |
| 127 | .add("allowedUnits", this.allowedUnits); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return string().toString(); |
| 133 | } |
| 134 | } |