/*
 * Copyright (C) 2013 Conductor, Inc.
 *
 * 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 net.opentsdb.contrib.tsquare.support;

import java.util.Iterator;

import net.opentsdb.core.Aggregator.Doubles;
import net.opentsdb.core.DataPoint;
import net.opentsdb.core.DataPoints;

/**
 * Wraps a {@link DataPoints} object and exposes the data consistent
 * with the {@link Doubles} interface.
 * 
 * @author James Royalty (jroyalty) <i>[Jul 12, 2013]</i>
 */
public class DataPointsAsDoubles implements Doubles {
    private final Iterator<DataPoint> iterator;
    
    public DataPointsAsDoubles(final DataPoints points) {
        this.iterator = points.iterator();
    }
    
    @Override
    public boolean hasNextValue() {
        return iterator.hasNext();
    }

    @Override
    public double nextDoubleValue() {
        final DataPoint point = iterator.next();
        return TsWebUtils.asDouble(point);
    }
}

@@ -790,16 +790,142 @@
 oints;%0A%0A
+import com.google.common.base.Predicate;%0Aimport com.google.common.collect.Iterators;%0Aimport com.google.common.collect.Range;%0A%0A
 /**%0A * W
@@ -1196,16 +1196,57 @@
 erator;%0A
+    private final Range%3CLong%3E timeRange;%0A
     %0A   
@@ -1296,24 +1296,24 @@
 s points) %7B%0A
-
         this
@@ -1339,24 +1339,503 @@
 iterator();%0A
+        this.timeRange = Range.all();%0A    %7D%0A    %0A    public DataPointsAsDoubles(final DataPoints points, final Range%3CLong%3E withinTimeRange) %7B%0A        this.timeRange = withinTimeRange;%0A        %0A        Predicate%3CDataPoint%3E pred = new Predicate%3CDataPoint%3E() %7B%0A            public boolean apply(final DataPoint input) %7B%0A                return timeRange.contains(input.timestamp());%0A            %7D%0A        %7D;%0A        %0A        this.iterator = Iterators.filter(points.iterator(), pred);%0A
     %7D%0A    %0A 

Allow for filtering data points by time range /after/ the query has executed. Would be better to do this in TsdbQuery instead.