KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > GenericResponse


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.util;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.cayenne.QueryResponse;
27
28 /**
29  * A simple serializable implementation of QueryResponse.
30  *
31  * @since 1.2
32  * @author Andrus Adamchik
33  */

34 public class GenericResponse implements QueryResponse, Serializable JavaDoc {
35
36     protected List JavaDoc results;
37
38     protected transient int currentIndex;
39
40     /**
41      * Creates an empty BaseResponse.
42      */

43     public GenericResponse() {
44         results = new ArrayList JavaDoc();
45     }
46
47     /**
48      * Creates a BaseResponse with a single result list.
49      */

50     public GenericResponse(List JavaDoc list) {
51         results = new ArrayList JavaDoc(1);
52         addResultList(list);
53     }
54
55     /**
56      * Creates a response that it a shallow copy of another response.
57      */

58     public GenericResponse(QueryResponse response) {
59
60         results = new ArrayList JavaDoc(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 JavaDoc 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 JavaDoc currentList() {
94         return (List JavaDoc) 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 JavaDoc;
103     }
104
105     public boolean next() {
106         return ++currentIndex <= results.size();
107     }
108
109     public void reset() {
110         // use a zero-based index, not -1, as this will simplify serialization handling
111
currentIndex = 0;
112     }
113
114     public int size() {
115         return results.size();
116     }
117
118     /**
119      * Clears any previously collected information.
120      */

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 JavaDoc list) {
139         this.results.add(list);
140     }
141
142     /**
143      * Replaces previously stored result with a new result.
144      */

145     public void replaceResult(Object JavaDoc oldResult, Object JavaDoc newResult) {
146         int index = results.indexOf(oldResult);
147         if (index >= 0) {
148             results.set(index, newResult);
149         }
150     }
151 }
152
Popular Tags