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