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