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 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    /** {@inheritDoc} */
100    public final int compareTo(Tag o) {
101        return name.compareTo(o.name);
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public final boolean equals(Object obj) {
107        if (this == obj) {
108            return true;
109        }
110
111        if (obj instanceof Tag) {
112            Tag other = (Tag) obj;
113            return other.name.equals(this.name);
114        }
115
116        return false;
117    }
118
119    /**
120     * Gets the name of this tag.
121     *
122     * @return Returns the name of this tag.
123     */
124    public final String getName() {
125        return name;
126    }
127
128    /**
129     * Gets the synopsis of this tag in the default locale.
130     *
131     * @return Returns the synopsis of this tag in the default locale.
132     */
133    public final LocalizableMessage getSynopsis() {
134        return getSynopsis(Locale.getDefault());
135    }
136
137    /**
138     * Gets the synopsis of this tag in the specified locale.
139     *
140     * @param locale
141     *            The locale.
142     * @return Returns the synopsis of this tag in the specified locale.
143     */
144    public final LocalizableMessage getSynopsis(Locale locale) {
145        ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
146        String property = "tag." + name + ".synopsis";
147        try {
148            return resource.getMessage(RootCfgDefn.getInstance(), property, locale);
149        } catch (MissingResourceException e) {
150            return null;
151        }
152    }
153
154    /** {@inheritDoc} */
155    @Override
156    public final int hashCode() {
157        return name.hashCode();
158    }
159
160    /** {@inheritDoc} */
161    @Override
162    public final String toString() {
163        return name;
164    }
165
166}