1 19 package org.apache.cayenne.map; 20 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.SortedMap ; 26 import java.util.TreeMap ; 27 28 import org.apache.cayenne.util.XMLEncoder; 29 import org.apache.cayenne.util.XMLSerializable; 30 31 40 public class Embeddable implements XMLSerializable, Serializable { 41 42 protected String className; 43 protected SortedMap attributes; 44 45 public Embeddable() { 46 this(null); 47 } 48 49 public Embeddable(String className) { 50 this.attributes = new TreeMap (); 51 this.className = className; 52 } 53 54 58 public EmbeddableAttribute getAttributeForDbPath(String dbPath) { 59 Iterator it = attributes.values().iterator(); 60 while (it.hasNext()) { 61 EmbeddableAttribute attribute = (EmbeddableAttribute) it.next(); 62 if (dbPath.equals(attribute.getDbAttributeName())) { 63 return attribute; 64 } 65 } 66 67 return null; 68 } 69 70 73 public SortedMap getAttributeMap() { 74 return Collections.unmodifiableSortedMap(attributes); 77 } 78 79 82 public Collection getAttributes() { 83 return Collections.unmodifiableCollection(attributes.values()); 86 } 87 88 92 public void addAttribute(EmbeddableAttribute attribute) { 93 if (attribute.getName() == null) { 94 throw new IllegalArgumentException ("Attempt to insert unnamed attribute."); 95 } 96 97 Object existingAttribute = attributes.get(attribute.getName()); 98 if (existingAttribute != null) { 99 if (existingAttribute == attribute) { 100 return; 101 } 102 else { 103 throw new IllegalArgumentException ( 104 "An attempt to override embeddable attribute '" 105 + attribute.getName() 106 + "'"); 107 } 108 } 109 110 attributes.put(attribute.getName(), attribute); 111 attribute.setEmbeddable(this); 112 } 113 114 public EmbeddableAttribute getAttribute(String name) { 115 return (EmbeddableAttribute) attributes.get(name); 116 } 117 118 public void removeAttribute(String name) { 119 attributes.remove(name); 120 } 121 122 public String getClassName() { 123 return className; 124 } 125 126 public void setClassName(String className) { 127 this.className = className; 128 } 129 130 133 public void encodeAsXML(XMLEncoder encoder) { 134 encoder.print("<embeddable"); 135 if (getClassName() != null) { 136 encoder.print(" className=\""); 137 encoder.print(getClassName()); 138 encoder.print("\""); 139 } 140 encoder.println(">"); 141 142 encoder.indent(1); 143 encoder.print(attributes); 144 encoder.indent(-1); 145 encoder.println("</embeddable>"); 146 } 147 } 148 | Popular Tags |