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/**
030 * Replica identifier comprised of the domain baseDN and its serverId within
031 * this domain.
032 */
033public final class ReplicaId implements Comparable<ReplicaId>
034{
035
036  private final DN baseDN;
037  private final int serverId;
038
039  /**
040   * Creates a ReplicaId with the provided parameters.
041   *
042   * @param baseDN
043   *          domain baseDN, cannot be null
044   * @param serverId
045   *          serverId within the domain
046   */
047  private ReplicaId(DN baseDN, int serverId)
048  {
049    this.baseDN = baseDN;
050    this.serverId = serverId;
051  }
052
053  /**
054   * Creates a ReplicaId with the provided parameters.
055   *
056   * @param baseDN
057   *          domain baseDN
058   * @param serverId
059   *          serverId within the domain
060   * @return a new ReplicaId
061   */
062  public static ReplicaId of(DN baseDN, int serverId)
063  {
064    return new ReplicaId(baseDN, serverId);
065  }
066
067  /** {@inheritDoc} */
068  @Override
069  public int compareTo(ReplicaId o)
070  {
071    final int compareResult = baseDN.compareTo(o.baseDN);
072    if (compareResult == 0)
073    {
074      return serverId - o.serverId;
075    }
076    return compareResult;
077  }
078
079  /** {@inheritDoc} */
080  @Override
081  public int hashCode()
082  {
083    final int prime = 31;
084    int result = 1;
085    result = prime * result + (baseDN == null ? 0 : baseDN.hashCode());
086    return prime * result + serverId;
087  }
088
089  /** {@inheritDoc} */
090  @Override
091  public boolean equals(Object obj)
092  {
093    if (this == obj)
094    {
095      return true;
096    }
097    if (!(obj instanceof ReplicaId))
098    {
099      return false;
100    }
101    final ReplicaId other = (ReplicaId) obj;
102    return serverId == other.serverId && baseDN.equals(other.baseDN);
103  }
104
105  /** {@inheritDoc} */
106  @Override
107  public String toString()
108  {
109    return getClass().getSimpleName() + "(" + baseDN + " " + serverId + ")";
110  }
111}