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.favored;
019
020import org.apache.hbase.thirdparty.com.google.common.net.HostAndPort;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.hadoop.hbase.ServerName;
023import org.apache.hadoop.hbase.util.Addressing;
024
025/**
026 * This class differs from ServerName in that start code is always ignored. This is because
027 * start code, ServerName.NON_STARTCODE is used to persist favored nodes and keeping this separate
028 * from {@link ServerName} is much cleaner. This should only be used by Favored node specific
029 * classes and should not be used outside favored nodes.
030 */
031@InterfaceAudience.Private
032class StartcodeAgnosticServerName extends ServerName {
033
034  public StartcodeAgnosticServerName(final String hostname, final int port, long startcode) {
035    super(hostname, port, startcode);
036  }
037
038  public static StartcodeAgnosticServerName valueOf(final ServerName serverName) {
039    return new StartcodeAgnosticServerName(serverName.getHostname(), serverName.getPort(),
040        serverName.getStartcode());
041  }
042
043  public static StartcodeAgnosticServerName valueOf(final String hostnameAndPort, long startcode) {
044    return new StartcodeAgnosticServerName(Addressing.parseHostname(hostnameAndPort),
045        Addressing.parsePort(hostnameAndPort), startcode);
046  }
047
048  public static StartcodeAgnosticServerName valueOf(final HostAndPort hostnameAndPort, long startcode) {
049    return new StartcodeAgnosticServerName(hostnameAndPort.getHost(),
050      hostnameAndPort.getPort(), startcode);
051  }
052
053  @Override
054  public int compareTo(ServerName other) {
055    int compare = this.getHostname().compareTo(other.getHostname());
056    if (compare != 0) return compare;
057    compare = this.getPort() - other.getPort();
058    if (compare != 0) return compare;
059    return 0;
060  }
061
062  @Override
063  public int hashCode() {
064    return getHostAndPort().hashCode();
065  }
066}