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