KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > integrity > GenericConditions


1 /*
2   Copyright (C) 2002 Julien van Malderen, Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.integrity;
19
20 import java.util.Iterator JavaDoc;
21 import org.apache.log4j.Logger;
22 import org.objectweb.jac.core.ACConfiguration;
23 import org.objectweb.jac.core.ObjectRepository;
24 import org.objectweb.jac.core.rtti.ClassItem;
25 import org.objectweb.jac.core.rtti.ClassRepository;
26 import org.objectweb.jac.core.rtti.FieldItem;
27
28 /**
29  * Some basic methods for constraints on fields values.
30  *
31  * <p>Constraint methods must return a Boolean that is
32  * <code>Boolean.TRUE</code> if the test has been validated (passed),
33  * <code>Boolean.FALSE</code> else. Their parameters are :</p>
34  *
35  * <ul><li><code>Wrappee wrappee</code>: the substance object
36  * (holding the field)</li>
37  *
38  * <li><code>FieldItem field</code>: the constrained field</li>
39  *
40  * <li><code>Object value</code>: the proposed future value of the
41  * field (can be refused by the contraint)</li>
42  *
43  * <li><code>Object[] values</code>: some configuration params that
44  * can be used in the test</li></ul>
45  *
46  * @see IntegrityAC#addPreCondition(FieldItem,MethodItem,Object[],String)
47  * @see IntegrityAC#addPostCondition(FieldItem,MethodItem,Object[],String)
48  * @see IntegrityAC#doCheck() */

