View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.krms.framework.engine;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.rice.krms.api.engine.EngineResults;
24  import org.kuali.rice.krms.api.engine.ResultEvent;
25  
26  /**
27   * An implementation of {@link EngineResults} using List<{@link ResultEvent}> for results and Map<String, Object> for
28   * attributes
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class EngineResultsImpl implements EngineResults {
32  	
33  	private List<ResultEvent> results = new ArrayList<ResultEvent>();
34  	private Map<String, Object> attributes = new HashMap<String, Object>();
35  	
36  	@Override
37  	public void addResult(ResultEvent result) {
38  		results.add(result);
39  	}
40  
41      /**
42       * Return a shallow copy of the list of ResultEvents.
43       * @return a shallow copy of all the ResultEvents
44       */
45  	@Override
46  	public List<ResultEvent> getAllResults() {		
47  		return new ArrayList<ResultEvent>(results); // shallow copy should be defensive enough
48  	}
49  
50      /**
51       * Returns null, unimplemented.
52       * @param index of the ResultEvent to return
53       * @return null
54       *
55       * @deprecated use {@link #getAllResults()} instead, this method will be removed in a future version
56       */
57  	@Override
58      @Deprecated
59  	public ResultEvent getResultEvent(int index) {
60  		return getAllResults().get(index);
61  	}
62  
63  	@Override
64  	public List<ResultEvent> getResultsOfType(String type) {
65  		// TODO Auto-generated method stub
66  		ArrayList<ResultEvent> newList = new ArrayList<ResultEvent>();
67  		if (type == null) return newList;
68  		for (int i=0; i<results.size(); i++){
69  			if (type.equalsIgnoreCase(results.get(i).getType())){
70  				newList.add(results.get(i));
71  			}
72  		}
73  		return newList;
74  	}
75  	
76  	/**
77  	 * @see org.kuali.rice.krms.api.engine.EngineResults#getAttribute(java.lang.String)
78  	 */
79  	@Override
80  	public Object getAttribute(String key) {
81  	    return attributes.get(key);
82  	}
83  	
84  	/**
85  	 * @see org.kuali.rice.krms.api.engine.EngineResults#setAttribute(java.lang.String, java.lang.Object)
86  	 */
87  	@Override
88  	public void setAttribute(String key, Object attr) {
89  	    attributes.put(key, attr);
90  	}
91  	
92  }