| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DescriptorBase | 
 | 
 | 1.8571428571428572;1.857 | 
| 1 |  package org.apache.ojb.broker.metadata; | |
| 2 | ||
| 3 |  /* Copyright 2002-2005 The Apache Software Foundation | |
| 4 |   * | |
| 5 |   * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 6 |   * you may not use this file except in compliance with the License. | |
| 7 |   * You may obtain a copy of the License at | |
| 8 |   * | |
| 9 |   *     http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 |   * | |
| 11 |   * Unless required by applicable law or agreed to in writing, software | |
| 12 |   * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 |   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 |   * See the License for the specific language governing permissions and | |
| 15 |   * limitations under the License. | |
| 16 |   */ | |
| 17 | ||
| 18 |  import java.io.Serializable; | |
| 19 |  import java.util.*; | |
| 20 | ||
| 21 |  /** | |
| 22 |   * base class for all Descriptors. It is used to implement the AttributeContainer | |
| 23 |   * interface which provides mechanics for user defined attributes. | |
| 24 |   * @author Thomas Mahler | |
| 25 |   */ | |
| 26 | class DescriptorBase implements AttributeContainer, Serializable | |
| 27 |  { | |
| 28 | static final long serialVersionUID = 713914612744155925L; | |
| 29 |      /** holds user defined attributes */ | |
| 30 | private Map attributeMap = null; | |
| 31 | ||
| 32 |      /** | |
| 33 |       * Constructor for DescriptorBase. | |
| 34 |       */ | |
| 35 |      public DescriptorBase() | |
| 36 |      { | |
| 37 | } | |
| 38 | ||
| 39 |      /** | |
| 40 |       * @see org.apache.ojb.broker.metadata.AttributeContainer#addAttribute(String, String) | |
| 41 |       */ | |
| 42 | public void addAttribute(String attributeName, String attributeValue) | |
| 43 |      { | |
| 44 |          // Don't allow null attribute names. | |
| 45 | if (attributeName == null) | |
| 46 |          { | |
| 47 |              return; | |
| 48 | } | |
| 49 |          // Set up the attribute list | |
| 50 | if (attributeMap == null) | |
| 51 |          { | |
| 52 |              attributeMap = new HashMap(); | |
| 53 | } | |
| 54 |          // Add the entry. | |
| 55 | attributeMap.put(attributeName, attributeValue); | |
| 56 | } | |
| 57 | ||
| 58 |      /** | |
| 59 |       * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(String, String) | |
| 60 |       */ | |
| 61 |      public String getAttribute(String attributeName, String defaultValue) | |
| 62 |      { | |
| 63 | String result = defaultValue; | |
| 64 | if (attributeMap != null) | |
| 65 |          { | |
| 66 | result = (String) attributeMap.get(attributeName); | |
| 67 | if (result == null) | |
| 68 |              { | |
| 69 | result = defaultValue; | |
| 70 | } | |
| 71 | } | |
| 72 |          return result; | |
| 73 | } | |
| 74 | ||
| 75 |      /** | |
| 76 |       * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(String) | |
| 77 |       */ | |
| 78 |      public String getAttribute(String attributeName) | |
| 79 |      { | |
| 80 | return this.getAttribute(attributeName, null); | |
| 81 | } | |
| 82 | ||
| 83 |      /** | |
| 84 |       * Returns the attribute map (name, value) of this descriptor. Note that the | |
| 85 |       * returned map is not modifiable. | |
| 86 |       *  | |
| 87 |       * @return The attributes | |
| 88 |       */ | |
| 89 |      public Map getAttributes() | |
| 90 |      { | |
| 91 |          return Collections.unmodifiableMap(attributeMap); | |
| 92 | } | |
| 93 | ||
| 94 |      /** | |
| 95 |       * Returns an array of the names of all atributes of this descriptor. | |
| 96 |       *  | |
| 97 |       * @return The list of attribute names (will not be <code>null</code>) | |
| 98 |       */ | |
| 99 |      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 | } |