1 23 24 29 30 package com.sun.jdo.api.persistence.model.jdo.impl; 31 32 import java.util.*; 33 import java.beans.PropertyVetoException ; 34 35 import com.sun.jdo.api.persistence.model.ModelException; 36 import com.sun.jdo.api.persistence.model.ModelVetoException; 37 import com.sun.jdo.api.persistence.model.jdo.*; 38 39 44 public class PersistenceElementCollection 45 { 46 47 private PersistenceElementImpl _owner; 48 49 50 private PersistenceElement[] _elements; 51 52 53 private Object [] _template; 54 55 56 private String _propertyName; 57 58 62 public PersistenceElementCollection () 63 { 64 this(null, null, null); 65 } 66 67 68 public PersistenceElementCollection (PersistenceElementImpl owner, 69 String propertyName, Object [] template) 70 { 71 _owner = owner; 72 _propertyName = propertyName; 73 _template = template; 74 } 75 76 83 public void changeElements (PersistenceElement[] elements, int action) 84 throws ModelException 85 { 86 changeElements(Arrays.asList(elements), action); 87 } 88 89 96 public void changeElements (List elements, int action) 97 throws ModelException 98 { 99 boolean changed = false; 100 101 try 102 { 103 PersistenceElement[] oldElements = getElements(); 104 int oldLength = (oldElements == null) ? 0 : oldElements.length; 105 int newLength = (elements == null) ? 0 : elements.size(); 106 List list = null; 107 108 switch (action) 109 { 110 case PersistenceElement.Impl.SET: 111 list = elements; 112 changed = true; 113 break; 114 case PersistenceElement.Impl.ADD: 115 if (newLength > 0) 116 { 117 list = ((oldLength == 0) ? new ArrayList() : 118 new ArrayList(Arrays.asList(oldElements))); 119 list.addAll(elements); 120 changed = true; 121 } 122 break; 123 case PersistenceElement.Impl.REMOVE: 124 if ((newLength > 0) && (oldLength > 0)) 125 { 126 list = new ArrayList(Arrays.asList(oldElements)); 127 list.removeAll(elements); 128 changed = true; 129 } 130 break; 131 } 132 if (changed) 133 { 134 try 135 { 136 _owner.fireVetoableChange(_propertyName, null, null); 137 _elements = (PersistenceElement[])list.toArray(_template); 138 } 139 catch (PropertyVetoException e) 140 { 141 throw new ModelVetoException(e); 142 } 143 } 144 } 145 finally 146 { 147 if (changed) 148 _owner.firePropertyChange(_propertyName, null, null); 149 } 150 } 151 152 156 public PersistenceElement[] getElements () { return _elements; } 157 158 164 public PersistenceElement getElement (String name) 165 { 166 PersistenceElement[] elements = getElements(); 167 int i, count = ((elements != null) ? elements.length : 0); 168 169 for (i = 0; i < count; i++) 170 { 171 PersistenceElement element = elements[i]; 172 173 if (name.equals(element.getName())) 174 return element; 175 } 176 177 return null; 178 } 179 180 182 186 public PersistenceElementImpl getOwner () { return _owner; } 187 188 193 public void setOwner (PersistenceElementImpl owner) 194 { 195 _owner = owner; 196 } 197 198 202 public Object [] getTemplate () { return _template; } 203 204 210 public void setTemplate (Object [] template) { _template = template; } 211 212 216 public String getPropertyName () { return _propertyName; } 217 218 224 public void setPropertyName (String propertyName) 225 { 226 _propertyName = propertyName; 227 } 228 229 234 public void setElements (PersistenceElement[] elements) 235 { 236 _elements = elements; 237 } 238 } 239 | Popular Tags |