]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.binding.impl;
13
14 import java.util.Collection;\r
15 import java.util.Iterator;\r
16 import java.util.PriorityQueue;\r
17 \r
18 import org.simantics.databoard.binding.ArrayBinding;\r
19 import org.simantics.databoard.binding.Binding;\r
20 import org.simantics.databoard.binding.error.BindingException;\r
21 import org.simantics.databoard.type.ArrayType;\r
22
23 /**
24  * PriorityQueueBindings binds ArrayType to java.util.PriorityQueue \r
25  *
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public class PriorityQueueBinding extends ArrayBinding {
29 \r
30         public PriorityQueueBinding(Binding componentBinding) {\r
31                 this(new ArrayType(componentBinding.type()), componentBinding);\r
32         }\r
33         
34         public PriorityQueueBinding(ArrayType type, Binding componentBinding) {
35                 super(type, componentBinding);
36                 if (type==null) throw new IllegalArgumentException("null arg");
37                 this.type = type;
38         }
39         
40         @Override
41         public Object create() {
42                 return new PriorityQueue<Object>(11, componentBinding);
43         }
44         
45         @Override
46         public Object create(Collection<Object> collection ) throws BindingException {
47                 PriorityQueue<Object> result = new PriorityQueue<Object>( collection.size(), componentBinding );\r
48                 result.addAll( collection );\r
49                 return result;
50         }
51         
52         /**
53          * Create new ArrayList
54          */
55         @Override
56         public Object create(int length, Iterator<Object> values) {
57                 PriorityQueue<Object> result = new PriorityQueue<Object>( length, componentBinding );
58                 while (values.hasNext())
59                         result.add(values.next());
60                 return result;
61         }
62
63         public Object create(Object[] values) {
64                 PriorityQueue<Object> result = new PriorityQueue<Object>(values.length, componentBinding );
65                 for (int i=0; i<values.length; i++)
66                         result.add(values[i]);
67                 return result;
68         }\r
69         \r
70         
71         @SuppressWarnings("unchecked")
72         @Override
73         public Object get(Object queue, int index) throws BindingException {
74                 if (!isInstance(queue)) throw new BindingException("Unexpected class "+queue.getClass().getSimpleName()+", PriorityQueue expected");\r
75                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue; 
76                 if ( index<0 || index>=q.size() ) throw new BindingException("Index out of bounds");
77                 Iterator<Object> it = q.iterator();\r
78                 Object result = null;\r
79                 for (int i=0; i<=index; i++)\r
80                 {\r
81                         result = it.next();\r
82                 }\r
83                 return result;
84         }\r
85         
86         @SuppressWarnings("unchecked")\r
87     @Override
88         public void getAll(Object queue, Object[] result) throws BindingException {
89                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
90                 int i=0;\r
91                 for (Iterator<Object> it = q.iterator(); it.hasNext();)\r
92                         result[ i++ ] = it.next();\r
93         }
94         
95     @Override    
96         public void set(Object array, int index, Object value) throws BindingException {\r
97                 remove(array, index, 1);\r
98                 add(array, index, value);\r
99         }
100
101         @SuppressWarnings("unchecked")\r
102     @Override
103         public void add(Object queue, int index, Object element) throws BindingException, IndexOutOfBoundsException {
104                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
105                 q.add( element );\r
106         }
107         
108         @SuppressWarnings("unchecked")
109         @Override
110         public void remove(Object queue, int index, int count) throws BindingException {
111                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
112                 Iterator<Object> it = q.iterator();\r
113                 if (index<0 || index>=q.size()) throw new BindingException("Index out of bounds");\r
114                 for (int i=0; i<=index; i++) it.next();\r
115                 it.remove();\r
116         }
117         
118         @SuppressWarnings("unchecked")
119         @Override
120         public int size(Object queue) throws BindingException {         
121                 if (!isInstance(queue)) throw new BindingException("Unexpected class "+queue.getClass().getSimpleName()+", PriorityQueue expected");            
122                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
123                 return q.size();
124         }
125
126         @Override
127         public boolean isInstance(Object obj) {
128                 return obj instanceof PriorityQueue<?>;
129         }\r
130 \r
131         @Override\r
132         public void setSize(Object queue, int newSize) throws BindingException {\r
133                 @SuppressWarnings("unchecked")\r
134                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;\r
135                 int c = q.size() - newSize;\r
136                 for (int i=0; i<c; i++) q.remove();\r
137         }               \r
138         \r
139         @Override\r
140         public boolean isImmutable() {\r
141                 return false;\r
142         }
143 \r
144         @Override\r
145         public boolean isResizable() {\r
146                 return true;\r
147         }\r
148         
149 }
150