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 { 028 static final long serialVersionUID = 713914612744155925L; 029 /** holds user defined attributes */ 030 private Map attributeMap = null; 031 032 /** 033 * Constructor for DescriptorBase. 034 */ 035 public DescriptorBase() 036 { 037 } 038 039 /** 040 * @see org.apache.ojb.broker.metadata.AttributeContainer#addAttribute(String, String) 041 */ 042 public void addAttribute(String attributeName, String attributeValue) 043 { 044 // Don't allow null attribute names. 045 if (attributeName == null) 046 { 047 return; 048 } 049 // Set up the attribute list 050 if (attributeMap == null) 051 { 052 attributeMap = new HashMap(); 053 } 054 // Add the entry. 055 attributeMap.put(attributeName, attributeValue); 056 } 057 058 /** 059 * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(String, String) 060 */ 061 public String getAttribute(String attributeName, String defaultValue) 062 { 063 String result = defaultValue; 064 if (attributeMap != null) 065 { 066 result = (String) attributeMap.get(attributeName); 067 if (result == null) 068 { 069 result = defaultValue; 070 } 071 } 072 return result; 073 } 074 075 /** 076 * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(String) 077 */ 078 public String getAttribute(String attributeName) 079 { 080 return this.getAttribute(attributeName, null); 081 } 082 083 /** 084 * Returns the attribute map (name, value) of this descriptor. Note that the 085 * returned map is not modifiable. 086 * 087 * @return The attributes 088 */ 089 public Map getAttributes() 090 { 091 return Collections.unmodifiableMap(attributeMap); 092 } 093 094 /** 095 * Returns an array of the names of all atributes of this descriptor. 096 * 097 * @return The list of attribute names (will not be <code>null</code>) 098 */ 099 public String[] getAttributeNames() 100 { 101 Set keys = (attributeMap == null ? new HashSet() : attributeMap.keySet()); 102 String[] result = new String[keys.size()]; 103 104 keys.toArray(result); 105 return result; 106 } 107 108 public String toString() 109 { 110 StringBuffer buf = new StringBuffer(); 111 buf.append("custom attributes ["); 112 buf.append(attributeMap); 113 buf.append("]"); 114 return buf.toString(); 115 } 116 }