X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.history%2Fsrc%2Forg%2Fsimantics%2Fhistory%2Futil%2FMedianBinding.java;h=a447ce3cfce4e0ea6432565372ba9b50790c613a;hb=e3940e67813aa0988bdd0fe6161500034e6e7518;hp=b377600bd42bb7719c777ad270635abc1212b93b;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.history/src/org/simantics/history/util/MedianBinding.java b/bundles/org.simantics.history/src/org/simantics/history/util/MedianBinding.java index b377600bd..a447ce3cf 100644 --- a/bundles/org.simantics.history/src/org/simantics/history/util/MedianBinding.java +++ b/bundles/org.simantics.history/src/org/simantics/history/util/MedianBinding.java @@ -1,173 +1,173 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.history.util; - -import java.util.Iterator; - -import org.simantics.databoard.binding.ArrayBinding; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.type.ArrayType; - -/** - * This class binds median class to as an array type - * - */ -public class MedianBinding extends ArrayBinding { - - public MedianBinding(Binding componentBinding) { - this(new ArrayType(componentBinding.type()), componentBinding); - } - - public MedianBinding(ArrayType type, Binding componentBinding) { - super(type, componentBinding); - if (type==null) throw new IllegalArgumentException("null arg"); - this.type = type; - } - - @Override - public Object create() { - return new Median( componentBinding ); - } - - @Override - public Object create(int length, Iterator it) throws BindingException { - Median result = new Median( length, componentBinding ); - while (it.hasNext()) result.add(it.next()); - return result; - } - - @Override - public Object create(Object[] array) throws BindingException { - Median result = new Median( array.length, componentBinding ); - for (int i=0; i median = (Median) array; - median.add(element); - } - - @Override - public void remove(Object array, int index, int count) throws BindingException { - if (index<0 || index+count>=size(array)) - throw new BindingException("Indx out of bounds"); - - @SuppressWarnings("unchecked") - Median median = (Median) array; - - if (index it = median.lower.iterator(); - for ( int i=0; i<=index; i++ ) it.next(); - it.remove(); - return; - } - index -= median.lower.size(); - if (median.median != null) index--; - if (index==-1) { - if (median.upper.size() >= median.lower.size()) { - median.median = median.upper.remove(); - } else { - median.median = median.lower.remove(); - } - return; - } - if (index it = median.upper.iterator(); - for ( int i=0; i<=index; i++ ) it.next(); - it.remove(); - return; - } - - } - - @Override - public Object get(Object array, int index) throws BindingException { - @SuppressWarnings("unchecked") - Median median = (Median) array; - if (index<0) throw new BindingException("Index out of bounds"); - if (index it = median.lower.iterator(); - for ( int i=0; i<=index; i++ ) result = it.next(); - return result; - } - index -= median.lower.size(); - if (median.median != null) index--; - if (index==-1) { - return median.median; - } - if (index it = median.upper.iterator(); - for ( int i=0; i<=index; i++ ) result = it.next(); - return result; - } - return null; - } - - @Override - public void getAll(Object array, Object[] result) throws BindingException { - int c = size( array ); - if (result.length median = (Median) array; - - Iterator it = median.lower.iterator(); - while (it.hasNext()) result[index++] = it.next(); - if (median.getMedian() != null) result[index++] = median.getMedian(); - it = median.upper.iterator(); - while (it.hasNext()) result[index++] = it.next(); - } - - @Override - public void set(Object array, int index, Object value) throws BindingException { - remove(array, index); - add(array, value); - } - - @Override - public void setSize(Object array, int newSize) throws BindingException { - int size = size(array); - if (size median = (Median) array; - int count = 0; - if (median.median != null) count++; - count += median.lower.size(); - count += median.upper.size(); - return count; - } - - @Override - public boolean isInstance(Object obj) { - return obj instanceof Median; - } - - @Override - public boolean isResizable() { - return true; - } - -} - - +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.history.util; + +import java.util.Iterator; + +import org.simantics.databoard.binding.ArrayBinding; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.type.ArrayType; + +/** + * This class binds median class to as an array type + * + */ +public class MedianBinding extends ArrayBinding { + + public MedianBinding(Binding componentBinding) { + this(new ArrayType(componentBinding.type()), componentBinding); + } + + public MedianBinding(ArrayType type, Binding componentBinding) { + super(type, componentBinding); + if (type==null) throw new IllegalArgumentException("null arg"); + this.type = type; + } + + @Override + public Object create() { + return new Median( componentBinding ); + } + + @Override + public Object create(int length, Iterator it) throws BindingException { + Median result = new Median( length, componentBinding ); + while (it.hasNext()) result.add(it.next()); + return result; + } + + @Override + public Object create(Object[] array) throws BindingException { + Median result = new Median( array.length, componentBinding ); + for (int i=0; i median = (Median) array; + median.add(element); + } + + @Override + public void remove(Object array, int index, int count) throws BindingException { + if (index<0 || index+count>=size(array)) + throw new BindingException("Indx out of bounds"); + + @SuppressWarnings("unchecked") + Median median = (Median) array; + + if (index it = median.lower.iterator(); + for ( int i=0; i<=index; i++ ) it.next(); + it.remove(); + return; + } + index -= median.lower.size(); + if (median.median != null) index--; + if (index==-1) { + if (median.upper.size() >= median.lower.size()) { + median.median = median.upper.remove(); + } else { + median.median = median.lower.remove(); + } + return; + } + if (index it = median.upper.iterator(); + for ( int i=0; i<=index; i++ ) it.next(); + it.remove(); + return; + } + + } + + @Override + public Object get(Object array, int index) throws BindingException { + @SuppressWarnings("unchecked") + Median median = (Median) array; + if (index<0) throw new BindingException("Index out of bounds"); + if (index it = median.lower.iterator(); + for ( int i=0; i<=index; i++ ) result = it.next(); + return result; + } + index -= median.lower.size(); + if (median.median != null) index--; + if (index==-1) { + return median.median; + } + if (index it = median.upper.iterator(); + for ( int i=0; i<=index; i++ ) result = it.next(); + return result; + } + return null; + } + + @Override + public void getAll(Object array, Object[] result) throws BindingException { + int c = size( array ); + if (result.length median = (Median) array; + + Iterator it = median.lower.iterator(); + while (it.hasNext()) result[index++] = it.next(); + if (median.getMedian() != null) result[index++] = median.getMedian(); + it = median.upper.iterator(); + while (it.hasNext()) result[index++] = it.next(); + } + + @Override + public void set(Object array, int index, Object value) throws BindingException { + remove(array, index); + add(array, value); + } + + @Override + public void setSize(Object array, int newSize) throws BindingException { + int size = size(array); + if (size median = (Median) array; + int count = 0; + if (median.median != null) count++; + count += median.lower.size(); + count += median.upper.size(); + return count; + } + + @Override + public boolean isInstance(Object obj) { + return obj instanceof Median; + } + + @Override + public boolean isResizable() { + return true; + } + +} + +