001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.console.filter;
018
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023public abstract class ResultTransformFilter implements QueryFilter {
024    private QueryFilter next;
025
026    /**
027     * Constructs a query filter that transform the format of the query result
028     * 
029     * @param next - the query filter to retrieve the results from
030     */
031    protected ResultTransformFilter(QueryFilter next) {
032        this.next = next;
033    }
034
035    /**
036     * Transforms the queried results to a collection of different objects
037     * 
038     * @param query - the query string
039     * @return collections of transformed objects
040     * @throws Exception
041     */
042    public List query(String query) throws Exception {
043        return transformList(next.query(query));
044    }
045
046    /**
047     * Transforms the queried results to a collection of different objects
048     * 
049     * @param queries - the query map
050     * @return collections of transformed objects
051     * @throws Exception
052     */
053    public List<Object> query(List queries) throws Exception {
054        return transformList(next.query(queries));
055    }
056
057    /**
058     * Transforms a collection to a collection of different objects.
059     * 
060     * @param result - the collection to transform
061     * @return collection of properties objects
062     */
063    protected List<Object> transformList(List<Object> result) throws Exception {
064        List<Object> props = new ArrayList<Object>();
065
066        for (Iterator<Object> i = result.iterator(); i.hasNext();) {
067            props.add(transformElement(i.next()));
068        }
069
070        return props;
071    }
072
073    /**
074     * Transform a result object
075     * 
076     * @param obj - the object instance to transform
077     * @return the transformed object
078     */
079    protected abstract Object transformElement(Object obj) throws Exception;
080
081}