001: package org.apache.ojb.broker.metadata;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.Serializable;
019: import java.util.*;
020:
021: /**
022: * base class for all Descriptors. It is used to implement the AttributeContainer
023: * interface which provides mechanics for user defined attributes.
024: * @author Thomas Mahler
025: */
026: class DescriptorBase implements AttributeContainer, Serializable {
027: static final long serialVersionUID = 713914612744155925L;
028: /** holds user defined attributes */
029: private Map attributeMap = null;
030:
031: /**
032: * Constructor for DescriptorBase.
033: */
034: public DescriptorBase() {
035: }
036:
037: /**
038: * @see org.apache.ojb.broker.metadata.AttributeContainer#addAttribute(String, String)
039: */
040: public void addAttribute(String attributeName, String attributeValue) {
041: // Don't allow null attribute names.
042: if (attributeName == null) {
043: return;
044: }
045: // Set up the attribute list
046: if (attributeMap == null) {
047: attributeMap = new HashMap();
048: }
049: // Add the entry.
050: attributeMap.put(attributeName, attributeValue);
051: }
052:
053: /**
054: * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(String, String)
055: */
056: public String getAttribute(String attributeName, String defaultValue) {
057: String result = defaultValue;
058: if (attributeMap != null) {
059: result = (String) attributeMap.get(attributeName);
060: if (result == null) {
061: result = defaultValue;
062: }
063: }
064: return result;
065: }
066:
067: /**
068: * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(String)
069: */
070: public String getAttribute(String attributeName) {
071: return this .getAttribute(attributeName, null);
072: }
073:
074: /**
075: * Returns the attribute map (name, value) of this descriptor. Note that the
076: * returned map is not modifiable.
077: *
078: * @return The attributes
079: */
080: public Map getAttributes() {
081: return Collections.unmodifiableMap(attributeMap);
082: }
083:
084: /**
085: * Returns an array of the names of all atributes of this descriptor.
086: *
087: * @return The list of attribute names (will not be <code>null</code>)
088: */
089: public String[] getAttributeNames() {
090: Set keys = (attributeMap == null ? new HashSet() : attributeMap
091: .keySet());
092: String[] result = new String[keys.size()];
093:
094: keys.toArray(result);
095: return result;
096: }
097:
098: public String toString() {
099: StringBuffer buf = new StringBuffer();
100: buf.append("custom attributes [");
101: buf.append(attributeMap);
102: buf.append("]");
103: return buf.toString();
104: }
105: }
|