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 2008 Sun Microsystems, Inc.
015 * Portions copyright 2013-2015 ForgeRock AS.
016 */
017package org.forgerock.opendj.server.core;
018
019import java.util.Locale;
020
021/**
022 * A unique ID which can be used for identifying data providers.
023 * <p>
024 * There are two types of data provider:
025 * <ul>
026 * <li><b>User configured</b>: these are data providers which have been defined
027 * using the server's configuration.
028 * <li><b>Internal</b>: these are data providers which have been created
029 * internally.
030 * </ul>
031 */
032public final class DataProviderID implements Comparable<DataProviderID> {
033
034    /**
035     * Creates a new ID for an internal data provider.
036     *
037     * @param name
038     *            The name of the internal data provider.
039     * @return The new data provider ID.
040     */
041    public static DataProviderID newInternalID(final String name) {
042        return new DataProviderID(name, true /* internal */);
043    }
044
045    /**
046     * Creates a new ID for a user configured data provider.
047     *
048     * @param name
049     *            The name of the user configured data provider.
050     * @return The new data provider ID.
051     */
052    public static DataProviderID newUserID(final String name) {
053        return new DataProviderID(name, false /* user */);
054    }
055
056    /**
057     * Flag indicating whether or not this ID represents an internal
058     * data provider.
059     */
060    private final boolean isInternal;
061
062    /** The data provider name. */
063    private final String name;
064
065    /** The normalized name. */
066    private final String normalizedName;
067
068    /** Prevent direct instantiation. */
069    private DataProviderID(final String name, final boolean isInternal) {
070        this.name = name;
071        this.normalizedName = name.trim().toLowerCase(Locale.ENGLISH);
072        this.isInternal = isInternal;
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    public int compareTo(final DataProviderID o) {
078        if (isInternal != o.isInternal) {
079            // Internal data providers sort last.
080            return isInternal ? 1 : -1;
081        } else {
082            return normalizedName.compareTo(o.normalizedName);
083        }
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public boolean equals(final Object obj) {
089        if (this == obj) {
090            return true;
091        } else if (obj instanceof DataProviderID) {
092            final DataProviderID other = (DataProviderID) obj;
093            return isInternal == other.isInternal
094                && normalizedName.equals(other.normalizedName);
095        } else {
096            return false;
097        }
098    }
099
100    /**
101     * Returns the data provider name associated with this data provider ID.
102     *
103     * @return The data provider name associated with this data provider ID.
104     */
105    public String getName() {
106        return name;
107    }
108
109    /** {@inheritDoc} */
110    @Override
111    public int hashCode() {
112        return normalizedName.hashCode();
113    }
114
115    /**
116     * Indicating whether or not this ID represents an internal data provider.
117     *
118     * @return <code>true</code> if this ID represents an internal data
119     *         provider.
120     */
121    public boolean isInternal() {
122        return isInternal;
123    }
124
125    /** {@inheritDoc} */
126    @Override
127    public String toString() {
128        if (isInternal) {
129            return "__" + name;
130        } else {
131            return name;
132        }
133    }
134
135}