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