X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.history%2Fsrc%2Forg%2Fsimantics%2Fhistory%2Futil%2FStreamIterator.java;fp=bundles%2Forg.simantics.history%2Fsrc%2Forg%2Fsimantics%2Fhistory%2Futil%2FStreamIterator.java;h=c30862af53c185dc2a1cbb52150f0b9e5704fcb4;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.history/src/org/simantics/history/util/StreamIterator.java b/bundles/org.simantics.history/src/org/simantics/history/util/StreamIterator.java new file mode 100644 index 000000000..c30862af5 --- /dev/null +++ b/bundles/org.simantics.history/src/org/simantics/history/util/StreamIterator.java @@ -0,0 +1,291 @@ +/******************************************************************************* + * 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