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 | * Represents an 'uptime' duration of server in a Glesys cloud |
28 | * |
29 | * @author Adam Lowe |
30 | * @see ServerStatus |
31 | */ |
32 | public class ServerUptime { |
33 | |
34 | public static Builder<?> builder() { |
35 | return new ConcreteBuilder(); |
36 | } |
37 | |
38 | public Builder<?> toBuilder() { |
39 | return new ConcreteBuilder().fromServerUptime(this); |
40 | } |
41 | |
42 | public abstract static class Builder<T extends Builder<T>> { |
43 | protected abstract T self(); |
44 | |
45 | protected long current; |
46 | protected String unit; |
47 | |
48 | /** |
49 | * @see ServerUptime#getCurrent() |
50 | */ |
51 | public T current(long current) { |
52 | this.current = current; |
53 | return self(); |
54 | } |
55 | |
56 | /** |
57 | * @see ServerUptime#getUnit() |
58 | */ |
59 | public T unit(String unit) { |
60 | this.unit = checkNotNull(unit, "unit"); |
61 | return self(); |
62 | } |
63 | |
64 | public ServerUptime build() { |
65 | return new ServerUptime(current, unit); |
66 | } |
67 | |
68 | public T fromServerUptime(ServerUptime in) { |
69 | return this.current(in.getCurrent()).unit(in.getUnit()); |
70 | } |
71 | } |
72 | |
73 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
74 | @Override |
75 | protected ConcreteBuilder self() { |
76 | return this; |
77 | } |
78 | } |
79 | |
80 | private final long current; |
81 | private final String unit; |
82 | |
83 | @ConstructorProperties({ |
84 | "current", "unit" |
85 | }) |
86 | protected ServerUptime(long current, String unit) { |
87 | this.current = current; |
88 | this.unit = checkNotNull(unit, "unit"); |
89 | } |
90 | |
91 | /** |
92 | * @return the time the server has been up in #getUnit() |
93 | */ |
94 | public long getCurrent() { |
95 | return this.current; |
96 | } |
97 | |
98 | /** |
99 | * @return the unit used for #getCurrent() |
100 | */ |
101 | public String getUnit() { |
102 | return this.unit; |
103 | } |
104 | |
105 | @Override |
106 | public int hashCode() { |
107 | return Objects.hashCode(current, unit); |
108 | } |
109 | |
110 | @Override |
111 | public boolean equals(Object obj) { |
112 | if (this == obj) return true; |
113 | if (obj == null || getClass() != obj.getClass()) return false; |
114 | ServerUptime that = ServerUptime.class.cast(obj); |
115 | return Objects.equal(this.current, that.current) && Objects.equal(this.unit, that.unit); |
116 | } |
117 | |
118 | protected ToStringHelper string() { |
119 | return Objects.toStringHelper("").add("current", current).add("unit", unit); |
120 | } |
121 | |
122 | @Override |
123 | public String toString() { |
124 | return string().toString(); |
125 | } |
126 | |
127 | } |