| 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.Date; |
| 23 | |
| 24 | import com.google.common.base.Objects; |
| 25 | import com.google.common.base.Objects.ToStringHelper; |
| 26 | |
| 27 | /** |
| 28 | * Detailed information on usage |
| 29 | * |
| 30 | * @author Adam Lowe |
| 31 | * @see org.jclouds.glesys.domain.ServerStatus |
| 32 | */ |
| 33 | public class ResourceUsageValue { |
| 34 | |
| 35 | public static Builder<?> builder() { |
| 36 | return new ConcreteBuilder(); |
| 37 | } |
| 38 | |
| 39 | public Builder<?> toBuilder() { |
| 40 | return new ConcreteBuilder().fromResourceUsage(this); |
| 41 | } |
| 42 | |
| 43 | public abstract static class Builder<T extends Builder<T>> { |
| 44 | protected abstract T self(); |
| 45 | |
| 46 | protected double value; |
| 47 | protected Date timestamp; |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * @see ResourceUsageValue#getValue() |
| 52 | */ |
| 53 | public T value(double value) { |
| 54 | this.value = value; |
| 55 | return self(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @see ResourceUsageValue#getTimestamp() |
| 60 | */ |
| 61 | public T timestamp(Date timestamp) { |
| 62 | this.timestamp = checkNotNull(timestamp, "timestamp"); |
| 63 | return self(); |
| 64 | } |
| 65 | |
| 66 | public ResourceUsageValue build() { |
| 67 | return new ResourceUsageValue(value, timestamp); |
| 68 | } |
| 69 | |
| 70 | public T fromResourceUsage(ResourceUsageValue in) { |
| 71 | return this |
| 72 | .value(in.getValue()) |
| 73 | .timestamp(in.getTimestamp()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
| 78 | @Override |
| 79 | protected ConcreteBuilder self() { |
| 80 | return this; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private final double value; |
| 85 | private final Date timestamp; |
| 86 | |
| 87 | @ConstructorProperties({ |
| 88 | "value", "timestamp" |
| 89 | }) |
| 90 | protected ResourceUsageValue(double value, Date timestamp) { |
| 91 | this.value = value; |
| 92 | this.timestamp = checkNotNull(timestamp, "timestamp"); |
| 93 | } |
| 94 | |
| 95 | public double getValue() { |
| 96 | return this.value; |
| 97 | } |
| 98 | |
| 99 | public Date getTimestamp() { |
| 100 | return this.timestamp; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
| 105 | return Objects.hashCode(value, timestamp); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean equals(Object obj) { |
| 110 | if (this == obj) return true; |
| 111 | if (obj == null || getClass() != obj.getClass()) return false; |
| 112 | ResourceUsageValue that = ResourceUsageValue.class.cast(obj); |
| 113 | return Objects.equal(this.value, that.value) |
| 114 | && Objects.equal(this.timestamp, that.timestamp); |
| 115 | } |
| 116 | |
| 117 | protected ToStringHelper string() { |
| 118 | return Objects.toStringHelper("").add("value", value).add("timestamp", timestamp); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public String toString() { |
| 123 | return string().toString(); |
| 124 | } |
| 125 | |
| 126 | } |