]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ValueTableSmall.java
Use java.util.Consumer instead of os.utils.datastructures.Callback
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ValueTableSmall.java
index 59eca5c8c97fb42a47b7ee02ec1152f685783f0b..6b57fb9b42c32fd2aef3fa8e5083128e3ff4d36b 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\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.db.procore.cluster;\r
-\r
-import java.util.Arrays;\r
-import java.util.Map;\r
-import java.util.TreeMap;\r
-\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ValidationException;\r
-import org.simantics.db.impl.ClusterI.ObjectProcedure;\r
-import org.simantics.db.impl.ClusterI.Procedure;\r
-import org.simantics.db.impl.ClusterSupport;\r
-import org.simantics.db.impl.Modifier;\r
-import org.simantics.db.impl.Table;\r
-import org.simantics.db.impl.TableFactory;\r
-import org.simantics.db.impl.TableSizeListener;\r
-\r
-public final class ValueTableSmall extends Table<byte[]> {\r
-    private static final boolean DEBUG = ClusterImpl.DEBUG;\r
-    public ValueTableSmall(TableSizeListener sizeListener, int[] header, int headerBase) {\r
-        super(TableFactory.getByteFactory(), sizeListener, header, headerBase);\r
-    }\r
-    public ValueTableSmall(TableSizeListener sizeListener, int[] header, int headerBase, byte[] bytes) {\r
-        super(TableFactory.getByteFactory(), sizeListener, header, headerBase, bytes);\r
-    }\r
-    private static final class IndexAndSize {\r
-       int index; // Real index (zero based) to start of value data.\r
-       short size; // Size of value data.\r
-    }\r
-    private IndexAndSize checkIndexAndGetRealIndex(int index) {\r
-       if (index < ZERO_SHIFT) // Index starts with ZERO_SHIFT.\r
-            throw new IllegalArgumentException("Underflow, index=" + index);\r
-       int tableSize = getTableSize();\r
-       if (index >= tableSize) // Element size is at least two (one byte length and one byte data).\r
-            throw new IllegalArgumentException("Overflow, index=" + index);\r
-       int i = index + offset;\r
-       byte[] table = getTable();\r
-       IndexAndSize is = new IndexAndSize();\r
-       is.size = table[i++]; \r
-       if (is.size == 0)\r
-               throw new IllegalArgumentException("Removed, index=" + index);\r
-       if (is.size < 0) // two byte size\r
-               is.size = (short)(((is.size & 0x7F) << 8) | (table[i++] & 0xFF));\r
-       is.index = i;\r
-       if (is.index + is.size > tableSize) // Element size too big.\r
-               throw new IllegalArgumentException("Illegal size, index=" + is.index + " size=" + is.size);\r
-       return is;\r
-    }\r
-    public byte[] getValue(int valueIndex) {\r
-       IndexAndSize is = checkIndexAndGetRealIndex(valueIndex);\r
-       byte[] value = new byte[is.size];\r
-        System.arraycopy(table, is.index, value, 0, is.size);\r
-        if (DEBUG)\r
-            System.out.println("ValueTableSmall.getValue " + valueIndex + " " + Arrays.toString(value));\r
-       return value;\r
-    }\r
-    char[] getString(int valueIndex) {\r
-        IndexAndSize is = checkIndexAndGetRealIndex(valueIndex);\r
-        final int size = is.size-1;\r
-        char[] t = new char[size];\r
-        int start = is.index;\r
-        for(int i=0; i<size; i++, ++start)\r
-            t[i] = (char)table[start];\r
-        return t;\r
-    }\r
-    int setValue(int valueIndex, byte[] value, int offset, int length) {\r
-       if (valueIndex != 0) // Modify old value.\r
-               removeValue(valueIndex);\r
-       return createValue(value, offset, length); \r
-    }\r
-    int createValue(byte[] value, int voffset, int vsize) {\r
-       if (vsize < 1 || vsize > (1<<15)-1)\r
-               throw new IllegalArgumentException("Illegal internal value size=" + vsize + ".");\r
-       int size = vsize;\r
-       if (vsize > 0x7F)\r
-               size += 2;\r
-       else\r
-               size += 1;\r
-        int valueIndex = createNewElement(size);\r
-        int i = checkIndexAndGetRealIndex(valueIndex, 1);\r
-       if (vsize > 0x7F) {\r
-               table[i++] = (byte)((vsize >>> 8) | 1<<7); // msb\r
-               table[i++] = (byte)(vsize & 0xFF); // lsb\r
-       } else\r
-               table[i++] = (byte)vsize;\r
-       System.arraycopy(value, voffset, table, i, vsize);\r
-        return valueIndex;\r
-    }\r
-    void removeValue(int valueIndex) {\r
-       IndexAndSize is = checkIndexAndGetRealIndex(valueIndex);\r
-       int length = is.size;\r
-       int index = is.index;\r
-       if (is.size > 0x7F) {\r
-               length += 2;\r
-               index -= 2;\r
-               table[index+1] = 0;\r
-       } else {\r
-               length += 1;\r
-               index -= 1;\r
-       }\r
-       table[index] = 0;\r
-       deleteOldElement(valueIndex, length);\r
-    }\r
-//    boolean isEqual(int valueIndex, byte[] value, int voffset, int vsize) {\r
-//        return isEqual(valueIndex, value, 0, value.length);\r
-//    }\r
-    private TreeMap<Integer, Integer> valueMap =\r
-        new TreeMap<Integer, Integer>();\r
-    \r
-    private int VALUE_SIZE = 0;\r
-    private int VALUE_OFFSET = 0;\r
-    public void checkValueInit()\r
-    throws DatabaseException {\r
-        valueMap.clear();\r
-        final int s = getTableSize();\r
-        final int c = getTableCapacity();\r
-        if (s < 0 || s > c)\r
-            throw new ValidationException("Illegal value table size=" + s + " cap=" + c);\r
-        VALUE_SIZE = s;\r
-        VALUE_OFFSET = getTableBase() - ValueTableSmall.ZERO_SHIFT; \r
-    }\r
-    public void checkValue(int capacity, int index)\r
-    throws DatabaseException {\r
-        if (0 == capacity && 0 == index)\r
-            return;\r
-        if (capacity < 1)\r
-            throw new ValidationException("Illegal resource value capacity=" + capacity);\r
-        if (index < 1)\r
-            throw new ValidationException("Illegal resource value index=" + index);\r
-        if (VALUE_SIZE < capacity + index + VALUE_OFFSET)\r
-            throw new ValidationException("Illegal resource value c=" + capacity +\r
-                    " i=" + index + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);\r
-        // Duplicate index is allowed because new index is created only if new size is greater than old.\r
-        Integer valueCap = valueMap.get(index);\r
-        if (null == valueCap)\r
-            valueMap.put(index, capacity);\r
-        else if (capacity > valueCap)\r
-            valueMap.put(index, capacity);\r
-        else\r
-            valueMap.put(index, valueCap);\r
-    }\r
-    public void checkValueFini()\r
-    throws DatabaseException {\r
-        int last = 0;\r
-        for (Map.Entry<Integer, Integer> e : valueMap.entrySet()) {\r
-            int i = e.getKey();\r
-            int c = e.getValue();\r
-            int cur = VALUE_OFFSET + i;\r
-            if (last > cur)\r
-                throw new ValidationException("Index error with resource value c=" + c +\r
-                        " i=" + i + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);\r
-            last = cur + c;\r
-        }\r
-    }\r
-    @Override\r
-    public <Context> boolean foreach(int setIndex, Procedure procedure, Context context, ClusterSupport support, Modifier modifier) throws DatabaseException {\r
-        throw new UnsupportedOperationException();\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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.db.procore.cluster;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ValidationException;
+import org.simantics.db.impl.ClusterI.Procedure;
+import org.simantics.db.impl.ClusterSupport;
+import org.simantics.db.impl.Modifier;
+import org.simantics.db.impl.Table;
+import org.simantics.db.impl.TableFactory;
+import org.simantics.db.impl.TableSizeListener;
+
+public final class ValueTableSmall extends Table<byte[]> {
+    private static final boolean DEBUG = ClusterImpl.DEBUG;
+    public ValueTableSmall(TableSizeListener sizeListener, int[] header, int headerBase) {
+        super(TableFactory.getByteFactory(), sizeListener, header, headerBase);
+    }
+    public ValueTableSmall(TableSizeListener sizeListener, int[] header, int headerBase, byte[] bytes) {
+        super(TableFactory.getByteFactory(), sizeListener, header, headerBase, bytes);
+    }
+    private static final class IndexAndSize {
+       int index; // Real index (zero based) to start of value data.
+       short size; // Size of value data.
+    }
+    private IndexAndSize checkIndexAndGetRealIndex(int index) {
+       if (index < ZERO_SHIFT) // Index starts with ZERO_SHIFT.
+            throw new IllegalArgumentException("Underflow, index=" + index);
+       int tableSize = getTableSize();
+       if (index >= tableSize) // Element size is at least two (one byte length and one byte data).
+            throw new IllegalArgumentException("Overflow, index=" + index);
+       int i = index + offset;
+       byte[] table = getTable();
+       IndexAndSize is = new IndexAndSize();
+       is.size = table[i++]; 
+       if (is.size == 0)
+               throw new IllegalArgumentException("Removed, index=" + index);
+       if (is.size < 0) // two byte size
+               is.size = (short)(((is.size & 0x7F) << 8) | (table[i++] & 0xFF));
+       is.index = i;
+       if (is.index + is.size > tableSize) // Element size too big.
+               throw new IllegalArgumentException("Illegal size, index=" + is.index + " size=" + is.size);
+       return is;
+    }
+    public byte[] getValue(int valueIndex) {
+       IndexAndSize is = checkIndexAndGetRealIndex(valueIndex);
+       byte[] value = new byte[is.size];
+        System.arraycopy(table, is.index, value, 0, is.size);
+        if (DEBUG)
+            System.out.println("ValueTableSmall.getValue " + valueIndex + " " + Arrays.toString(value));
+       return value;
+    }
+    char[] getString(int valueIndex) {
+        IndexAndSize is = checkIndexAndGetRealIndex(valueIndex);
+        final int size = is.size-1;
+        char[] t = new char[size];
+        int start = is.index;
+        for(int i=0; i<size; i++, ++start)
+            t[i] = (char)table[start];
+        return t;
+    }
+    int setValue(int valueIndex, byte[] value, int offset, int length) {
+       if (valueIndex != 0) // Modify old value.
+               removeValue(valueIndex);
+       return createValue(value, offset, length); 
+    }
+    int createValue(byte[] value, int voffset, int vsize) {
+       if (vsize < 1 || vsize > (1<<15)-1)
+               throw new IllegalArgumentException("Illegal internal value size=" + vsize + ".");
+       int size = vsize;
+       if (vsize > 0x7F)
+               size += 2;
+       else
+               size += 1;
+        int valueIndex = createNewElement(size);
+        int i = checkIndexAndGetRealIndex(valueIndex, 1);
+       if (vsize > 0x7F) {
+               table[i++] = (byte)((vsize >>> 8) | 1<<7); // msb
+               table[i++] = (byte)(vsize & 0xFF); // lsb
+       } else
+               table[i++] = (byte)vsize;
+       System.arraycopy(value, voffset, table, i, vsize);
+        return valueIndex;
+    }
+    void removeValue(int valueIndex) {
+       IndexAndSize is = checkIndexAndGetRealIndex(valueIndex);
+       int length = is.size;
+       int index = is.index;
+       if (is.size > 0x7F) {
+               length += 2;
+               index -= 2;
+               table[index+1] = 0;
+       } else {
+               length += 1;
+               index -= 1;
+       }
+       table[index] = 0;
+       deleteOldElement(valueIndex, length);
+    }
+//    boolean isEqual(int valueIndex, byte[] value, int voffset, int vsize) {
+//        return isEqual(valueIndex, value, 0, value.length);
+//    }
+    private TreeMap<Integer, Integer> valueMap =
+        new TreeMap<Integer, Integer>();
+    
+    private int VALUE_SIZE = 0;
+    private int VALUE_OFFSET = 0;
+    public void checkValueInit()
+    throws DatabaseException {
+        valueMap.clear();
+        final int s = getTableSize();
+        final int c = getTableCapacity();
+        if (s < 0 || s > c)
+            throw new ValidationException("Illegal value table size=" + s + " cap=" + c);
+        VALUE_SIZE = s;
+        VALUE_OFFSET = getTableBase() - ValueTableSmall.ZERO_SHIFT; 
+    }
+    public void checkValue(int capacity, int index)
+    throws DatabaseException {
+        if (0 == capacity && 0 == index)
+            return;
+        if (capacity < 1)
+            throw new ValidationException("Illegal resource value capacity=" + capacity);
+        if (index < 1)
+            throw new ValidationException("Illegal resource value index=" + index);
+        if (VALUE_SIZE < capacity + index + VALUE_OFFSET)
+            throw new ValidationException("Illegal resource value c=" + capacity +
+                    " i=" + index + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);
+        // Duplicate index is allowed because new index is created only if new size is greater than old.
+        Integer valueCap = valueMap.get(index);
+        if (null == valueCap)
+            valueMap.put(index, capacity);
+        else if (capacity > valueCap)
+            valueMap.put(index, capacity);
+        else
+            valueMap.put(index, valueCap);
+    }
+    public void checkValueFini()
+    throws DatabaseException {
+        int last = 0;
+        for (Map.Entry<Integer, Integer> e : valueMap.entrySet()) {
+            int i = e.getKey();
+            int c = e.getValue();
+            int cur = VALUE_OFFSET + i;
+            if (last > cur)
+                throw new ValidationException("Index error with resource value c=" + c +
+                        " i=" + i + " ts=" + VALUE_SIZE + " off=" + VALUE_OFFSET);
+            last = cur + c;
+        }
+    }
+    @Override
+    public <Context> boolean foreach(int setIndex, Procedure procedure, Context context, ClusterSupport support, Modifier modifier) throws DatabaseException {
+        throw new UnsupportedOperationException();
+    }
+
+}