View Javadoc

1   /**
2    * Copyright 2005-2014 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.core.api.criteria;
17  
18  import org.apache.commons.lang.builder.EqualsBuilder;
19  import org.apache.commons.lang.builder.HashCodeBuilder;
20  import org.apache.commons.lang.builder.ToStringBuilder;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.core.api.mo.ModelObjectComplete;
23  
24  import java.io.Serializable;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  
29  public final class GenericQueryResults<T> implements QueryResults<T> {
30  
31  	private final List<T> results;
32  	private final Integer totalRowCount;
33  	private final boolean moreResultsAvailable;
34  	
35  	private GenericQueryResults(Builder<T> builder) {
36  		this.results = builder.getResults() != null ? Collections.unmodifiableList(new ArrayList<T>(builder.getResults())) : Collections.<T>emptyList();
37  		this.totalRowCount = builder.getTotalRowCount();
38  		this.moreResultsAvailable = builder.isMoreResultsAvailable();
39  	}
40  
41  	@Override
42  	public List<T> getResults() {
43  		return results;
44  	}
45  	
46  	@Override
47  	public Integer getTotalRowCount() {
48  		return totalRowCount;
49  	}
50  
51  	@Override
52  	public boolean isMoreResultsAvailable() {
53  		return moreResultsAvailable;
54  	}
55  
56      @Override
57  	public int hashCode() {
58  		return HashCodeBuilder.reflectionHashCode(this);
59  	}
60  
61  	@Override
62  	public boolean equals(Object obj) {
63  		return EqualsBuilder.reflectionEquals(obj, this);
64  	}
65  
66  	@Override
67  	public String toString() {
68  		return ToStringBuilder.reflectionToString(this);
69  	}
70  
71  	public static final class Builder<T> implements ModelBuilder, QueryResults<T>, Serializable {
72  
73  		private List<T> results;
74  		private Integer totalRowCount;
75  		private boolean moreResultsAvailable;
76  
77          public static <T> Builder create() {
78              return new Builder<T>();
79          }
80  
81  		private Builder() {
82  			this.results = new ArrayList<T>();
83  			this.moreResultsAvailable = false;
84  		}
85  
86          @Override
87  		public GenericQueryResults<T> build() {
88  			return new GenericQueryResults<T>(this);
89  		}
90  
91  		public List<T> getResults() {
92  			return this.results;
93  		}
94  
95  		public void setResults(List<T> results) {
96  			if (results == null) {
97                  throw new IllegalArgumentException("results is null");
98              }
99  
100             this.results = Collections.unmodifiableList(new ArrayList<T>(results));
101 		}
102 
103 		public Integer getTotalRowCount() {
104 			return this.totalRowCount;
105 		}
106 
107 		public void setTotalRowCount(Integer totalRowCount) {
108 			if (totalRowCount != null && totalRowCount < 0) {
109                 throw new IllegalArgumentException("totalRowCount < 0");
110             }
111 
112             this.totalRowCount = totalRowCount;
113 		}
114 
115 		public boolean isMoreResultsAvailable() {
116 			return this.moreResultsAvailable;
117 		}
118 
119 		public void setMoreResultsAvailable(boolean moreResultsAvailable) {
120 			this.moreResultsAvailable = moreResultsAvailable;
121 		}
122 	}
123 }