| 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 java.beans.ConstructorProperties; |
| 20 | |
| 21 | import com.google.common.base.Objects; |
| 22 | import com.google.common.base.Objects.ToStringHelper; |
| 23 | |
| 24 | /** |
| 25 | * Detailed information about an OpenVZ server's limits |
| 26 | * |
| 27 | * @author Adam Lowe |
| 28 | * @see <a href= "https://customer.glesys.com/api.php?a=doc#server_limits" /> |
| 29 | */ |
| 30 | public class ServerLimit { |
| 31 | |
| 32 | public static Builder<?> builder() { |
| 33 | return new ConcreteBuilder(); |
| 34 | } |
| 35 | |
| 36 | public Builder<?> toBuilder() { |
| 37 | return new ConcreteBuilder().fromServerLimit(this); |
| 38 | } |
| 39 | |
| 40 | public abstract static class Builder<T extends Builder<T>> { |
| 41 | protected abstract T self(); |
| 42 | |
| 43 | protected long held; |
| 44 | protected long maxHeld; |
| 45 | protected long barrier; |
| 46 | protected long limit; |
| 47 | protected long failCount; |
| 48 | |
| 49 | /** |
| 50 | * @see ServerLimit#getHeld() |
| 51 | */ |
| 52 | public T held(long held) { |
| 53 | this.held = held; |
| 54 | return self(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @see ServerLimit#getMaxHeld() |
| 59 | */ |
| 60 | public T maxHeld(long maxHeld) { |
| 61 | this.maxHeld = maxHeld; |
| 62 | return self(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @see ServerLimit#getBarrier() |
| 67 | */ |
| 68 | public T barrier(long barrier) { |
| 69 | this.barrier = barrier; |
| 70 | return self(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @see ServerLimit#getLimit() |
| 75 | */ |
| 76 | public T limit(long limit) { |
| 77 | this.limit = limit; |
| 78 | return self(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @see ServerLimit#getFailCount() |
| 83 | */ |
| 84 | public T failCount(long failCount) { |
| 85 | this.failCount = failCount; |
| 86 | return self(); |
| 87 | } |
| 88 | |
| 89 | public ServerLimit build() { |
| 90 | return new ServerLimit(held, maxHeld, barrier, limit, failCount); |
| 91 | } |
| 92 | |
| 93 | public T fromServerLimit(ServerLimit in) { |
| 94 | return this.held(in.getHeld()) |
| 95 | .maxHeld(in.getMaxHeld()) |
| 96 | .barrier(in.getBarrier()) |
| 97 | .limit(in.getLimit()) |
| 98 | .failCount(in.getFailCount()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
| 103 | @Override |
| 104 | protected ConcreteBuilder self() { |
| 105 | return this; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private final long held; |
| 110 | private final long maxHeld; |
| 111 | private final long barrier; |
| 112 | private final long limit; |
| 113 | private final long failCount; |
| 114 | |
| 115 | @ConstructorProperties({ |
| 116 | "held", "maxHeld", "barrier", "limit", "failCount" |
| 117 | }) |
| 118 | protected ServerLimit(long held, long maxHeld, long barrier, long limit, long failCount) { |
| 119 | this.held = held; |
| 120 | this.maxHeld = maxHeld; |
| 121 | this.barrier = barrier; |
| 122 | this.limit = limit; |
| 123 | this.failCount = failCount; |
| 124 | } |
| 125 | |
| 126 | public long getHeld() { |
| 127 | return this.held; |
| 128 | } |
| 129 | |
| 130 | public long getMaxHeld() { |
| 131 | return this.maxHeld; |
| 132 | } |
| 133 | |
| 134 | public long getBarrier() { |
| 135 | return this.barrier; |
| 136 | } |
| 137 | |
| 138 | public long getLimit() { |
| 139 | return this.limit; |
| 140 | } |
| 141 | |
| 142 | public long getFailCount() { |
| 143 | return this.failCount; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public int hashCode() { |
| 148 | return Objects.hashCode(held, maxHeld, barrier, limit, failCount); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean equals(Object obj) { |
| 153 | if (this == obj) return true; |
| 154 | if (obj == null || getClass() != obj.getClass()) return false; |
| 155 | ServerLimit that = ServerLimit.class.cast(obj); |
| 156 | return Objects.equal(this.held, that.held) |
| 157 | && Objects.equal(this.maxHeld, that.maxHeld) |
| 158 | && Objects.equal(this.barrier, that.barrier) |
| 159 | && Objects.equal(this.limit, that.limit) |
| 160 | && Objects.equal(this.failCount, that.failCount); |
| 161 | } |
| 162 | |
| 163 | protected ToStringHelper string() { |
| 164 | return Objects.toStringHelper("").add("held", held).add("maxHeld", maxHeld).add("barrier", barrier) |
| 165 | .add("limit", limit).add("failCount", failCount); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public String toString() { |
| 170 | return string().toString(); |
| 171 | } |
| 172 | |
| 173 | } |