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