001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.rest.client;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * A list of 'host:port' addresses of HTTP servers operating as a single
030 * entity, for example multiple redundant web service gateways.
031 */
032@InterfaceAudience.Public
033public class Cluster {
034  protected List<String> nodes = 
035    Collections.synchronizedList(new ArrayList<String>());
036  protected String lastHost;
037
038  /**
039   * Constructor
040   */
041  public Cluster() {}
042
043  /**
044   * Constructor
045   * @param nodes a list of service locations, in 'host:port' format
046   */
047  public Cluster(List<String> nodes) {
048    this.nodes.addAll(nodes);
049  }
050
051  /**
052   * @return true if no locations have been added, false otherwise
053   */
054  public boolean isEmpty() {
055    return nodes.isEmpty();
056  }
057
058  /**
059   * Add a node to the cluster
060   * @param node the service location in 'host:port' format
061   */
062  public Cluster add(String node) {
063    nodes.add(node);
064    return this;
065  }
066
067  /**
068   * Add a node to the cluster
069   * @param name host name
070   * @param port service port
071   */
072  public Cluster add(String name, int port) {
073    StringBuilder sb = new StringBuilder();
074    sb.append(name);
075    sb.append(':');
076    sb.append(port);
077    return add(sb.toString());
078  }
079
080  /**
081   * Remove a node from the cluster
082   * @param node the service location in 'host:port' format
083   */
084  public Cluster remove(String node) {
085    nodes.remove(node);
086    return this;
087  }
088
089  /**
090   * Remove a node from the cluster
091   * @param name host name
092   * @param port service port
093   */
094  public Cluster remove(String name, int port) {
095    StringBuilder sb = new StringBuilder();
096    sb.append(name);
097    sb.append(':');
098    sb.append(port);
099    return remove(sb.toString());
100  }
101
102  @Override public String toString() {
103    return "Cluster{" +
104        "nodes=" + nodes +
105        ", lastHost='" + lastHost + '\'' +
106        '}';
107  }
108}