Salome HOME
app.root property is removed.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Any.java
1 package org.splat.dal.bo.kernel;
2 /**
3  * Abstract root class of persistent objects supporting dynamic attributes.<br/>
4  * Dynamic attributes are instances of concrete subclasses of Attribute that are assigned to Any objects at run time.
5  * The attributes of a given Any object must all be of different types.
6  * 
7  * @see Attribute
8  * @author    Daniel Brunier-Coulin
9  * @copyright OPEN CASCADE 2012
10  */
11
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16 import org.splat.kernel.InvalidPropertyException;
17 import org.splat.kernel.MissedPropertyException;
18 import org.splat.kernel.MultiplyDefinedException;
19 import org.splat.kernel.ObjectProperties;
20
21
22 public abstract class Any extends Persistent {
23
24     private  Set<Attribute> attributes = new HashSet<Attribute>();
25
26 //  ==============================================================================================================================
27 //  Constructors
28 //  ==============================================================================================================================
29
30 //  Database fetch constructor.
31     protected Any () {
32     }
33 //  Initialization constructors
34     protected Any (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
35 //  --------------------------------------
36       super(oprop);
37     }
38     protected Any (Attribute... field) {
39       for (int i=0; i<field.length; i++) {
40         if (field[i] == null) continue;     // Happen when newing an Any object without property 
41         if (field[i].getFrom().equals(this)) getAttributes().add(field[i]);
42       }
43     }
44
45 //  ==============================================================================================================================
46 //  Public member functions
47 //  ==============================================================================================================================
48
49     public Attribute getAttribute (Class<? extends Attribute> type) {
50       for (Iterator<Attribute> i=getAttributes().iterator(); i.hasNext(); ) {
51         Attribute field = i.next();
52         if (field.getClass().equals(type)) return field;
53       }
54       return null;
55     }
56
57 //  ==============================================================================================================================
58 //  Protected services
59 //  ==============================================================================================================================
60
61     protected boolean removeAttribute (Attribute field) {
62       for (Iterator<Attribute> i=getAttributes().iterator(); i.hasNext(); ) {
63         if (!i.next().equals(field)) continue;
64         i.remove();
65         return true;
66       }
67       return false;
68     }
69
70     public boolean setAttribute (Attribute field) {
71       Class<?> type    = field.getClass();
72
73       if (!field.getFrom().equals(this)) return false;
74       for (Iterator<Attribute> i=getAttributes().iterator(); i.hasNext(); ) {
75         if (!i.next().getClass().equals(type)) continue;
76         i.remove();
77         break;
78       }
79       getAttributes().add(field);
80       return true;
81     }
82         /**
83          * Get the attributes.
84          * @return the attributes
85          */
86         protected Set<Attribute> getAttributes() {
87                 return attributes;
88         }
89 }