Salome HOME
2e41832c4347770229fc2e2129f7dabc0a66d4ee
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Any.java
1 package org.splat.dal.bo.kernel;
2
3 /**
4  * Abstract root class of persistent objects supporting dynamic attributes.<br/>
5  * Dynamic attributes are instances of concrete subclasses of Attribute that are assigned to Any objects at run time.
6  * The attributes of a given Any object must all be of different types.
7  * 
8  * @see Attribute
9  * @author    Daniel Brunier-Coulin
10  * @copyright OPEN CASCADE 2012-2014
11  */
12
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Set;
16
17 import org.splat.kernel.InvalidPropertyException;
18 import org.splat.kernel.MissedPropertyException;
19 import org.splat.kernel.MultiplyDefinedException;
20 import org.splat.kernel.ObjectProperties;
21
22 public abstract class Any extends Persistent {
23
24         private final Set<Attribute> attributes = new HashSet<Attribute>();
25
26         // ==============================================================================================================================
27         // Constructors
28         // ==============================================================================================================================
29
30         // Database fetch constructor.
31         protected Any() {
32                 super();
33         }
34
35         // Initialization constructors
36         protected Any(final ObjectProperties oprop) throws MissedPropertyException,
37                         InvalidPropertyException, MultiplyDefinedException {
38                 super(oprop);
39         }
40
41         protected Any(final Attribute... field) {
42                 for (int i = 0; i < field.length; i++) {
43                         if (field[i] == null) {
44                                 continue; // Happen when newing an Any object without property
45                         }
46                         if (field[i].getFrom().equals(this)) {
47                                 getAttributes().add(field[i]);
48                         }
49                 }
50         }
51
52         // ==============================================================================================================================
53         // Public member functions
54         // ==============================================================================================================================
55
56         public Attribute getAttribute(final Class<? extends Attribute> type) {
57                 Attribute found = null;
58                 for (Attribute field : getAttributes()) {
59                         if (field.getClass().equals(type)) {
60                                 found = field;
61                                 break;
62                         }
63                 }
64                 return found;
65         }
66
67         // ==============================================================================================================================
68         // Protected services
69         // ==============================================================================================================================
70
71         public boolean removeAttribute(final Attribute field) {
72                 boolean res = false;
73                 for (Iterator<Attribute> i = getAttributes().iterator(); i.hasNext();) {
74                         if (i.next().equals(field)) {
75                                 i.remove();
76                                 res = true;
77                                 break;
78                         }
79                 }
80                 return res;
81         }
82
83         public boolean setAttribute(final Attribute field) {
84                 Class<?> type = field.getClass();
85
86                 if (!field.getFrom().equals(this)) {
87                         return false;
88                 }
89                 for (Iterator<Attribute> i = getAttributes().iterator(); i.hasNext();) {
90                         if (i.next().getClass().equals(type)) {
91                                 i.remove();
92                                 break;
93                         }
94                 }
95                 getAttributes().add(field);
96                 return true;
97         }
98
99         /**
100          * Get the attributes.
101          * 
102          * @return the attributes
103          */
104         protected Set<Attribute> getAttributes() {
105                 return attributes;
106         }
107
108         /**
109          * {@inheritDoc}
110          * 
111          * @see org.splat.dal.bo.kernel.Persistent#evict()
112          */
113         @Override
114         public void evict() {
115                 super.evict();
116                 // Evict all attributes of the persistent object
117                 Set<Attribute> tmpSet = new HashSet<Attribute>();
118                 tmpSet.addAll(attributes);
119                 attributes.clear();
120                 for (Attribute field : tmpSet) {
121                         if (field.isSaved()) { // to avoid recursive evict
122                                 field.evict();
123                         }
124                         attributes.add(field);
125                 }
126         }
127 }