| 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.cloudloadbalancers.domain.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | import java.util.SortedSet; |
| 25 | |
| 26 | import com.google.common.collect.ImmutableSet; |
| 27 | import com.google.common.collect.ImmutableSortedSet; |
| 28 | import com.google.common.collect.Sets; |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | * @author Adrian Cole |
| 33 | * @see <a href= |
| 34 | * "http://docs.rackspacecloud.com/loadbalancers/api/v1.0/clb-devguide/content/ch04s01s02.html" |
| 35 | * /> |
| 36 | */ |
| 37 | public class BaseLoadBalancer<N extends BaseNode<N>, T extends BaseLoadBalancer<N, T>> implements |
| 38 | Comparable<BaseLoadBalancer<N, T>> { |
| 39 | |
| 40 | public static <N extends BaseNode<N>, T extends BaseLoadBalancer<N, T>> Builder<N, T> builder() { |
| 41 | return new Builder<N, T>(); |
| 42 | } |
| 43 | |
| 44 | @SuppressWarnings("unchecked") |
| 45 | public Builder<N, T> toBuilder() { |
| 46 | return new Builder<N, T>().from((T) this); |
| 47 | } |
| 48 | |
| 49 | public static class Builder<N extends BaseNode<N>, T extends BaseLoadBalancer<N, T>> { |
| 50 | protected String name; |
| 51 | protected String protocol; |
| 52 | protected Integer port; |
| 53 | protected String algorithm; |
| 54 | protected Set<N> nodes = Sets.newLinkedHashSet(); |
| 55 | |
| 56 | public Builder<N, T> name(String name) { |
| 57 | this.name = name; |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | public Builder<N, T> protocol(String protocol) { |
| 62 | this.protocol = protocol; |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | public Builder<N, T> port(Integer port) { |
| 67 | this.port = port; |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | public Builder<N, T> algorithm(String algorithm) { |
| 72 | this.algorithm = algorithm; |
| 73 | return this; |
| 74 | } |
| 75 | |
| 76 | public Builder<N, T> nodes(Iterable<N> nodes) { |
| 77 | this.nodes = ImmutableSet.<N> copyOf(checkNotNull(nodes, "nodes")); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | @SuppressWarnings("unchecked") |
| 82 | public Builder<N, T> node(N node) { |
| 83 | this.nodes.add((N) checkNotNull(nodes, "nodes")); |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | public BaseLoadBalancer<N, T> build() { |
| 88 | return new BaseLoadBalancer<N, T>(name, protocol, port, algorithm, nodes); |
| 89 | } |
| 90 | |
| 91 | public Builder<N, T> from(T baseLoadBalancer) { |
| 92 | return name(baseLoadBalancer.getName()).port(baseLoadBalancer.getPort()).protocol( |
| 93 | baseLoadBalancer.getProtocol()).algorithm(baseLoadBalancer.getAlgorithm()).nodes( |
| 94 | baseLoadBalancer.getNodes()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // for serialization only |
| 99 | protected BaseLoadBalancer() { |
| 100 | |
| 101 | } |
| 102 | |
| 103 | protected String name; |
| 104 | protected String protocol; |
| 105 | protected Integer port; |
| 106 | protected String algorithm; |
| 107 | // so tests will come out consistently |
| 108 | protected SortedSet<N> nodes = ImmutableSortedSet.of(); |
| 109 | |
| 110 | public BaseLoadBalancer(String name, String protocol, Integer port, String algorithm, Iterable<N> nodes) { |
| 111 | this.name = checkNotNull(name, "name"); |
| 112 | this.protocol = protocol;// null on deleted LB |
| 113 | this.port = port;// null on deleted LB |
| 114 | this.algorithm = algorithm;// null on deleted LB |
| 115 | this.nodes = ImmutableSortedSet.copyOf(checkNotNull(nodes, "nodes")); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public int compareTo(BaseLoadBalancer<N, T> arg0) { |
| 120 | return name.compareTo(arg0.name); |
| 121 | } |
| 122 | |
| 123 | public String getName() { |
| 124 | return name; |
| 125 | } |
| 126 | |
| 127 | public String getProtocol() { |
| 128 | return protocol; |
| 129 | } |
| 130 | |
| 131 | public Integer getPort() { |
| 132 | return port; |
| 133 | } |
| 134 | |
| 135 | public String getAlgorithm() { |
| 136 | return algorithm; |
| 137 | } |
| 138 | |
| 139 | public Set<N> getNodes() { |
| 140 | return nodes; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public int hashCode() { |
| 145 | final Integer prime = 31; |
| 146 | Integer result = 1; |
| 147 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean equals(Object obj) { |
| 153 | if (this == obj) |
| 154 | return true; |
| 155 | if (obj == null) |
| 156 | return false; |
| 157 | if (getClass() != obj.getClass()) |
| 158 | return false; |
| 159 | BaseLoadBalancer<?, ?> other = (BaseLoadBalancer<?, ?>) obj; |
| 160 | if (name == null) { |
| 161 | if (other.name != null) |
| 162 | return false; |
| 163 | } else if (!name.equals(other.name)) |
| 164 | return false; |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public String toString() { |
| 170 | return String.format("[name=%s, port=%s, protocol=%s, algorithm=%s, nodes=%s]", name, port, protocol, algorithm, |
| 171 | nodes); |
| 172 | } |
| 173 | } |