
org.mongojack.AggregationResult Maven / Gradle / Ivy
/*
* Copyright 2014 Christopher Exell
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mongojack;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.mongodb.AggregationOutput;
import com.mongodb.DBObject;
/**
* A Generic AggregationResult class that wraps a mongodb AggregationOutput to deserialize
* the DBObjects coming from mongo into the desired type T.
*
* @param The return type of the aggregation.
*
* @author Christopher Exell
* @since 2.1.0
*/
public class AggregationResult {
private AggregationOutput output;
private List results;
private JacksonDBCollection, ?> collection;
private Class resultType;
public AggregationResult(JacksonDBCollection, ?> collection, AggregationOutput output, Class resultType) {
this.output = output;
this.collection = collection;
this.resultType = resultType;
}
/**
* Return the underlying AggregationOutput Object received from mongo.
*
* @return The AggregationOutput from mongo.
*/
public AggregationOutput getAggregationOutput()
{
return output;
}
/**
* returns an iterator to the results of the aggregation converted
* back to the type of the generic using the Jackson ObjectMapper
* from the JacksonDBCollection from which this AggregationResult
* was created.
*
* @return A List Containing the result of the aggregation.
*/
public List results() {
// only generate our results list once
if (results == null) {
results = new ArrayList();
Iterable iterable = output.results();
Iterator iter = iterable.iterator();
while (iter.hasNext())
{
results.add((T) collection.convertFromDbObject(iter.next(), resultType));
}
}
return results;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy