001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014-2016 ForgeRock AS.
015 */
016package org.opends.server.replication.server.changelog.api;
017
018import org.forgerock.opendj.ldap.DN;
019
020/** Replica identifier comprised of the domain baseDN and its serverId within this domain. */
021public final class ReplicaId implements Comparable<ReplicaId>
022{
023  private final DN baseDN;
024  private final int serverId;
025
026  /**
027   * Creates a ReplicaId with the provided parameters.
028   *
029   * @param baseDN
030   *          domain baseDN, cannot be null
031   * @param serverId
032   *          serverId within the domain
033   */
034  private ReplicaId(DN baseDN, int serverId)
035  {
036    this.baseDN = baseDN;
037    this.serverId = serverId;
038  }
039
040  /**
041   * Creates a ReplicaId with the provided parameters.
042   *
043   * @param baseDN
044   *          domain baseDN
045   * @param serverId
046   *          serverId within the domain
047   * @return a new ReplicaId
048   */
049  public static ReplicaId of(DN baseDN, int serverId)
050  {
051    return new ReplicaId(baseDN, serverId);
052  }
053
054  /**
055   * Returns the baseDN.
056   * @return the baseDN
057   */
058  public DN getBaseDN()
059  {
060    return baseDN;
061  }
062
063  /**
064   * Returns the serverId.
065   * @return the serverId
066   */
067  public int getServerId()
068  {
069    return serverId;
070  }
071
072  @Override
073  public int compareTo(ReplicaId o)
074  {
075    final int compareResult = baseDN.compareTo(o.baseDN);
076    if (compareResult == 0)
077    {
078      return serverId - o.serverId;
079    }
080    return compareResult;
081  }
082
083  @Override
084  public int hashCode()
085  {
086    final int prime = 31;
087    int result = 1;
088    result = prime * result + (baseDN == null ? 0 : baseDN.hashCode());
089    return prime * result + serverId;
090  }
091
092  @Override
093  public boolean equals(Object obj)
094  {
095    if (this == obj)
096    {
097      return true;
098    }
099    if (!(obj instanceof ReplicaId))
100    {
101      return false;
102    }
103    final ReplicaId other = (ReplicaId) obj;
104    return serverId == other.serverId && baseDN.equals(other.baseDN);
105  }
106
107  @Override
108  public String toString()
109  {
110    return getClass().getSimpleName() + "(" + baseDN + " " + serverId + ")";
111  }
112}