]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/PriorityQueueBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / PriorityQueueBinding.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/PriorityQueueBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/PriorityQueueBinding.java
new file mode 100644 (file)
index 0000000..66d3e07
--- /dev/null
@@ -0,0 +1,150 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  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.databoard.binding.impl;
+
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.PriorityQueue;\r
+\r
+import org.simantics.databoard.binding.ArrayBinding;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.ArrayType;\r
+
+/**
+ * PriorityQueueBindings binds ArrayType to java.util.PriorityQueue \r
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class PriorityQueueBinding extends ArrayBinding {
+\r
+       public PriorityQueueBinding(Binding componentBinding) {\r
+               this(new ArrayType(componentBinding.type()), componentBinding);\r
+       }\r
+       
+       public PriorityQueueBinding(ArrayType type, Binding componentBinding) {
+               super(type, componentBinding);
+               if (type==null) throw new IllegalArgumentException("null arg");
+               this.type = type;
+       }
+       
+       @Override
+       public Object create() {
+               return new PriorityQueue<Object>(11, componentBinding);
+       }
+       
+       @Override
+       public Object create(Collection<Object> collection ) throws BindingException {
+               PriorityQueue<Object> result = new PriorityQueue<Object>( collection.size(), componentBinding );\r
+               result.addAll( collection );\r
+               return result;
+       }
+       
+       /**
+        * Create new ArrayList
+        */
+       @Override
+       public Object create(int length, Iterator<Object> values) {
+               PriorityQueue<Object> result = new PriorityQueue<Object>( length, componentBinding );
+               while (values.hasNext())
+                       result.add(values.next());
+               return result;
+       }
+
+       public Object create(Object[] values) {
+               PriorityQueue<Object> result = new PriorityQueue<Object>(values.length, componentBinding );
+               for (int i=0; i<values.length; i++)
+                       result.add(values[i]);
+               return result;
+       }\r
+       \r
+       
+       @SuppressWarnings("unchecked")
+       @Override
+       public Object get(Object queue, int index) throws BindingException {
+               if (!isInstance(queue)) throw new BindingException("Unexpected class "+queue.getClass().getSimpleName()+", PriorityQueue expected");\r
+               PriorityQueue<Object> q = (PriorityQueue<Object>) queue; 
+               if ( index<0 || index>=q.size() ) throw new BindingException("Index out of bounds");
+               Iterator<Object> it = q.iterator();\r
+               Object result = null;\r
+               for (int i=0; i<=index; i++)\r
+               {\r
+                       result = it.next();\r
+               }\r
+               return result;
+       }\r
+       
+       @SuppressWarnings("unchecked")\r
+    @Override
+       public void getAll(Object queue, Object[] result) throws BindingException {
+               PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
+               int i=0;\r
+               for (Iterator<Object> it = q.iterator(); it.hasNext();)\r
+                       result[ i++ ] = it.next();\r
+       }
+       
+    @Override    
+       public void set(Object array, int index, Object value) throws BindingException {\r
+               remove(array, index, 1);\r
+               add(array, index, value);\r
+       }
+
+       @SuppressWarnings("unchecked")\r
+    @Override
+       public void add(Object queue, int index, Object element) throws BindingException, IndexOutOfBoundsException {
+               PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
+               q.add( element );\r
+       }
+       
+       @SuppressWarnings("unchecked")
+       @Override
+       public void remove(Object queue, int index, int count) throws BindingException {
+               PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
+               Iterator<Object> it = q.iterator();\r
+               if (index<0 || index>=q.size()) throw new BindingException("Index out of bounds");\r
+               for (int i=0; i<=index; i++) it.next();\r
+               it.remove();\r
+       }
+       
+       @SuppressWarnings("unchecked")
+       @Override
+       public int size(Object queue) throws BindingException {         
+               if (!isInstance(queue)) throw new BindingException("Unexpected class "+queue.getClass().getSimpleName()+", PriorityQueue expected");            
+               PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
+               return q.size();
+       }
+
+       @Override
+       public boolean isInstance(Object obj) {
+               return obj instanceof PriorityQueue<?>;
+       }\r
+\r
+       @Override\r
+       public void setSize(Object queue, int newSize) throws BindingException {\r
+               @SuppressWarnings("unchecked")\r
+               PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
+               int c = q.size() - newSize;\r
+               for (int i=0; i<c; i++) q.remove();\r
+       }               \r
+       \r
+       @Override\r
+       public boolean isImmutable() {\r
+               return false;\r
+       }
+\r
+       @Override\r
+       public boolean isResizable() {\r
+               return true;\r
+       }\r
+       
+}
+