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