KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.cayenne.QueryResponse;
28
29 /**
30  * A QueryResponse optimized to hold a single object or data row list.
31  *
32  * @since 1.2
33  * @author Andrus Adamchik
34  */

35 public class ListResponse implements QueryResponse, Serializable JavaDoc {
36
37     protected List JavaDoc objectList;
38
39     protected transient int currentIndex;
40
41     /**
42      * Creates an empty response.
43      */

44     public ListResponse() {
45         this.objectList = new ArrayList JavaDoc(1);
46     }
47
48     public ListResponse(Object JavaDoc object) {
49         this.objectList = Collections.singletonList(object);
50     }
51
52     public ListResponse(List JavaDoc objectList) {
53         this.objectList = objectList;
54     }
55
56     public int size() {
57         return 1;
58     }
59
60     public boolean isList() {
61         if (currentIndex != 1) {
62             throw new IndexOutOfBoundsException JavaDoc("Past iteration end: " + currentIndex);
63         }
64
65         return true;
66     }
67
68     public List JavaDoc currentList() {
69         if (currentIndex != 1) {
70             throw new IndexOutOfBoundsException JavaDoc("Past iteration end: " + currentIndex);
71         }
72
73         return objectList;
74     }
75
76     public int[] currentUpdateCount() {
77         throw new IllegalStateException JavaDoc("Current object is not an update count");
78     }
79
80     public boolean next() {
81         return ++currentIndex <= 1;
82     }
83
84     public void reset() {
85         // use a zero-based index, not -1, as this will simplify serialization handling
86
currentIndex = 0;
87     }
88
89     public List JavaDoc firstList() {
90         return objectList;
91     }
92
93     public int[] firstUpdateCount() {
94         return new int[0];
95     }
96 }
97
Popular Tags