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