de.fischer.thotti.reportgen.combiner.AverageDayCombinerIterator.java Source code

Java tutorial

Introduction

Here is the source code for de.fischer.thotti.reportgen.combiner.AverageDayCombinerIterator.java

Source

/*
 * Copyright 2012 Oliver B. Fischer
 *
 * 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 de.fischer.thotti.reportgen.combiner;

import de.fischer.thotti.persistence.NDResultEntity;
import de.fischer.thotti.reportgen.diagram.Measurement;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

public class AverageDayCombinerIterator extends AbstractDayCombinerIterator<Measurement> {

    private long elementsDay;
    private long durationAggregatedDay;
    private Date dayOfNextElement;

    public AverageDayCombinerIterator(Iterator<NDResultEntity> source) {
        super(source);
    }

    @Override
    void processNextInputElement(NDResultEntity nextElement) {

        dayOfNextElement = nextElement.getStartedAt();
        elementsDay++;
        durationAggregatedDay += nextElement.getExecutionTime();
    }

    @Override
    Measurement getNextElementComputed() {
        Measurement result = new Measurement();
        Date date = DateUtils.truncate(dayOfNextElement, Calendar.DAY_OF_MONTH);
        result.setPointInTime(date);
        result.setDuration(durationAggregatedDay / elementsDay);

        return result;
    }

    @Override
    void prepareNextComputation() {
        elementsDay = 0;
        durationAggregatedDay = 0;
        dayOfNextElement = null;
    }
}