KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > query > mof > FeatureFilter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.jmi.query.mof;
20
21 import java.util.Comparator JavaDoc;
22
23 import javax.jmi.model.*;
24 import javax.jmi.reflect.RefFeatured;
25
26 import org.netbeans.lib.jmi.query.*;
27
28 /** Abstract class for creating filter queries.
29  *
30  * @author Petr Hrebejk
31  */

32 public class FeatureFilter extends ValueFilter {
33
34     /** Name of the feature to filter by */
35     protected String JavaDoc featureName;
36
37     /** Cache for the meta class and feature */
38     private javax.jmi.model.Feature feature = null;
39     private javax.jmi.model.MofClass metaClazz = null;
40
41     public FeatureFilter(Query query, String JavaDoc featureName, Object JavaDoc value) {
42         super( query, value );
43         this.featureName = featureName;
44     }
45      
46     public FeatureFilter(Query query, String JavaDoc featureName, Object JavaDoc value, Comparator JavaDoc comparator ) {
47         super( query, value, comparator );
48         this.featureName = featureName;
49     }
50     
51     protected boolean accept( Object JavaDoc object ) {
52         
53         if ( object instanceof RefFeatured ) {
54             resolveFeature( (RefFeatured)object );
55
56             if ( feature == null ) {
57                 return false;
58             }
59             Object JavaDoc featureValue = ((RefFeatured)object).refGetValue( (javax.jmi.model.StructuralFeature) feature );
60
61
62             return super.accept( featureValue );
63         }
64         else {
65             return false;
66         }
67     }
68     
69     
70     /** Finds the right feature for filtering, caches the information
71     * to perform quicker.
72     */

73     private void resolveFeature( RefFeatured refFeatured ) {
74         javax.jmi.model.MofClass clazz = (javax.jmi.model.MofClass)refFeatured.refMetaObject();
75         
76         
77         
78         if ( metaClazz == null || !metaClazz.equals( clazz ) || feature != null) {
79             metaClazz = null;
80             feature = null;
81             
82             try {
83                 metaClazz = clazz;
84                
85                 ModelElement me = clazz.lookupElementExtended( featureName );
86                 if ( me instanceof Feature ) {
87                     feature = (Feature) me;
88                 }
89             }
90             catch( NameNotFoundException e ) {
91                 // Let feature be null
92
}
93         }
94     }
95     
96 }
97
Popular Tags