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-2016 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    /** Whether this ID represents an internal data provider. */
057    private final boolean isInternal;
058    /** The data provider name. */
059    private final String name;
060    /** The normalized name. */
061    private final String normalizedName;
062
063    /** Prevent direct instantiation. */
064    private DataProviderID(final String name, final boolean isInternal) {
065        this.name = name;
066        this.normalizedName = name.trim().toLowerCase(Locale.ENGLISH);
067        this.isInternal = isInternal;
068    }
069
070    @Override
071    public int compareTo(final DataProviderID o) {
072        if (isInternal != o.isInternal) {
073            // Internal data providers sort last.
074            return isInternal ? 1 : -1;
075        } else {
076            return normalizedName.compareTo(o.normalizedName);
077        }
078    }
079
080    @Override
081    public boolean equals(final Object obj) {
082        if (this == obj) {
083            return true;
084        } else if (obj instanceof DataProviderID) {
085            final DataProviderID other = (DataProviderID) obj;
086            return isInternal == other.isInternal
087                && normalizedName.equals(other.normalizedName);
088        } else {
089            return false;
090        }
091    }
092
093    /**
094     * Returns the data provider name associated with this data provider ID.
095     *
096     * @return The data provider name associated with this data provider ID.
097     */
098    public String getName() {
099        return name;
100    }
101
102    @Override
103    public int hashCode() {
104        return normalizedName.hashCode();
105    }
106
107    /**
108     * Indicating whether this ID represents an internal data provider.
109     *
110     * @return <code>true</code> if this ID represents an internal data
111     *         provider.
112     */
113    public boolean isInternal() {
114        return isInternal;
115    }
116
117    @Override
118    public String toString() {
119        if (isInternal) {
120            return "__" + name;
121        } else {
122            return name;
123        }
124    }
125
126}