| 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 static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Strings.emptyToNull; |
| 23 | |
| 24 | /** |
| 25 | * A region is made up of a keyname and a description of that region. |
| 26 | * A region keyname can be used as part of an order. |
| 27 | * Check the SoftLayer_Product_Order service for more details. |
| 28 | * |
| 29 | * @author Jason King |
| 30 | * @see <a href= "http://sldn.softlayer.com/reference/datatypes/SoftLayer_Location_Region" |
| 31 | * /> |
| 32 | */ |
| 33 | public class Region implements Comparable<Region> { |
| 34 | public static Builder builder() { |
| 35 | return new Builder(); |
| 36 | } |
| 37 | |
| 38 | public static class Builder { |
| 39 | private String keyname; |
| 40 | private String description; |
| 41 | |
| 42 | public Builder keyname(String keyname) { |
| 43 | this.keyname = keyname; |
| 44 | return this; |
| 45 | } |
| 46 | |
| 47 | public Builder description(String description) { |
| 48 | this.description = description; |
| 49 | return this; |
| 50 | } |
| 51 | |
| 52 | public Region build() { |
| 53 | return new Region(keyname, description); |
| 54 | } |
| 55 | |
| 56 | public static Builder fromAddress(Region in) { |
| 57 | return Region.builder().keyname(in.getKeyname()) |
| 58 | .description(in.getDescription()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* An integer representing the order in which this element is displayed */ |
| 63 | private int sortOrder = 0; |
| 64 | private String keyname; |
| 65 | private String description; |
| 66 | |
| 67 | // for deserializer |
| 68 | Region() { |
| 69 | |
| 70 | } |
| 71 | |
| 72 | public Region(String keyname, String description) { |
| 73 | this.keyname = checkNotNull(emptyToNull(keyname),"keyname cannot be null or empty:"+keyname); |
| 74 | this.description = checkNotNull(emptyToNull(description),"country cannot be null or empty:"+description); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int compareTo(Region arg0) { |
| 79 | return new Integer(sortOrder).compareTo(arg0.sortOrder); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return A unique key name for a region. Provided for easy debugging. This is to be sent in with an order. |
| 84 | */ |
| 85 | public String getKeyname() { |
| 86 | return keyname; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return A short description of a region's name. This description is seen on the order forms. |
| 91 | */ |
| 92 | public String getDescription() { |
| 93 | return description; |
| 94 | } |
| 95 | |
| 96 | public Builder toBuilder() { |
| 97 | return Builder.fromAddress(this); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean equals(Object o) { |
| 102 | if (this == o) return true; |
| 103 | if (o == null || getClass() != o.getClass()) return false; |
| 104 | |
| 105 | Region region = (Region) o; |
| 106 | |
| 107 | if (sortOrder != region.sortOrder) return false; |
| 108 | if (!description.equals(region.description)) return false; |
| 109 | if (!keyname.equals(region.keyname)) return false; |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
| 116 | int result = sortOrder; |
| 117 | result = 31 * result + keyname.hashCode(); |
| 118 | result = 31 * result + description.hashCode(); |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String toString() { |
| 124 | return "[keyname=" + keyname + ", description=" + description + "]"; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | } |