/******************************************************************************* * Copyright (c) 2007, 2012 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.history.util; import org.simantics.databoard.Bindings; import org.simantics.databoard.accessor.ArrayAccessor; import org.simantics.databoard.accessor.error.AccessorException; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.RecordBinding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.history.HistoryException; /** * Stream iterator iterates sample entries in an array. * * Is scans the next sample and knows its time stamp. * * @author toni.kalajainen */ public class StreamIterator { // Sample binding RecordBinding sampleBinding; // Array accessor ArrayAccessor aa; // Array stream Stream stream; // From and end times of the whole stream double from, end; // Current and next sample Object current, next; // Utility for reading sample ValueBand valueBand, nextValueBand; // Start and end time of current sample double startTime, endTime; int index = -1; int size; public StreamIterator(ArrayAccessor aa) throws HistoryException { try { this.aa = aa; sampleBinding = (RecordBinding) Bindings.getBeanBinding( aa.type().componentType ); current = sampleBinding.createDefault(); next = sampleBinding.createDefault(); valueBand = new ValueBand(sampleBinding, current); nextValueBand = new ValueBand(sampleBinding, next); stream = new Stream( aa, sampleBinding ); size = aa.size(); if ( size>0 ) { aa.get(0, sampleBinding, current); from = valueBand.getTimeDouble(); aa.get(size-1, sampleBinding, current); end = valueBand.hasEndTime() ? valueBand.getEndTimeDouble() : valueBand.getTimeDouble(); } } catch (BindingException e) { throw new HistoryException( e ); } catch (AccessorException e) { throw new HistoryException( e ); } } /** * Go to time using random access * @param time * @return true if sample was found * @throws HistoryException */ public boolean gotoTime(double time) throws HistoryException { // Outside range if ( timeend ) { index = -1; return false; } // Already at cursor if ( time>=startTime && time=0) { gotoIndex( i ); } else { int insertPos = -i-2; if ( insertPos<0 || insertPos>=size ) { index = -1; } else { gotoIndex( insertPos ); if ( endTime