1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 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.history.util;
14 import java.util.Iterator;
16 import org.simantics.databoard.binding.ArrayBinding;
17 import org.simantics.databoard.binding.Binding;
18 import org.simantics.databoard.binding.error.BindingException;
19 import org.simantics.databoard.type.ArrayType;
22 * This class binds median class to as an array type
25 public class MedianBinding extends ArrayBinding {
27 public MedianBinding(Binding componentBinding) {
28 this(new ArrayType(componentBinding.type()), componentBinding);
31 public MedianBinding(ArrayType type, Binding componentBinding) {
32 super(type, componentBinding);
33 if (type==null) throw new IllegalArgumentException("null arg");
38 public Object create() {
39 return new Median<Object>( componentBinding );
43 public Object create(int length, Iterator<Object> it) throws BindingException {
44 Median<Object> result = new Median<Object>( length, componentBinding );
45 while (it.hasNext()) result.add(it.next());
50 public Object create(Object[] array) throws BindingException {
51 Median<Object> result = new Median<Object>( array.length, componentBinding );
52 for (int i=0; i<array.length; i++) result.add( array[i] );
57 public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException {
58 @SuppressWarnings("unchecked")
59 Median<Object> median = (Median<Object>) array;
64 public void remove(Object array, int index, int count) throws BindingException {
65 if (index<0 || index+count>=size(array))
66 throw new BindingException("Indx out of bounds");
68 @SuppressWarnings("unchecked")
69 Median<Object> median = (Median<Object>) array;
71 if (index<median.lower.size()) {
72 Iterator<Object> it = median.lower.iterator();
73 for ( int i=0; i<=index; i++ ) it.next();
77 index -= median.lower.size();
78 if (median.median != null) index--;
80 if (median.upper.size() >= median.lower.size()) {
81 median.median = median.upper.remove();
83 median.median = median.lower.remove();
87 if (index<median.upper.size()) {
88 Iterator<Object> it = median.upper.iterator();
89 for ( int i=0; i<=index; i++ ) it.next();
97 public Object get(Object array, int index) throws BindingException {
98 @SuppressWarnings("unchecked")
99 Median<Object> median = (Median<Object>) array;
100 if (index<0) throw new BindingException("Index out of bounds");
101 if (index<median.lower.size()) {
102 Object result = null;
103 Iterator<Object> it = median.lower.iterator();
104 for ( int i=0; i<=index; i++ ) result = it.next();
107 index -= median.lower.size();
108 if (median.median != null) index--;
110 return median.median;
112 if (index<median.upper.size()) {
113 Object result = null;
114 Iterator<Object> it = median.upper.iterator();
115 for ( int i=0; i<=index; i++ ) result = it.next();
122 public void getAll(Object array, Object[] result) throws BindingException {
123 int c = size( array );
124 if (result.length<c) throw new BindingException("Array too small");
127 @SuppressWarnings("unchecked")
128 Median<Object> median = (Median<Object>) array;
130 Iterator<Object> it = median.lower.iterator();
131 while (it.hasNext()) result[index++] = it.next();
132 if (median.getMedian() != null) result[index++] = median.getMedian();
133 it = median.upper.iterator();
134 while (it.hasNext()) result[index++] = it.next();
138 public void set(Object array, int index, Object value) throws BindingException {
139 remove(array, index);
144 public void setSize(Object array, int newSize) throws BindingException {
145 int size = size(array);
146 if (size<newSize) newSize = size;
147 remove( array, size-newSize, newSize);
151 public int size(Object array) throws BindingException {
152 @SuppressWarnings("unchecked")
153 Median<Object> median = (Median<Object>) array;
155 if (median.median != null) count++;
156 count += median.lower.size();
157 count += median.upper.size();
162 public boolean isInstance(Object obj) {
163 return obj instanceof Median;
167 public boolean isResizable() {