]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/util/MedianBinding.java
Assertion template for L0.SCLValue
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / util / MedianBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.history.util;
13
14 import java.util.Iterator;
15
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;
20
21 /**
22  * This class binds median class to as an array type
23  *  
24  */
25 public class MedianBinding extends ArrayBinding {
26
27         public MedianBinding(Binding componentBinding) {
28                 this(new ArrayType(componentBinding.type()), componentBinding);
29         }
30         
31         public MedianBinding(ArrayType type, Binding componentBinding) {
32                 super(type, componentBinding);
33                 if (type==null) throw new IllegalArgumentException("null arg");
34                 this.type = type;
35         }
36                 
37         @Override
38         public Object create() {
39                 return new Median<Object>( componentBinding );
40         }
41
42         @Override
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());             
46                 return result;
47         }
48
49         @Override
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] );
53                 return result;
54         }
55
56         @Override
57         public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException {
58                 @SuppressWarnings("unchecked")
59                 Median<Object> median = (Median<Object>) array;
60                 median.add(element);
61         }
62
63         @Override
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");
67                 
68                 @SuppressWarnings("unchecked")
69                 Median<Object> median = (Median<Object>) array;
70                 
71                 if (index<median.lower.size()) {
72                         Iterator<Object> it = median.lower.iterator();
73                         for ( int i=0; i<=index; i++ ) it.next();
74                         it.remove();
75                         return;
76                 }
77                 index -= median.lower.size();
78                 if (median.median != null) index--;
79                 if (index==-1) {
80                 if (median.upper.size() >= median.lower.size()) {
81                     median.median = median.upper.remove();
82                 } else {
83                     median.median = median.lower.remove();
84                 }                       
85                 return;
86                 }
87                 if (index<median.upper.size()) {
88                         Iterator<Object> it = median.upper.iterator();
89                         for ( int i=0; i<=index; i++ ) it.next();
90                         it.remove();
91                         return;
92                 }                               
93                 
94         }
95
96         @Override
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();
105                         return result;
106                 }
107                 index -= median.lower.size();
108                 if (median.median != null) index--;
109                 if (index==-1) {
110                         return median.median;
111                 }
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();
116                         return result;
117                 }                               
118                 return null;
119         }
120
121         @Override
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");
125
126                 int index = 0;
127                 @SuppressWarnings("unchecked")
128                 Median<Object> median = (Median<Object>) array;
129
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();
135         }
136
137         @Override
138         public void set(Object array, int index, Object value) throws BindingException {
139                 remove(array, index);
140                 add(array, value);
141         }
142
143         @Override
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);
148         }
149
150         @Override
151         public int size(Object array) throws BindingException {
152                 @SuppressWarnings("unchecked")
153                 Median<Object> median = (Median<Object>) array;
154                 int count = 0;
155                 if (median.median != null) count++;
156                 count += median.lower.size();
157                 count += median.upper.size();
158                 return count;
159         }
160
161         @Override
162         public boolean isInstance(Object obj) {
163                 return obj instanceof Median;
164         }       
165
166         @Override
167         public boolean isResizable() {
168                 return true;
169         }
170         
171 }
172
173