KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > metadata > support > AbstractAttributes


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.metadata.support;
18
19 import java.lang.reflect.Field JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.springframework.metadata.Attributes;
27
28 /**
29  * Convenient superclass for Attributes implementations.
30  * Implements filtering and saves attribute packages.
31  *
32  * <p>TODO could implement caching here for efficiency,
33  * or add a caching decorator (probably a better idea).
34  *
35  * @author Rod Johnson
36  */

37 public abstract class AbstractAttributes implements Attributes {
38     
39     /**
40      * @see org.springframework.metadata.Attributes#getAttributes(java.lang.Class, java.lang.Class)
41      */

42     public final Collection JavaDoc getAttributes(Class JavaDoc targetClass, Class JavaDoc filter) {
43         return filter(getAttributes(targetClass), filter);
44     }
45
46     /**
47      * Filter these attributes to those matching the filter type
48      */

49     private Collection JavaDoc filter(Collection JavaDoc coll, Class JavaDoc filter) {
50         if (filter == null) {
51             return coll;
52         }
53
54         List JavaDoc matches = new LinkedList JavaDoc();
55         for (Iterator JavaDoc it = coll.iterator(); it.hasNext(); ) {
56             Object JavaDoc next = it.next();
57             if (filter.isInstance(next)) {
58                 matches.add(next);
59             }
60         }
61         return matches;
62     }
63
64     /**
65      * @see org.springframework.metadata.Attributes#getAttributes(java.lang.reflect.Method, java.lang.Class)
66      */

67     public final Collection JavaDoc getAttributes(Method JavaDoc targetMethod, Class JavaDoc filter) {
68         return filter(getAttributes(targetMethod), filter);
69     }
70
71     /**
72      * @see org.springframework.metadata.Attributes#getAttributes(java.lang.reflect.Field, java.lang.Class)
73      */

74     public final Collection JavaDoc getAttributes(Field JavaDoc targetField, Class JavaDoc filter) {
75         return filter(getAttributes(targetField), filter);
76     }
77
78 }
79
Popular Tags