X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FDefaultSetBinding.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FDefaultSetBinding.java;h=783f80b3350c5d2773faa9fbcd4da83d2862c046;hb=fe8abfc35440c00d9a116bfebe1b58ff2fe2eff7;hp=0000000000000000000000000000000000000000;hpb=43ddca759254b8e38029c1041d91cbdd7890c9b5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DefaultSetBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DefaultSetBinding.java new file mode 100644 index 000000000..783f80b33 --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DefaultSetBinding.java @@ -0,0 +1,239 @@ +/******************************************************************************* + * Copyright (c) 2010 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.databoard.binding.impl; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.simantics.databoard.binding.ArrayBinding; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.MapBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.reflection.VoidBinding; +import org.simantics.databoard.type.MapType; +import org.simantics.databoard.type.RecordType; + +/** + * Binds java.util.Set to Map(T, {}) + * + * This binding accepts all java.util.Set instances, but instantiates + * java.util.TreeSet objects. + * + * @author Reino Ruusu + */ +@SuppressWarnings("unchecked") +public class DefaultSetBinding extends MapBinding { + + public DefaultSetBinding(MapType mapType, Binding elementBinding) { + super(mapType, elementBinding, VoidBinding.VOID_BINDING); + } + + public DefaultSetBinding(Binding elementBinding) { + super(new MapType(elementBinding.type(), RecordType.VOID_TYPE), elementBinding, VoidBinding.VOID_BINDING); + } + + @Override + public void clear(Object set) throws BindingException { + Set _set = (Set) set; + _set.clear(); + } + + @Override + public boolean containsKey(Object set, Object key) throws BindingException { + Set _set = (Set) set; + return _set.contains(key); + } + + @Override + public boolean containsValue(Object set, Object value) + throws BindingException { + return false; + } + + @Override + public Object create() throws BindingException { + return new TreeSet( keyBinding ); + } + + public Object create(Set initialSet) throws BindingException { + return initialSet; + } + + @Override + public Object create(Map initialMap) throws BindingException { + Set result = new TreeSet( keyBinding ); + result.addAll(initialMap.keySet()); + return result; + } + + @Override + public Object create(Object[] keys, Object[] values) + throws BindingException { + Set result = new TreeSet( keyBinding ); + for (int i=0; i keys, List values) { + Set result = new TreeSet( keyBinding ); + for (int i=0; i void getAll(Object setFrom, Map to) throws BindingException { + Map _to = (Map) to; + Set _setFrom = (Set) setFrom; + for (K k : _setFrom) + _to.put(k, null); + } + + @Override + public void getAll(Object setFrom, Object[] keys, Object[] values) + throws BindingException { + Set _setFrom = (Set) setFrom; + int i=0; + for (Object k : _setFrom) { + keys[i] = k; + values[i] = null; + } + } + + @Override + public Object[] getKeys(Object set) throws BindingException { + Set _set = (Set) set; + return _set.toArray(new Object[_set.size()]); + } + + @Override + public void getKeys(Object set, Set keys) throws BindingException { + Set s = (Set) set; + keys.addAll(s); + } + + @Override + public Object[] getValues(Object set) throws BindingException { + Set _set = (Set) set; + return new Object[_set.size()]; + } + + @Override + public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException { + if (src instanceof TreeSet) + return new TreeSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive); + else + return new HashSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive); + } + + @Override + public int getEntries(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive, ArrayBinding dstKeyArrayBinding, Object dstKeyArray, ArrayBinding dstValueArrayBinding, Object dstValueArray, int limit) throws BindingException { + return 0; + } + + @Override + public void put(Object set, Object key, Object value) + throws BindingException { + Set _set = (Set) set; + if (value!=null) throw new BindingException("Cannot put non-null to a Set"); + _set.add(key); + } + + public void putAll(Object setTo, Set from) { + Set _set = (Set) setTo; + _set.addAll(from); + } + + @Override + public void putAll(Object setTo, Map from) throws BindingException { + Set _set = (Set) setTo; + _set.addAll(from.keySet()); + } + + @Override + public Object remove(Object set, Object key) throws BindingException { + Set _set = (Set) set; + _set.remove(key); + return null; + } + + @Override + public int size(Object set) throws BindingException { + Set _set = (Set) set; + return _set.size(); + } + + @Override + public boolean isInstance(Object obj) { + return obj instanceof Set; + } + + @Override + public Object getCeilingKey(Object set, Object key) { + if (set instanceof TreeSet) + return new TreeSetBinding(keyBinding).getCeilingKey(set, key); + else + return new HashSetBinding(keyBinding).getCeilingKey(set, key); + } + + @Override + public Object getFirstKey(Object set) { + if (set instanceof TreeSet) + return new TreeSetBinding(keyBinding).getFirstKey(set); + else + return new HashSetBinding(keyBinding).getFirstKey(set); + } + + @Override + public Object getFloorKey(Object set, Object key) { + if (set instanceof TreeSet) + return new TreeSetBinding(keyBinding).getFloorKey(set, key); + else + return new HashSetBinding(keyBinding).getFloorKey(set, key); + } + + @Override + public Object getHigherKey(Object set, Object key) { + if (set instanceof TreeSet) + return new TreeSetBinding(keyBinding).getHigherKey(set, key); + else + return new HashSetBinding(keyBinding).getHigherKey(set, key); + } + + @Override + public Object getLastKey(Object set) { + if (set instanceof TreeSet) + return new TreeSetBinding(keyBinding).getLastKey(set); + else + return new HashSetBinding(keyBinding).getLastKey(set); + } + + @Override + public Object getLowerKey(Object set, Object key) { + if (set instanceof TreeSet) + return new TreeSetBinding(keyBinding).getLowerKey(set, key); + else + return new HashSetBinding(keyBinding).getLowerKey(set, key); + } + +} +