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 *
024 *      Copyright 2014-2015 ForgeRock AS
025 */
026package org.opends.server.backends.pluggable.spi;
027
028/**
029 * Represents the name of a tree (key-value store) in a database.
030 * A tree name is made of the baseDN it is part of, and the identifier of the index it represents.
031 * <p>
032 * Note: This class assumes name components don't contain a '/'.
033 */
034public final class TreeName implements Comparable<TreeName>
035{
036  private final String baseDN;
037  private final String indexId;
038  private final String s;
039
040  /**
041   * Builds a tree name.
042   *
043   * @param baseDN
044   *          the base DN
045   * @param indexId
046   *          the index identifier
047   */
048  public TreeName(String baseDN, String indexId)
049  {
050    this.baseDN = baseDN;
051    this.indexId = indexId;
052    this.s = '/' + baseDN + '/' + indexId;
053  }
054
055  /**
056   * Builds a new {@link TreeName} object based on the provided string representation.
057   *
058   * @param treeName the string representation of the tree name
059   * @return a new {@link TreeName} object constructed from the provided string
060   */
061  public static TreeName valueOf(String treeName)
062  {
063    final String[] split = treeName.split("/");
064    return new TreeName(split[0], split[1]);
065  }
066
067  /**
068   * Returns the base DN.
069   *
070   * @return a {@code String} representing the base DN
071   */
072  public String getBaseDN()
073  {
074    return baseDN;
075  }
076
077  /**
078   * Returns the index identifier.
079   *
080   * @return a {@code String} representing the base DN
081   */
082  public String getIndexId()
083  {
084    return indexId;
085  }
086
087  /**
088   * Returns a new tree name object created by replacing the baseDN of the current object.
089   *
090   * @param newBaseDN
091   *          the new base DN that replaces the existing base DN
092   * @return a new tree name object with the provided the base DN
093   */
094  public TreeName replaceBaseDN(String newBaseDN)
095  {
096    return new TreeName(newBaseDN, indexId);
097  }
098
099  /** {@inheritDoc} */
100  @Override
101  public boolean equals(final Object obj)
102  {
103    if (this == obj)
104    {
105      return true;
106    }
107    else if (obj instanceof TreeName)
108    {
109      return s.equals(((TreeName) obj).s);
110    }
111    else
112    {
113      return false;
114    }
115  }
116
117  /** {@inheritDoc} */
118  @Override
119  public int hashCode()
120  {
121    return s.hashCode();
122  }
123
124  /** {@inheritDoc} */
125  @Override
126  public String toString()
127  {
128    return s;
129  }
130
131  @Override
132  public int compareTo(TreeName o)
133  {
134    return s.compareTo(o.s);
135  }
136}