-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.history.util;\r
-\r
-import java.util.Iterator;\r
-\r
-import org.simantics.databoard.binding.ArrayBinding;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.type.ArrayType;\r
-\r
-/**\r
- * This class binds median class to as an array type\r
- * \r
- */\r
-public class MedianBinding extends ArrayBinding {\r
-\r
- public MedianBinding(Binding componentBinding) {\r
- this(new ArrayType(componentBinding.type()), componentBinding);\r
- }\r
- \r
- public MedianBinding(ArrayType type, Binding componentBinding) {\r
- super(type, componentBinding);\r
- if (type==null) throw new IllegalArgumentException("null arg");\r
- this.type = type;\r
- }\r
- \r
- @Override\r
- public Object create() {\r
- return new Median<Object>( componentBinding );\r
- }\r
-\r
- @Override\r
- public Object create(int length, Iterator<Object> it) throws BindingException {\r
- Median<Object> result = new Median<Object>( length, componentBinding );\r
- while (it.hasNext()) result.add(it.next()); \r
- return result;\r
- }\r
-\r
- @Override\r
- public Object create(Object[] array) throws BindingException {\r
- Median<Object> result = new Median<Object>( array.length, componentBinding );\r
- for (int i=0; i<array.length; i++) result.add( array[i] );\r
- return result;\r
- }\r
-\r
- @Override\r
- public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException {\r
- @SuppressWarnings("unchecked")\r
- Median<Object> median = (Median<Object>) array;\r
- median.add(element);\r
- }\r
-\r
- @Override\r
- public void remove(Object array, int index, int count) throws BindingException {\r
- if (index<0 || index+count>=size(array)) \r
- throw new BindingException("Indx out of bounds");\r
- \r
- @SuppressWarnings("unchecked")\r
- Median<Object> median = (Median<Object>) array;\r
- \r
- if (index<median.lower.size()) {\r
- Iterator<Object> it = median.lower.iterator();\r
- for ( int i=0; i<=index; i++ ) it.next();\r
- it.remove();\r
- return;\r
- }\r
- index -= median.lower.size();\r
- if (median.median != null) index--;\r
- if (index==-1) {\r
- if (median.upper.size() >= median.lower.size()) {\r
- median.median = median.upper.remove();\r
- } else {\r
- median.median = median.lower.remove();\r
- } \r
- return;\r
- }\r
- if (index<median.upper.size()) {\r
- Iterator<Object> it = median.upper.iterator();\r
- for ( int i=0; i<=index; i++ ) it.next();\r
- it.remove();\r
- return;\r
- } \r
- \r
- }\r
-\r
- @Override\r
- public Object get(Object array, int index) throws BindingException {\r
- @SuppressWarnings("unchecked")\r
- Median<Object> median = (Median<Object>) array;\r
- if (index<0) throw new BindingException("Index out of bounds");\r
- if (index<median.lower.size()) {\r
- Object result = null;\r
- Iterator<Object> it = median.lower.iterator();\r
- for ( int i=0; i<=index; i++ ) result = it.next();\r
- return result;\r
- }\r
- index -= median.lower.size();\r
- if (median.median != null) index--;\r
- if (index==-1) {\r
- return median.median;\r
- }\r
- if (index<median.upper.size()) {\r
- Object result = null;\r
- Iterator<Object> it = median.upper.iterator();\r
- for ( int i=0; i<=index; i++ ) result = it.next();\r
- return result;\r
- } \r
- return null;\r
- }\r
-\r
- @Override\r
- public void getAll(Object array, Object[] result) throws BindingException {\r
- int c = size( array );\r
- if (result.length<c) throw new BindingException("Array too small");\r
-\r
- int index = 0;\r
- @SuppressWarnings("unchecked")\r
- Median<Object> median = (Median<Object>) array;\r
-\r
- Iterator<Object> it = median.lower.iterator();\r
- while (it.hasNext()) result[index++] = it.next();\r
- if (median.getMedian() != null) result[index++] = median.getMedian();\r
- it = median.upper.iterator();\r
- while (it.hasNext()) result[index++] = it.next();\r
- }\r
-\r
- @Override\r
- public void set(Object array, int index, Object value) throws BindingException {\r
- remove(array, index);\r
- add(array, value);\r
- }\r
-\r
- @Override\r
- public void setSize(Object array, int newSize) throws BindingException {\r
- int size = size(array);\r
- if (size<newSize) newSize = size;\r
- remove( array, size-newSize, newSize);\r
- }\r
-\r
- @Override\r
- public int size(Object array) throws BindingException {\r
- @SuppressWarnings("unchecked")\r
- Median<Object> median = (Median<Object>) array;\r
- int count = 0;\r
- if (median.median != null) count++;\r
- count += median.lower.size();\r
- count += median.upper.size();\r
- return count;\r
- }\r
-\r
- @Override\r
- public boolean isInstance(Object obj) {\r
- return obj instanceof Median;\r
- } \r
-\r
- @Override\r
- public boolean isResizable() {\r
- return true;\r
- }\r
- \r
-}\r
-\r
-\r
+/*******************************************************************************
+ * 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<Object>( componentBinding );
+ }
+
+ @Override
+ public Object create(int length, Iterator<Object> it) throws BindingException {
+ Median<Object> result = new Median<Object>( length, componentBinding );
+ while (it.hasNext()) result.add(it.next());
+ return result;
+ }
+
+ @Override
+ public Object create(Object[] array) throws BindingException {
+ Median<Object> result = new Median<Object>( array.length, componentBinding );
+ for (int i=0; i<array.length; i++) result.add( array[i] );
+ return result;
+ }
+
+ @Override
+ public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException {
+ @SuppressWarnings("unchecked")
+ Median<Object> median = (Median<Object>) 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<Object> median = (Median<Object>) array;
+
+ if (index<median.lower.size()) {
+ Iterator<Object> 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<median.upper.size()) {
+ Iterator<Object> 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<Object> median = (Median<Object>) array;
+ if (index<0) throw new BindingException("Index out of bounds");
+ if (index<median.lower.size()) {
+ Object result = null;
+ Iterator<Object> 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<median.upper.size()) {
+ Object result = null;
+ Iterator<Object> 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<c) throw new BindingException("Array too small");
+
+ int index = 0;
+ @SuppressWarnings("unchecked")
+ Median<Object> median = (Median<Object>) array;
+
+ Iterator<Object> 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<newSize) newSize = size;
+ remove( array, size-newSize, newSize);
+ }
+
+ @Override
+ public int size(Object array) throws BindingException {
+ @SuppressWarnings("unchecked")
+ Median<Object> median = (Median<Object>) 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;
+ }
+
+}
+
+