| 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 | * Detailed information on usage |
| 30 | * |
| 31 | * @author Adam Lowe |
| 32 | * @see ResourceUsageInfo |
| 33 | * @see ResourceUsageValue |
| 34 | */ |
| 35 | public class ResourceUsage { |
| 36 | |
| 37 | public static Builder<?> builder() { |
| 38 | return new ConcreteBuilder(); |
| 39 | } |
| 40 | |
| 41 | public Builder<?> toBuilder() { |
| 42 | return new ConcreteBuilder().fromResourceUsages(this); |
| 43 | } |
| 44 | |
| 45 | public abstract static class Builder<T extends Builder<T>> { |
| 46 | protected abstract T self(); |
| 47 | |
| 48 | protected ResourceUsageInfo info; |
| 49 | protected Set<ResourceUsageValue> values = ImmutableSet.of(); |
| 50 | |
| 51 | /** |
| 52 | * @see ResourceUsage#getInfo() |
| 53 | */ |
| 54 | public T info(ResourceUsageInfo info) { |
| 55 | this.info = checkNotNull(info, "info"); |
| 56 | return self(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @see ResourceUsage#getValues() |
| 61 | */ |
| 62 | public T values(Set<ResourceUsageValue> values) { |
| 63 | this.values = ImmutableSet.copyOf(checkNotNull(values, "values")); |
| 64 | return self(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @see ResourceUsage#getValues() |
| 69 | */ |
| 70 | public T values(ResourceUsageValue... in) { |
| 71 | return values(ImmutableSet.copyOf(in)); |
| 72 | } |
| 73 | |
| 74 | public ResourceUsage build() { |
| 75 | return new ResourceUsage(info, values); |
| 76 | } |
| 77 | |
| 78 | public T fromResourceUsages(ResourceUsage in) { |
| 79 | return this |
| 80 | .info(in.getInfo()) |
| 81 | .values(in.getValues()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
| 86 | @Override |
| 87 | protected ConcreteBuilder self() { |
| 88 | return this; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private final ResourceUsageInfo info; |
| 93 | private final Set<ResourceUsageValue> values; |
| 94 | |
| 95 | @ConstructorProperties({ |
| 96 | "info", "values" |
| 97 | }) |
| 98 | protected ResourceUsage(ResourceUsageInfo info, Set<ResourceUsageValue> values) { |
| 99 | this.info = checkNotNull(info, "info"); |
| 100 | this.values = ImmutableSet.copyOf(checkNotNull(values, "values")); |
| 101 | } |
| 102 | |
| 103 | public ResourceUsageInfo getInfo() { |
| 104 | return this.info; |
| 105 | } |
| 106 | |
| 107 | public Set<ResourceUsageValue> getValues() { |
| 108 | return this.values; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int hashCode() { |
| 113 | return Objects.hashCode(info, values); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean equals(Object obj) { |
| 118 | if (this == obj) return true; |
| 119 | if (obj == null || getClass() != obj.getClass()) return false; |
| 120 | ResourceUsage that = ResourceUsage.class.cast(obj); |
| 121 | return Objects.equal(this.info, that.info) |
| 122 | && Objects.equal(this.values, that.values); |
| 123 | } |
| 124 | |
| 125 | protected ToStringHelper string() { |
| 126 | return Objects.toStringHelper("") |
| 127 | .add("info", info).add("values", values); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return string().toString(); |
| 133 | } |
| 134 | |
| 135 | } |