-/*******************************************************************************\r
- * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.databoard.accessor.impl;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.accessor.MapAccessor;\r
-import org.simantics.databoard.accessor.error.AccessorException;\r
-import org.simantics.databoard.binding.ArrayBinding;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.BooleanBinding;\r
-import org.simantics.databoard.binding.NumberBinding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.impl.BooleanArrayBinding;\r
-import org.simantics.databoard.binding.impl.ByteArrayBinding;\r
-import org.simantics.databoard.binding.impl.DoubleArrayBinding;\r
-import org.simantics.databoard.binding.impl.FloatArrayBinding;\r
-import org.simantics.databoard.binding.impl.IntArrayBinding;\r
-import org.simantics.databoard.binding.impl.LongArrayBinding;\r
-import org.simantics.databoard.type.ArrayType;\r
-import org.simantics.databoard.util.Limit;\r
-import org.simantics.databoard.util.Range;\r
-\r
-/**\r
- * This helper class creates an iterator to MapAccessor\r
- *\r
- * @param <K> key class\r
- * @param <V> value class\r
- * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
- */\r
-public class MapAccessorIterator<K, V> {\r
- \r
- MapAccessor map;\r
- Binding keyBinding;\r
- Binding valueBinding;\r
- \r
- Object from;\r
- boolean fromInclusive;\r
- \r
- Object end;\r
- boolean endInclusive;\r
-\r
- ArrayBinding keyCacheBinding;\r
- Object keyCache;\r
- \r
- ArrayBinding valueCacheBinding;\r
- Object valueCache;\r
- \r
- // Entry index, i.e. the number of entries read starting from 0\r
- int index = -1;\r
- \r
- // The number of samples written to cache\r
- int samplesInCache = 0;\r
- \r
- // The index in cache\r
- int cacheIndex = -1;\r
- \r
- /**\r
- * Initialize iterator with a cache. The cache should already be allocated with some empty elements.\r
- * @param map\r
- * @param keyBinding\r
- * @param from\r
- * @param fromInclusive\r
- * @param end\r
- * @param endInclusive\r
- * @param keyCacheBinding\r
- * @param keyCache\r
- * @param valueCacheBinding\r
- * @param valueCache\r
- */\r
- public MapAccessorIterator(\r
- MapAccessor map, \r
- Binding keyBinding, Object from, boolean fromInclusive, Object end, boolean endInclusive, \r
- ArrayBinding keyCacheBinding, Object keyCache,\r
- ArrayBinding valueCacheBinding, Object valueCache) {\r
- this.map = map;\r
- this.keyBinding = keyBinding;\r
- this.from = from;\r
- this.fromInclusive = fromInclusive;\r
- this.end = end;\r
- this.endInclusive = endInclusive;\r
- this.valueBinding = valueCacheBinding.getComponentBinding();\r
- this.keyCacheBinding = keyCacheBinding;\r
- this.keyCache = keyCache;\r
- this.valueCacheBinding = valueCacheBinding;\r
- this.valueCache = valueCache;\r
- }\r
-\r
- /**\r
- * Initialize map accessor iterator with a default cache\r
- * @param map\r
- * @param keyBinding\r
- * @param from\r
- * @param fromInclusive\r
- * @param end\r
- * @param endInclusive\r
- * @param valueBinding\r
- * @param cacheSize\r
- * @throws BindingException \r
- * @throws AccessorException \r
- */\r
- public MapAccessorIterator(\r
- MapAccessor map, \r
- Binding keyBinding, Object from, boolean fromInclusive, Object end, boolean endInclusive,\r
- Binding valueBinding,\r
- int cacheSize) throws BindingException, AccessorException {\r
- this.map = map;\r
- this.keyBinding = keyBinding;\r
- this.from = from;\r
- this.fromInclusive = fromInclusive;\r
- this.end = end;\r
- this.endInclusive = endInclusive;\r
- this.valueBinding = valueBinding;\r
- \r
- // TODO calc num of elements if source\r
-// int count = map.count(keyBinding, from, fromInclusive, end, endInclusive); \r
-// int len = Math.min(count, 256);\r
- Range range = new Range(Limit.inclusive(cacheSize), Limit.nolimit());\r
- this.keyCacheBinding = Bindings.getBinding( new ArrayType(keyBinding.type(), range) );\r
- this.keyCache = keyCacheBinding.createDefault();\r
- this.valueCacheBinding = Bindings.getBinding( new ArrayType(valueBinding.type(), range) );\r
- this.valueCache = valueCacheBinding.createDefault();\r
- }\r
-\r
- /**\r
- * Reads data to cache.\r
- * \r
- * Updates from and fromInclusive fields for next read.\r
- * Updates samplesInCache field.\r
- * Sets cacheIndex to 0.\r
- * \r
- * @throws AccessorException\r
- */\r
- private void fillCache() throws AccessorException {\r
- try {\r
- int limit = Math.min(keyCacheBinding.size(keyCache), valueCacheBinding.size(valueCache));\r
- if (limit==0) throw new AccessorException("You should have some entries in the cache");\r
- \r
- cacheIndex = -1;\r
- \r
- if (from==null) {\r
- samplesInCache = 0;\r
- return;\r
- }\r
- \r
- samplesInCache = map.getEntries(\r
- keyBinding, from, fromInclusive, \r
- end, endInclusive, \r
- keyCacheBinding, keyCache, \r
- valueCacheBinding, valueCache, \r
- limit);\r
- \r
- if (samplesInCache==0) {\r
- from = null;\r
- return;\r
- }\r
- \r
- from = keyCacheBinding.get(keyCache, samplesInCache-1);\r
- fromInclusive = false;\r
- } catch (BindingException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
- \r
- public boolean hasNext() throws AccessorException {\r
- if (from==null) return false; \r
- if (cacheIndex>=samplesInCache-1) fillCache();\r
- if (cacheIndex>=samplesInCache-1) return false;\r
- return cacheIndex+1<samplesInCache;\r
- }\r
- \r
- public boolean next() throws AccessorException {\r
- if (from==null) return false;\r
- if (cacheIndex>=samplesInCache-1) fillCache();\r
- if (cacheIndex>=samplesInCache-1) return false; \r
- cacheIndex++;\r
- index++;\r
- return true;\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public K key() throws AccessorException {\r
- try {\r
- return (K) keyCacheBinding.get(keyCache, cacheIndex);\r
- } catch (IndexOutOfBoundsException e) {\r
- throw new AccessorException(e);\r
- } catch (BindingException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- public V value() throws AccessorException {\r
- try {\r
- return (V) valueCacheBinding.get(valueCache, cacheIndex);\r
- } catch (IndexOutOfBoundsException e) {\r
- throw new AccessorException(e);\r
- } catch (BindingException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
- \r
- public double keyDouble() throws AccessorException {\r
- try {\r
- if (keyCacheBinding instanceof FloatArrayBinding) {\r
- return ((float[]) keyCache)[cacheIndex];\r
- }\r
- if (keyCacheBinding instanceof DoubleArrayBinding) {\r
- return ((double[]) keyCache)[cacheIndex];\r
- }\r
- if (keyCacheBinding instanceof ByteArrayBinding) {\r
- return ((byte[]) keyCache)[cacheIndex];\r
- }\r
- if (keyCacheBinding instanceof IntArrayBinding) {\r
- return ((int[]) keyCache)[cacheIndex];\r
- }\r
- if (keyCacheBinding instanceof LongArrayBinding) {\r
- return ((long[]) keyCache)[cacheIndex];\r
- }\r
- if (keyCacheBinding instanceof BooleanArrayBinding) {\r
- return ((boolean[]) keyCache)[cacheIndex] ? 1.0 : 0.0;\r
- }\r
- Object o = keyCacheBinding.get(keyCache, cacheIndex);\r
- Binding b = keyCacheBinding.getComponentBinding();\r
- if (b instanceof BooleanBinding)\r
- return ((BooleanBinding)b).getValue(o) ? 1.0 : 0.0;\r
- NumberBinding nb = (NumberBinding) b;\r
- return nb.getValue(o).doubleValue();\r
- } catch (IndexOutOfBoundsException e) {\r
- throw new AccessorException(e);\r
- } catch (BindingException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
- \r
- public double valueDouble() throws AccessorException {\r
- try {\r
- if (valueCacheBinding instanceof FloatArrayBinding) {\r
- return ((float[]) valueCache)[cacheIndex];\r
- }\r
- if (valueCacheBinding instanceof DoubleArrayBinding) {\r
- return ((double[]) valueCache)[cacheIndex];\r
- }\r
- if (valueCacheBinding instanceof ByteArrayBinding) {\r
- return ((byte[]) valueCache)[cacheIndex];\r
- }\r
- if (valueCacheBinding instanceof IntArrayBinding) {\r
- return ((int[]) valueCache)[cacheIndex];\r
- }\r
- if (valueCacheBinding instanceof LongArrayBinding) {\r
- return ((long[]) valueCache)[cacheIndex];\r
- }\r
- if (valueCacheBinding instanceof BooleanArrayBinding) {\r
- return ((boolean[]) valueCache)[cacheIndex] ? 1.0 : 0.0;\r
- }\r
- Object o = valueCacheBinding.get(valueCache, cacheIndex);\r
- Binding b = valueCacheBinding.getComponentBinding();\r
- if (b instanceof BooleanBinding)\r
- return ((BooleanBinding)b).getValue(o) ? 1.0 : 0.0;\r
- NumberBinding nb = (NumberBinding) b;\r
- return nb.getValue(o).doubleValue();\r
- } catch (IndexOutOfBoundsException e) {\r
- throw new AccessorException(e);\r
- } catch (BindingException e) {\r
- throw new AccessorException(e);\r
- }\r
- }\r
- \r
- public int index() {\r
- return index;\r
- }\r
-\r
-}\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
+ * 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.databoard.accessor.impl;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.accessor.MapAccessor;
+import org.simantics.databoard.accessor.error.AccessorException;
+import org.simantics.databoard.binding.ArrayBinding;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.BooleanBinding;
+import org.simantics.databoard.binding.NumberBinding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.impl.BooleanArrayBinding;
+import org.simantics.databoard.binding.impl.ByteArrayBinding;
+import org.simantics.databoard.binding.impl.DoubleArrayBinding;
+import org.simantics.databoard.binding.impl.FloatArrayBinding;
+import org.simantics.databoard.binding.impl.IntArrayBinding;
+import org.simantics.databoard.binding.impl.LongArrayBinding;
+import org.simantics.databoard.type.ArrayType;
+import org.simantics.databoard.util.Limit;
+import org.simantics.databoard.util.Range;
+
+/**
+ * This helper class creates an iterator to MapAccessor
+ *
+ * @param <K> key class
+ * @param <V> value class
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class MapAccessorIterator<K, V> {
+
+ MapAccessor map;
+ Binding keyBinding;
+ Binding valueBinding;
+
+ Object from;
+ boolean fromInclusive;
+
+ Object end;
+ boolean endInclusive;
+
+ ArrayBinding keyCacheBinding;
+ Object keyCache;
+
+ ArrayBinding valueCacheBinding;
+ Object valueCache;
+
+ // Entry index, i.e. the number of entries read starting from 0
+ int index = -1;
+
+ // The number of samples written to cache
+ int samplesInCache = 0;
+
+ // The index in cache
+ int cacheIndex = -1;
+
+ /**
+ * Initialize iterator with a cache. The cache should already be allocated with some empty elements.
+ * @param map
+ * @param keyBinding
+ * @param from
+ * @param fromInclusive
+ * @param end
+ * @param endInclusive
+ * @param keyCacheBinding
+ * @param keyCache
+ * @param valueCacheBinding
+ * @param valueCache
+ */
+ public MapAccessorIterator(
+ MapAccessor map,
+ Binding keyBinding, Object from, boolean fromInclusive, Object end, boolean endInclusive,
+ ArrayBinding keyCacheBinding, Object keyCache,
+ ArrayBinding valueCacheBinding, Object valueCache) {
+ this.map = map;
+ this.keyBinding = keyBinding;
+ this.from = from;
+ this.fromInclusive = fromInclusive;
+ this.end = end;
+ this.endInclusive = endInclusive;
+ this.valueBinding = valueCacheBinding.getComponentBinding();
+ this.keyCacheBinding = keyCacheBinding;
+ this.keyCache = keyCache;
+ this.valueCacheBinding = valueCacheBinding;
+ this.valueCache = valueCache;
+ }
+
+ /**
+ * Initialize map accessor iterator with a default cache
+ * @param map
+ * @param keyBinding
+ * @param from
+ * @param fromInclusive
+ * @param end
+ * @param endInclusive
+ * @param valueBinding
+ * @param cacheSize
+ * @throws BindingException
+ * @throws AccessorException
+ */
+ public MapAccessorIterator(
+ MapAccessor map,
+ Binding keyBinding, Object from, boolean fromInclusive, Object end, boolean endInclusive,
+ Binding valueBinding,
+ int cacheSize) throws BindingException, AccessorException {
+ this.map = map;
+ this.keyBinding = keyBinding;
+ this.from = from;
+ this.fromInclusive = fromInclusive;
+ this.end = end;
+ this.endInclusive = endInclusive;
+ this.valueBinding = valueBinding;
+
+ // TODO calc num of elements if source
+// int count = map.count(keyBinding, from, fromInclusive, end, endInclusive);
+// int len = Math.min(count, 256);
+ Range range = new Range(Limit.inclusive(cacheSize), Limit.nolimit());
+ this.keyCacheBinding = Bindings.getBinding( new ArrayType(keyBinding.type(), range) );
+ this.keyCache = keyCacheBinding.createDefault();
+ this.valueCacheBinding = Bindings.getBinding( new ArrayType(valueBinding.type(), range) );
+ this.valueCache = valueCacheBinding.createDefault();
+ }
+
+ /**
+ * Reads data to cache.
+ *
+ * Updates from and fromInclusive fields for next read.
+ * Updates samplesInCache field.
+ * Sets cacheIndex to 0.
+ *
+ * @throws AccessorException
+ */
+ private void fillCache() throws AccessorException {
+ try {
+ int limit = Math.min(keyCacheBinding.size(keyCache), valueCacheBinding.size(valueCache));
+ if (limit==0) throw new AccessorException("You should have some entries in the cache");
+
+ cacheIndex = -1;
+
+ if (from==null) {
+ samplesInCache = 0;
+ return;
+ }
+
+ samplesInCache = map.getEntries(
+ keyBinding, from, fromInclusive,
+ end, endInclusive,
+ keyCacheBinding, keyCache,
+ valueCacheBinding, valueCache,
+ limit);
+
+ if (samplesInCache==0) {
+ from = null;
+ return;
+ }
+
+ from = keyCacheBinding.get(keyCache, samplesInCache-1);
+ fromInclusive = false;
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ }
+ }
+
+ public boolean hasNext() throws AccessorException {
+ if (from==null) return false;
+ if (cacheIndex>=samplesInCache-1) fillCache();
+ if (cacheIndex>=samplesInCache-1) return false;
+ return cacheIndex+1<samplesInCache;
+ }
+
+ public boolean next() throws AccessorException {
+ if (from==null) return false;
+ if (cacheIndex>=samplesInCache-1) fillCache();
+ if (cacheIndex>=samplesInCache-1) return false;
+ cacheIndex++;
+ index++;
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ public K key() throws AccessorException {
+ try {
+ return (K) keyCacheBinding.get(keyCache, cacheIndex);
+ } catch (IndexOutOfBoundsException e) {
+ throw new AccessorException(e);
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public V value() throws AccessorException {
+ try {
+ return (V) valueCacheBinding.get(valueCache, cacheIndex);
+ } catch (IndexOutOfBoundsException e) {
+ throw new AccessorException(e);
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ }
+ }
+
+ public double keyDouble() throws AccessorException {
+ try {
+ if (keyCacheBinding instanceof FloatArrayBinding) {
+ return ((float[]) keyCache)[cacheIndex];
+ }
+ if (keyCacheBinding instanceof DoubleArrayBinding) {
+ return ((double[]) keyCache)[cacheIndex];
+ }
+ if (keyCacheBinding instanceof ByteArrayBinding) {
+ return ((byte[]) keyCache)[cacheIndex];
+ }
+ if (keyCacheBinding instanceof IntArrayBinding) {
+ return ((int[]) keyCache)[cacheIndex];
+ }
+ if (keyCacheBinding instanceof LongArrayBinding) {
+ return ((long[]) keyCache)[cacheIndex];
+ }
+ if (keyCacheBinding instanceof BooleanArrayBinding) {
+ return ((boolean[]) keyCache)[cacheIndex] ? 1.0 : 0.0;
+ }
+ Object o = keyCacheBinding.get(keyCache, cacheIndex);
+ Binding b = keyCacheBinding.getComponentBinding();
+ if (b instanceof BooleanBinding)
+ return ((BooleanBinding)b).getValue(o) ? 1.0 : 0.0;
+ NumberBinding nb = (NumberBinding) b;
+ return nb.getValue(o).doubleValue();
+ } catch (IndexOutOfBoundsException e) {
+ throw new AccessorException(e);
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ }
+ }
+
+ public double valueDouble() throws AccessorException {
+ try {
+ if (valueCacheBinding instanceof FloatArrayBinding) {
+ return ((float[]) valueCache)[cacheIndex];
+ }
+ if (valueCacheBinding instanceof DoubleArrayBinding) {
+ return ((double[]) valueCache)[cacheIndex];
+ }
+ if (valueCacheBinding instanceof ByteArrayBinding) {
+ return ((byte[]) valueCache)[cacheIndex];
+ }
+ if (valueCacheBinding instanceof IntArrayBinding) {
+ return ((int[]) valueCache)[cacheIndex];
+ }
+ if (valueCacheBinding instanceof LongArrayBinding) {
+ return ((long[]) valueCache)[cacheIndex];
+ }
+ if (valueCacheBinding instanceof BooleanArrayBinding) {
+ return ((boolean[]) valueCache)[cacheIndex] ? 1.0 : 0.0;
+ }
+ Object o = valueCacheBinding.get(valueCache, cacheIndex);
+ Binding b = valueCacheBinding.getComponentBinding();
+ if (b instanceof BooleanBinding)
+ return ((BooleanBinding)b).getValue(o) ? 1.0 : 0.0;
+ NumberBinding nb = (NumberBinding) b;
+ return nb.getValue(o).doubleValue();
+ } catch (IndexOutOfBoundsException e) {
+ throw new AccessorException(e);
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ }
+ }
+
+ public int index() {
+ return index;
+ }
+
+}
+