| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.softlayer.domain; |
| 20 | |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Sets; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @author Adrian Cole |
| 31 | * @see <a href= "http://sldn.softlayer.com/reference/datatypes/SoftLayer_Location_Datacenter" |
| 32 | * /> |
| 33 | */ |
| 34 | public class Datacenter implements Comparable<Datacenter> { |
| 35 | public static Builder builder() { |
| 36 | return new Builder(); |
| 37 | } |
| 38 | |
| 39 | public static class Builder { |
| 40 | private int id = -1; |
| 41 | private String name; |
| 42 | private String longName; |
| 43 | private Address locationAddress; |
| 44 | private Set<Region> regions = Sets.newLinkedHashSet(); |
| 45 | |
| 46 | public Builder id(int id) { |
| 47 | this.id = id; |
| 48 | return this; |
| 49 | } |
| 50 | |
| 51 | public Builder name(String name) { |
| 52 | this.name = name; |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | public Builder longName(String longName) { |
| 57 | this.longName = longName; |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | public Builder locationAddress(Address locationAddress) { |
| 62 | this.locationAddress = locationAddress; |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | public Builder region(Region regions) { |
| 67 | this.regions.add(checkNotNull(regions, "regions")); |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | public Builder regions(Iterable<Region> regions) { |
| 72 | this.regions = ImmutableSet.<Region> copyOf(checkNotNull(regions, "regions")); |
| 73 | return this; |
| 74 | } |
| 75 | |
| 76 | public Datacenter build() { |
| 77 | return new Datacenter(id, name, longName, locationAddress, regions); |
| 78 | } |
| 79 | |
| 80 | public static Builder fromDatacenter(Datacenter in) { |
| 81 | return Datacenter.builder().id(in.getId()).name(in.getName()) |
| 82 | .longName(in.getLongName()).locationAddress(in.getLocationAddress()) |
| 83 | .regions(in.getRegions()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private int id = -1; |
| 88 | private String name; |
| 89 | private String longName; |
| 90 | private Address locationAddress; |
| 91 | private Set<Region> regions = Sets.newLinkedHashSet(); |
| 92 | // for deserializer |
| 93 | Datacenter() { |
| 94 | |
| 95 | } |
| 96 | |
| 97 | public Datacenter(int id, String name, String longName, Address locationAddress, Iterable<Region> regions) { |
| 98 | this.id = id; |
| 99 | this.name = name; |
| 100 | this.longName = longName; |
| 101 | this.locationAddress = locationAddress; |
| 102 | this.regions = ImmutableSet.<Region> copyOf(checkNotNull(regions, "regions")); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int compareTo(Datacenter arg0) { |
| 107 | return new Integer(id).compareTo(arg0.getId()); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @return The unique identifier of a specific location. |
| 112 | */ |
| 113 | public int getId() { |
| 114 | return id; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @return A short location description. |
| 119 | */ |
| 120 | public String getName() { |
| 121 | return name; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @return A longer location description. |
| 126 | */ |
| 127 | public String getLongName() { |
| 128 | return longName; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return A location's physical address (optional). |
| 133 | */ |
| 134 | public Address getLocationAddress() { |
| 135 | return locationAddress; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * A location can be a member of 1 or more regions. |
| 140 | * Sometimes the list of regions is empty, for example as a new Datacenter is being added. |
| 141 | * The list of regions usually contains one with keyName=FIRST_AVAILABLE which should be ignored. |
| 142 | * @return The regions to which a location belongs. |
| 143 | */ |
| 144 | public Set<Region> getRegions() { |
| 145 | return regions; |
| 146 | } |
| 147 | |
| 148 | public Builder toBuilder() { |
| 149 | return Builder.fromDatacenter(this); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int hashCode() { |
| 154 | final int prime = 31; |
| 155 | int result = 1; |
| 156 | result = prime * result + (id ^ (id >>> 32)); |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean equals(Object obj) { |
| 162 | if (this == obj) |
| 163 | return true; |
| 164 | if (obj == null) |
| 165 | return false; |
| 166 | if (getClass() != obj.getClass()) |
| 167 | return false; |
| 168 | Datacenter other = (Datacenter) obj; |
| 169 | if (id != other.id) |
| 170 | return false; |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public String toString() { |
| 176 | return "[id=" + id + ", country=" + name + ", state=" + longName + "], locationAddress=" + locationAddress + ", regions="+regions+"]"; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | } |