1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.binding.impl;
14 import java.util.Collection;
\r
15 import java.util.Iterator;
\r
16 import java.util.PriorityQueue;
\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
24 * PriorityQueueBindings binds ArrayType to java.util.PriorityQueue
\r
26 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
28 public class PriorityQueueBinding extends ArrayBinding {
30 public PriorityQueueBinding(Binding componentBinding) {
\r
31 this(new ArrayType(componentBinding.type()), componentBinding);
\r
34 public PriorityQueueBinding(ArrayType type, Binding componentBinding) {
35 super(type, componentBinding);
36 if (type==null) throw new IllegalArgumentException("null arg");
41 public Object create() {
42 return new PriorityQueue<Object>(11, componentBinding);
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
53 * Create new ArrayList
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());
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]);
71 @SuppressWarnings("unchecked")
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
86 @SuppressWarnings("unchecked")
\r
88 public void getAll(Object queue, Object[] result) throws BindingException {
89 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;
\r
91 for (Iterator<Object> it = q.iterator(); it.hasNext();)
\r
92 result[ i++ ] = it.next();
\r
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
101 @SuppressWarnings("unchecked")
\r
103 public void add(Object queue, int index, Object element) throws BindingException, IndexOutOfBoundsException {
104 PriorityQueue<Object> q = (PriorityQueue<Object>) queue;
\r
108 @SuppressWarnings("unchecked")
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
118 @SuppressWarnings("unchecked")
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
127 public boolean isInstance(Object obj) {
128 return obj instanceof PriorityQueue<?>;
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
140 public boolean isImmutable() {
\r
145 public boolean isResizable() {
\r