1 19 20 package org.apache.cayenne.util; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 import org.apache.cayenne.QueryResponse; 27 28 34 public class GenericResponse implements QueryResponse, Serializable { 35 36 protected List results; 37 38 protected transient int currentIndex; 39 40 43 public GenericResponse() { 44 results = new ArrayList (); 45 } 46 47 50 public GenericResponse(List list) { 51 results = new ArrayList (1); 52 addResultList(list); 53 } 54 55 58 public GenericResponse(QueryResponse response) { 59 60 results = new ArrayList (response.size()); 61 62 response.reset(); 63 while (response.next()) { 64 if (response.isList()) { 65 addResultList(response.currentList()); 66 } 67 else { 68 addBatchUpdateCount(response.currentUpdateCount()); 69 } 70 } 71 } 72 73 public List firstList() { 74 for (reset(); next();) { 75 if (isList()) { 76 return currentList(); 77 } 78 } 79 80 return null; 81 } 82 83 public int[] firstUpdateCount() { 84 for (reset(); next();) { 85 if (!isList()) { 86 return currentUpdateCount(); 87 } 88 } 89 90 return null; 91 } 92 93 public List currentList() { 94 return (List ) results.get(currentIndex - 1); 95 } 96 97 public int[] currentUpdateCount() { 98 return (int[]) results.get(currentIndex - 1); 99 } 100 101 public boolean isList() { 102 return results.get(currentIndex - 1) instanceof List ; 103 } 104 105 public boolean next() { 106 return ++currentIndex <= results.size(); 107 } 108 109 public void reset() { 110 currentIndex = 0; 112 } 113 114 public int size() { 115 return results.size(); 116 } 117 118 121 public void clear() { 122 results.clear(); 123 } 124 125 public void addBatchUpdateCount(int[] resultCount) { 126 127 if (resultCount != null) { 128 results.add(resultCount); 129 } 130 } 131 132 public void addUpdateCount(int resultCount) { 133 results.add(new int[] { 134 resultCount 135 }); 136 } 137 138 public void addResultList(List list) { 139 this.results.add(list); 140 } 141 142 145 public void replaceResult(Object oldResult, Object newResult) { 146 int index = results.indexOf(oldResult); 147 if (index >= 0) { 148 results.set(index, newResult); 149 } 150 } 151 } 152 | Popular Tags |