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