001    package de.deepamehta.core.service;
002    
003    import de.deepamehta.core.DeepaMehtaObject;
004    import de.deepamehta.core.JSONEnabled;
005    import de.deepamehta.core.util.DeepaMehtaUtils;
006    
007    import org.codehaus.jettison.json.JSONObject;
008    
009    import java.util.ArrayList;
010    import java.util.Iterator;
011    import java.util.List;
012    
013    
014    
015    public class ResultList<T extends JSONEnabled> implements Iterable<T>, JSONEnabled {
016    
017        // ---------------------------------------------------------------------------------------------- Instance Variables
018    
019        private int totalCount;
020        private List<T> items;
021    
022        // ---------------------------------------------------------------------------------------------------- Constructors
023    
024        public ResultList() {
025            this.totalCount = 0;
026            this.items = new ArrayList<T>();
027        }
028    
029        public ResultList(int totalCount, List<T> items) {
030            this.totalCount = totalCount;
031            this.items = items;
032        }
033    
034        // -------------------------------------------------------------------------------------------------- Public Methods
035    
036        public T get(int index) {
037            return items.get(index);
038        }
039    
040        public int getSize() {
041            return items.size();
042        }
043    
044        public int getTotalCount() {
045            return totalCount;
046        }
047    
048        public List<T> getItems() {
049            return items;
050        }
051    
052        // ---
053    
054        public void add(T item) {
055            totalCount++;
056            items.add(item);
057        }
058    
059        public void addAll(ResultList<T> result) {
060            totalCount += result.getTotalCount();
061            items.addAll(result.getItems());
062        }
063    
064        // ---
065    
066        public ResultList<T> loadChildTopics() {
067            for (T item : this) {
068                // Note: we store also models in a result list. So we need a cast here.
069                ((DeepaMehtaObject) item).loadChildTopics();
070            }
071            return this;
072        }
073    
074        // *** Iterable Implementation ***
075    
076        @Override
077        public Iterator<T> iterator() {
078            return items.iterator();
079        }
080    
081        // *** JSONEnabled Implementation ***
082    
083        @Override
084        public JSONObject toJSON() {
085            try {
086                JSONObject o = new JSONObject();
087                o.put("total_count", totalCount);
088                o.put("items", DeepaMehtaUtils.objectsToJSON(items));
089                return o;
090            } catch (Exception e) {
091                throw new RuntimeException("Serialization failed (" + this + ")", e);
092            }
093        }
094    }