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