001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.metadata;
023:
024: import java.io.Serializable;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.Locale;
028:
029: import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
030:
031: /**
032: * An abstract class for meta data that has descriptions
033: *
034: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
035: * @version $Revision: 57189 $
036: */
037: public class DescriptionMetaDataContainer implements Serializable {
038: private static final long serialVersionUID = 2831943526217092377L;
039:
040: /** The descriptions */
041: private ConcurrentReaderHashMap descriptions = new ConcurrentReaderHashMap();
042:
043: public DescriptionMetaDataContainer() {
044: DescriptionMetaData dmd = new DescriptionMetaData();
045: descriptions.put(dmd.getLanguage(), dmd);
046: }
047:
048: /**
049: * Get the desription for the default language
050: * or the first description if there is no default
051: *
052: * @return the description for the default language
053: */
054: public DescriptionMetaData getDescription() {
055: // Try the default locale
056: DescriptionMetaData dmd = (DescriptionMetaData) descriptions
057: .get(Locale.getDefault().getLanguage());
058: // No description using the default locale, just use the first
059: if (dmd == null) {
060: for (Iterator i = descriptions.values().iterator(); i
061: .hasNext();) {
062: dmd = (DescriptionMetaData) i.next();
063: break;
064: }
065: }
066: return dmd;
067: }
068:
069: /**
070: * Get the description for the give language
071: *
072: * @param lang the language
073: * @return the description
074: */
075: public DescriptionMetaData getDescription(String lang) {
076: return (DescriptionMetaData) descriptions.get(lang);
077: }
078:
079: /**
080: * Add a description
081: *
082: * @param dmd the description
083: */
084: public void addDescription(DescriptionMetaData dmd) {
085: descriptions.put(dmd.getLanguage(), dmd);
086: }
087:
088: /**
089: * Get the descriptions
090: *
091: * @return the descriptions
092: */
093: public Collection getDescriptions() {
094: return descriptions.values();
095: }
096:
097: public String toString() {
098: StringBuffer buffer = new StringBuffer();
099: buffer.append("DescriptionMetaDataContainer").append('@');
100: buffer.append(Integer
101: .toHexString(System.identityHashCode(this )));
102: buffer.append("[descriptions=").append(descriptions.values());
103: buffer.append(']');
104: return buffer.toString();
105: }
106: }
|