KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > filter > PropertiesViewFilter


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.filter;
19
20 import java.util.Set JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 public class PropertiesViewFilter implements QueryFilter {
29     protected QueryFilter next;
30     protected Set JavaDoc viewFilter;
31
32     /**
33      * Creates a filter that will select the properties of a map object to view
34      * @param next - the next query filter that will return a collection of maps
35      */

36     public PropertiesViewFilter(QueryFilter next) {
37         this(null, next);
38     }
39
40     /**
41      * Creates a filter that will select the properties of a map object to view
42      * @param viewFilter - the properties to view
43      * @param next - the next query filter that will return a collection of maps
44      */

45     public PropertiesViewFilter(Set JavaDoc viewFilter, QueryFilter next) {
46         this.next = next;
47         this.viewFilter = viewFilter;
48     }
49
50     /**
51      * Filter the properties to view of the query result
52      * @param query - the query string
53      * @return list of objects that has been view filtered
54      */

55     public List JavaDoc query(String JavaDoc query) throws Exception JavaDoc {
56         return filterViewCollection(next.query(query), viewFilter);
57     }
58
59     /**
60      * Filter the properties to view of the query result
61      * @param queries - the query map
62      * @return list of objects that has been view filtered
63      * @throws Exception
64      */

65     public List JavaDoc query(List JavaDoc queries) throws Exception JavaDoc {
66         return filterViewCollection(next.query(queries), viewFilter);
67     }
68
69     /**
70      * Filter the view of each element in the collection
71      * @param result - the lists to filter the view from
72      * @param viewFilter - the views to select
73      * @return lsit of objects whose view has been filtered
74      */

75     protected List JavaDoc filterViewCollection(Collection JavaDoc result, Set JavaDoc viewFilter) {
76         // Use a list to allow duplicate entries
77
List JavaDoc newCollection = new ArrayList JavaDoc();
78
79         for (Iterator JavaDoc i=result.iterator(); i.hasNext();) {
80             newCollection.add(filterView((Map JavaDoc)i.next()));
81         }
82
83         return newCollection;
84     }
85
86     /**
87      * Select only the attributes to view from the map data
88      * @param data - data to filter the view from
89      * @return - data with the view filtered
90      */

91     protected Map JavaDoc filterView(Map JavaDoc data) {
92         // If no view specified, display all attributes
93
if (viewFilter == null || viewFilter.isEmpty()) {
94             return data;
95         }
96
97         Map JavaDoc newData;
98         try {
99             // Lets try to use the same class as the original
100
newData = (Map JavaDoc)data.getClass().newInstance();
101         } catch (Exception JavaDoc e) {
102             // Lets use a default HashMap
103
newData = new HashMap JavaDoc();
104         }
105
106         // Filter the keys to view
107
for (Iterator JavaDoc i=viewFilter.iterator(); i.hasNext();) {
108             Object JavaDoc key = i.next();
109             Object JavaDoc val = data.get(key);
110
111             if (val != null) {
112                 newData.put(key, val);
113             }
114         }
115
116         return newData;
117     }
118
119 }
120
Popular Tags