]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/PriorityQueueBinding.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / PriorityQueueBinding.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.impl;
13
14 import java.util.Collection;
15 import java.util.Iterator;
16 import java.util.PriorityQueue;
17
18 import org.simantics.databoard.binding.ArrayBinding;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.databoard.binding.error.BindingException;
21 import org.simantics.databoard.type.ArrayType;
22
23 /**
24  * PriorityQueueBindings binds ArrayType to java.util.PriorityQueue 
25  *
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public class PriorityQueueBinding extends ArrayBinding {
29
30         public PriorityQueueBinding(Binding componentBinding) {
31                 this(new ArrayType(componentBinding.type()), componentBinding);
32         }
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 );
48                 result.addAll( collection );
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         }
69         
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");
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();
78                 Object result = null;
79                 for (int i=0; i<=index; i++)
80                 {
81                         result = it.next();
82                 }
83                 return result;
84         }
85         
86         @SuppressWarnings("unchecked")
87     @Override
88         public void getAll(Object queue, Object[] result) throws BindingException {
89                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;
90                 int i=0;
91                 for (Iterator<Object> it = q.iterator(); it.hasNext();)
92                         result[ i++ ] = it.next();
93         }
94         
95     @Override    
96         public void set(Object array, int index, Object value) throws BindingException {
97                 remove(array, index, 1);
98                 add(array, index, value);
99         }
100
101         @SuppressWarnings("unchecked")
102     @Override
103         public void add(Object queue, int index, Object element) throws BindingException, IndexOutOfBoundsException {
104                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;
105                 q.add( element );
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;
112                 Iterator<Object> it = q.iterator();
113                 if (index<0 || index>=q.size()) throw new BindingException("Index out of bounds");
114                 for (int i=0; i<=index; i++) it.next();
115                 it.remove();
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;
123                 return q.size();
124         }
125
126         @Override
127         public boolean isInstance(Object obj) {
128                 return obj instanceof PriorityQueue<?>;
129         }
130
131         @Override
132         public void setSize(Object queue, int newSize) throws BindingException {
133                 @SuppressWarnings("unchecked")
134                 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;
135                 int c = q.size() - newSize;
136                 for (int i=0; i<c; i++) q.remove();
137         }               
138         
139         @Override
140         public boolean isImmutable() {
141                 return false;
142         }
143
144         @Override
145         public boolean isResizable() {
146                 return true;
147         }
148         
149 }
150