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 2015-2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.config;
018
019import org.forgerock.util.Reject;
020
021import java.util.Collection;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Locale;
025import java.util.Map;
026import java.util.MissingResourceException;
027
028import org.forgerock.i18n.LocalizableMessage;
029import org.forgerock.opendj.server.config.meta.RootCfgDefn;
030
031/**
032 * An interface for querying the properties of a tag.
033 * <p>
034 * Tags are used to group related managed objects together into categories.
035 */
036public final class Tag implements Comparable<Tag> {
037
038    /** All the tags. */
039    private static final Map<String, Tag> TAGS = new HashMap<>();
040
041    /**
042     * Defines a new tag with the specified name.
043     *
044     * @param name
045     *            The name of the new tag.
046     */
047    public static void define(String name) {
048        Tag tag = new Tag(name);
049
050        // Register the tag.
051        TAGS.put(name, tag);
052    }
053
054    /**
055     * Returns the tag associated with the specified name.
056     *
057     * @param name
058     *            The name of the tag.
059     * @return Returns the tag associated with the specified name.
060     * @throws IllegalArgumentException
061     *             If the tag name was not recognized.
062     */
063    public static Tag valueOf(String name) {
064        Reject.ifNull(name);
065
066        // Hack to force initialization of the tag definitions.
067        RootCfgDefn.getInstance();
068
069        Tag tag = TAGS.get(name.toLowerCase());
070
071        if (tag == null) {
072            throw new IllegalArgumentException("Unknown tag \"" + name + "\"");
073        }
074
075        return tag;
076    }
077
078    /**
079     * Returns an unmodifiable collection view of the set of registered tags.
080     *
081     * @return Returns an unmodifiable collection view of the set of registered
082     *         tags.
083     */
084    public static Collection<Tag> values() {
085        // Hack to force initialization of the tag definitions.
086        RootCfgDefn.getInstance();
087
088        return Collections.unmodifiableCollection(TAGS.values());
089    }
090
091    /** The name of the tag. */
092    private final String name;
093
094    /** Private constructor. */
095    private Tag(String name) {
096        this.name = name;
097    }
098
099    @Override
100    public final int compareTo(Tag o) {
101        return name.compareTo(o.name);
102    }
103
104    @Override
105    public final boolean equals(Object obj) {
106        if (this == obj) {
107            return true;
108        }
109
110        if (obj instanceof Tag) {
111            Tag other = (Tag) obj;
112            return other.name.equals(this.name);
113        }
114
115        return false;
116    }
117
118    /**
119     * Gets the name of this tag.
120     *
121     * @return Returns the name of this tag.
122     */
123    public final String getName() {
124        return name;
125    }
126
127    /**
128     * Gets the synopsis of this tag in the default locale.
129     *
130     * @return Returns the synopsis of this tag in the default locale.
131     */
132    public final LocalizableMessage getSynopsis() {
133        return getSynopsis(Locale.getDefault());
134    }
135
136    /**
137     * Gets the synopsis of this tag in the specified locale.
138     *
139     * @param locale
140     *            The locale.
141     * @return Returns the synopsis of this tag in the specified locale.
142     */
143    public final LocalizableMessage getSynopsis(Locale locale) {
144        ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
145        String property = "tag." + name + ".synopsis";
146        try {
147            return resource.getMessage(RootCfgDefn.getInstance(), property, locale);
148        } catch (MissingResourceException e) {
149            return null;
150        }
151    }
152
153    @Override
154    public final int hashCode() {
155        return name.hashCode();
156    }
157
158    @Override
159    public final String toString() {
160        return name;
161    }
162
163}