001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krms.framework.engine;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.rice.krms.api.engine.EngineResults;
024    import org.kuali.rice.krms.api.engine.ResultEvent;
025    
026    /**
027     * An implementation of {@link EngineResults} using List<{@link ResultEvent}> for results and Map<String, Object> for
028     * attributes
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    public class EngineResultsImpl implements EngineResults {
032            
033            private List<ResultEvent> results = new ArrayList<ResultEvent>();
034            private Map<String, Object> attributes = new HashMap<String, Object>();
035            
036            @Override
037            public void addResult(ResultEvent result) {
038                    results.add(result);
039            }
040    
041        /**
042         * Return a shallow copy of the list of ResultEvents.
043         * @return a shallow copy of all the ResultEvents
044         */
045            @Override
046            public List<ResultEvent> getAllResults() {                
047                    return new ArrayList<ResultEvent>(results); // shallow copy should be defensive enough
048            }
049    
050        /**
051         * Returns null, unimplemented.
052         * @param index of the ResultEvent to return
053         * @return null
054         *
055         * @deprecated use {@link #getAllResults()} instead, this method will be removed in a future version
056         */
057            @Override
058        @Deprecated
059            public ResultEvent getResultEvent(int index) {
060                    return getAllResults().get(index);
061            }
062    
063            @Override
064            public List<ResultEvent> getResultsOfType(String type) {
065                    // TODO Auto-generated method stub
066                    ArrayList<ResultEvent> newList = new ArrayList<ResultEvent>();
067                    if (type == null) return newList;
068                    for (int i=0; i<results.size(); i++){
069                            if (type.equalsIgnoreCase(results.get(i).getType())){
070                                    newList.add(results.get(i));
071                            }
072                    }
073                    return newList;
074            }
075            
076            /**
077             * @see org.kuali.rice.krms.api.engine.EngineResults#getAttribute(java.lang.String)
078             */
079            @Override
080            public Object getAttribute(String key) {
081                return attributes.get(key);
082            }
083            
084            /**
085             * @see org.kuali.rice.krms.api.engine.EngineResults#setAttribute(java.lang.String, java.lang.Object)
086             */
087            @Override
088            public void setAttribute(String key, Object attr) {
089                attributes.put(key, attr);
090            }
091            
092    }