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