49
50 public class GenericConditions {
51     static final Logger logger = Logger.getLogger("integrity.conditions");
52
53     /**
54      * If obj1 and obj2 are instances of String, uses equals(), else ==.
55      */

56     private static boolean areEqual(Object JavaDoc obj1, Object JavaDoc obj2)
57     {
58         logger.debug("areEqual("+obj1+","+obj2+")");
59         if(obj1 instanceof String JavaDoc && obj2 instanceof String JavaDoc) {
60             return ((String JavaDoc) obj1).equals((String JavaDoc) obj2);
61         }
62
63         return (obj1 == obj2);
64     }
65
66     /**
67      * Check if field's value is not equal to the forbidden values. If
68      * it is, the method returns <code>false</code>.
69      *
70      * @param substance the object that owns the field
71      * @param field the tested field
72      * @param value the value that is about to be set
73      * @param values the forbidden values
74      */

75     public static boolean forbiddenValues(Object JavaDoc substance,
76                                           FieldItem field,
77                                           Object JavaDoc value,
78                                           Object JavaDoc[] values)
79     {
80         for (int i=0; i<values.length; i++)
81             if (areEqual(value, values[i]))
82                 return false;
83         return true;
84     }
85
86     /**
87      * Check if field's value is equal to one of the authorized
88      * values. If it is not, the method returns
89      * <code>false</code>.
90      *
91      * @param substance the object that owns the field
92      * @param field the tested field
93      * @param value the value that is about to be set
94      * @param values the authorized values
95      */

96     public static boolean authorizedValues(Object JavaDoc substance,
97                                            FieldItem field,
98                                            Object JavaDoc value,
99                                            Object JavaDoc[] values)
100     {
101         for (int i=0; i<values.length; i++)
102             if (areEqual(value, values[i]))
103                 return true;
104         return false;
105     }
106
107     /**
108      * Check if this field already has the same value in another object
109      * of the same type.
110      *
111      * @param substance the object that owns the field
112      * @param field the tested field
113      * @param value the value that is about to be set
114      * @param values unused
115      */

116     public static boolean isUniqueValue(Object JavaDoc substance,
117                                         FieldItem field,
118                                         Object JavaDoc value,
119                                         Object JavaDoc[] values)
120     {
121         ClassItem cli = ClassRepository.get().getClass(substance);
122         Iterator JavaDoc it = ObjectRepository.getObjects(cli).iterator();
123         return true;
124     }
125
126     /**
127      * Tells if the value is an upper-case char begining string.
128      *
129      * @param substance the object that owns the field
130      * @param field the tested field
131      * @param value the value that is about to be set
132      * @param values unused
133      */

134     public static boolean isBeginingWithUpperCaseChar(Object JavaDoc substance,
135                                                       FieldItem field,
136                                                       Object JavaDoc value,
137                                                       Object JavaDoc[] values)
138     {
139         if (value instanceof String JavaDoc) {
140             String JavaDoc s = (String JavaDoc)value;
141             if (s==null || s.length()==0 ||
142                 Character.isUpperCase(s.charAt(0)))
143             {
144                 return true;
145             } else {
146                 return false;
147             }
148         }
149         throw new RuntimeException JavaDoc("Invalid constraint check: "+value+
150                                    " is not a string");
151     }
152
153     public static boolean isNotNull(Object JavaDoc substance,
154                                     FieldItem field,
155                                     Object JavaDoc value,
156                                     Object JavaDoc[] values)
157     {
158         return value != null;
159     }
160
161     public static boolean isNull(Object JavaDoc substance,
162                                  FieldItem field,
163                                  Object JavaDoc value,
164                                  Object JavaDoc[] values)
165     {
166         return value == null;
167     }
168
169
170     /**
171      * Tells if the value is a letter begining string.
172      *
173      * @param substance the object that owns the field
174      * @param field the tested field
175      * @param value the value that is about to be set
176      * @param values unused
177      */

178     public static boolean isBeginingWithLetter(Object JavaDoc substance,
179                                                FieldItem field,
180                                                Object JavaDoc value,
181                                                Object JavaDoc[] values)
182     {
183         if (value instanceof String JavaDoc) {
184             String JavaDoc s = (String JavaDoc)value;
185             if (s==null || s.length()==0 ||
186                 Character.isLetter(s.charAt(0)))
187             {
188                 return true;
189             } else {
190                 return false;
191             }
192         }
193         throw new RuntimeException JavaDoc("Invalid constraint check: "+value+
194                                    " is not a string");
195     }
196
197     /**
198      * Tells if the value is a valid java identifier.
199      *
200      * @param substance the object that owns the field
201      * @param field the tested field
202      * @param value the value that is about to be set
203      * @param values unused
204      */

205     public static boolean isJavaIdentifier(Object JavaDoc substance,
206                                            FieldItem field,
207                                            Object JavaDoc value,
208                                            Object JavaDoc[] values)
209     {
210         if (value instanceof String JavaDoc) {
211             String JavaDoc s=(String JavaDoc)value;
212             if (s==null)
213                 return true;
214             if (s.length()==0)
215                 return false;
216             if (!Character.isJavaIdentifierStart(s.charAt(0)))
217                 return false;
218             for(int i=1;i<s.length(); i++) {
219                 if(!Character.isJavaIdentifierPart(s.charAt(i)))
220                     return false;
221             }
222             return true;
223         }
224         throw new RuntimeException JavaDoc("Invalid constraint check: "+value+
225                                    " is not a string");
226     }
227
228     /**
229      * Tells if the value is greater than a given number.
230      *
231      * @param substance the object that owns the field
232      * @param field the tested field
233      * @param value the value that is about to be set
234      * @param values unused
235      */

236     public static boolean isGreaterThan(Object JavaDoc substance,
237                                         FieldItem field,
238                                         Object JavaDoc value,
239                                         Object JavaDoc[] values)
240         throws Exception JavaDoc
241     {
242         if (value instanceof Number JavaDoc) {
243             Number JavaDoc th = (Number JavaDoc)ACConfiguration.convertValue(values[0],
244                                                              value.getClass());
245             if (((Number JavaDoc)value).doubleValue()>th.doubleValue()) {
246                 return true;
247             } else {
248                 return false;
249             }
250         }
251         throw new RuntimeException JavaDoc("Invalid constraint check: "+value+
252                                    " is not a number");
253     }
254
255     /**
256      * Tells if the value is lower than a given number.
257      *
258      * @param substance the object that owns the field
259      * @param field the tested field
260      * @param value the value that is about to be set
261      * @param values unused
262      */

263     public static boolean isLowerThan(Object JavaDoc substance,
264                                       FieldItem field,
265                                       Object JavaDoc value,
266                                       Object JavaDoc[] values)
267         throws Exception JavaDoc
268     {
269         if (value instanceof Number JavaDoc) {
270             Number JavaDoc th = (Number JavaDoc)ACConfiguration.convertValue(values[0],
271                                                              value.getClass());
272             if (((Number JavaDoc)value).doubleValue()<th.doubleValue()) {
273                 return true;
274             } else {
275                 return false;
276             }
277         }
278         throw new RuntimeException JavaDoc("Invalid constraint check: "+value+
279                                    " is not a number");
280     }
281
282 }
283
Popular Tags