| 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 | * Information about an archive |
| 28 | * |
| 29 | * @author Adam Lowe |
| 30 | * @see <a href= "https://customer.glesys.com/api.php?a=doc#archive_list" /> |
| 31 | */ |
| 32 | public class Archive { |
| 33 | |
| 34 | public static Builder<?> builder() { |
| 35 | return new ConcreteBuilder(); |
| 36 | } |
| 37 | |
| 38 | public Builder<?> toBuilder() { |
| 39 | return new ConcreteBuilder().fromArchive(this); |
| 40 | } |
| 41 | |
| 42 | public abstract static class Builder<T extends Builder<T>> { |
| 43 | protected abstract T self(); |
| 44 | |
| 45 | protected String username; |
| 46 | protected String totalSize; |
| 47 | protected String freeSize; |
| 48 | protected boolean locked; |
| 49 | |
| 50 | /** |
| 51 | * @see Archive#getUsername() |
| 52 | */ |
| 53 | public T username(String username) { |
| 54 | this.username = checkNotNull(username, "username"); |
| 55 | return self(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @see Archive#getTotalSize() |
| 60 | */ |
| 61 | public T totalSize(String totalSize) { |
| 62 | this.totalSize = checkNotNull(totalSize, "totalSize"); |
| 63 | return self(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @see Archive#getFreeSize() |
| 68 | */ |
| 69 | public T freeSize(String freeSize) { |
| 70 | this.freeSize = checkNotNull(freeSize, "freeSize"); |
| 71 | return self(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @see Archive#isLocked() |
| 76 | */ |
| 77 | public T locked(boolean locked) { |
| 78 | this.locked = locked; |
| 79 | return self(); |
| 80 | } |
| 81 | |
| 82 | public Archive build() { |
| 83 | return new Archive(username, totalSize, freeSize, new GleSYSBoolean(locked)); |
| 84 | } |
| 85 | |
| 86 | public T fromArchive(Archive in) { |
| 87 | return this |
| 88 | .username(in.getUsername()) |
| 89 | .totalSize(in.getTotalSize()) |
| 90 | .freeSize(in.getFreeSize()) |
| 91 | .locked(in.isLocked()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
| 96 | @Override |
| 97 | protected ConcreteBuilder self() { |
| 98 | return this; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private final String username; |
| 103 | private final String totalSize; |
| 104 | private final String freeSize; |
| 105 | private final boolean locked; |
| 106 | |
| 107 | @ConstructorProperties({ |
| 108 | "username", "sizetotal", "sizefree", "locked" |
| 109 | }) |
| 110 | protected Archive(String username, String totalSize, String freeSize, GleSYSBoolean locked) { |
| 111 | this.username = checkNotNull(username, "username"); |
| 112 | this.totalSize = checkNotNull(totalSize, "totalSize"); |
| 113 | this.freeSize = checkNotNull(freeSize, "freeSize"); |
| 114 | this.locked = checkNotNull(locked, "locked").getValue(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @return the name (username) of the archive |
| 119 | */ |
| 120 | public String getUsername() { |
| 121 | return this.username; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @return the total size of the archive, ex. "10 GB" |
| 126 | */ |
| 127 | public String getTotalSize() { |
| 128 | return this.totalSize; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return the free space left of the archive |
| 133 | */ |
| 134 | public String getFreeSize() { |
| 135 | return this.freeSize; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return true if the archive is locked |
| 140 | */ |
| 141 | public boolean isLocked() { |
| 142 | return this.locked; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public int hashCode() { |
| 147 | return Objects.hashCode(username, totalSize, freeSize, locked); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean equals(Object obj) { |
| 152 | if (this == obj) return true; |
| 153 | if (obj == null || getClass() != obj.getClass()) return false; |
| 154 | Archive that = Archive.class.cast(obj); |
| 155 | return Objects.equal(this.username, that.username) |
| 156 | && Objects.equal(this.totalSize, that.totalSize) |
| 157 | && Objects.equal(this.freeSize, that.freeSize) |
| 158 | && Objects.equal(this.locked, that.locked); |
| 159 | } |
| 160 | |
| 161 | protected ToStringHelper string() { |
| 162 | return Objects.toStringHelper("") |
| 163 | .add("username", username).add("totalSize", totalSize).add("freeSize", freeSize).add("locked", locked); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public String toString() { |
| 168 | return string().toString(); |
| 169 | } |
| 170 | |
| 171 | } |