/******************************************************************************* * 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.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; 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 Databoard Map(T, {}) to java.util.Set and instantiates java.util.HashSet * * @author Toni Kalajainen */ @SuppressWarnings({"rawtypes", "unchecked"}) public class HashSetBinding extends MapBinding { public HashSetBinding(MapType mapType, Binding elementBinding) { super(mapType, elementBinding, VoidBinding.VOID_BINDING); } public HashSetBinding(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 { // HashSet _set = (HashSet) set; // return _set.contains(key); Set s = ((Set)set); Binding kb = getKeyBinding(); for (Object v : s) { if (kb.equals(v, key)) return true; } return false; } @Override public boolean containsValue(Object set, Object value) throws BindingException { return false; } @Override public Object create() throws BindingException { return new HashSet(); } public Object create(Set initialSet) throws BindingException { return new HashSet(initialSet); } @Override public Object create(Map initialMap) throws BindingException { return new HashSet(initialMap.keySet()); } @Override public Object create(Object[] keys, Object[] values) throws BindingException { HashSet result = new HashSet(keys.length); for (int i=0; i keys, List values) { HashSet result = new HashSet(keys.size()); for (int i=0; i _setFrom = (HashSet) setFrom; for (Object 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; Object[] result = _set.toArray(new Object[_set.size()]); Arrays.sort(result, getKeyBinding()); return result; } @Override public void getKeys(Object set, Set keys) throws BindingException { Set s = (Set) set; keys.addAll(s); } @Override public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException { int result = 0; Set m = ((Set)src); for (Object k : m) { int fk = keyBinding.compare(from, k); int ek = keyBinding.compare(k, end); boolean fromMatches = fromInclusive ? fk<=0 : fk<0; boolean endMatches = endInclusive ? ek<=0 : ek <0; if ( fromMatches && endMatches ) result++; } return result; } 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 Object[] getValues(Object set) throws BindingException { Set _set = (Set) set; return new Object[_set.size()]; } @Override public void put(Object set, Object key, Object value) throws BindingException { if (value!=null) throw new BindingException("Cannot put non-null to a Set"); Set s = (Set) set; Binding kb = getKeyBinding(); for (Object e : s) { if (kb.equals(e, key)) { s.remove(e); s.add(value); return; } } s.add(key); } Object getComparableKey(Object set, Object key) { // if (keyIsComparable) return key; Set s = (Set) set; Binding kb = getKeyBinding(); for (Object k : s) { if (kb.equals(k, key)) return k; } return 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 s = (Set) setTo; for (Entry e : (Set>) from.entrySet()) { Object k = getComparableKey(s, e.getKey()); s.remove(k); s.add(e.getKey()); } } @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) { Set s = (Set) set; if (s.isEmpty()) return null; Comparator comparator = getKeyBinding(); Object pivot = null; for (Object o : s) { // We are trying to find key > o > pivot int c2 = comparator.compare(key, o); if (c2>0) continue; if (pivot==null) {pivot = o; continue;} int c1 = comparator.compare(o, pivot); if (c1<0) pivot = o; } return pivot; } @Override public Object getFirstKey(Object set) { Set s = (Set) set; if (s.isEmpty()) return null; Comparator c = getKeyBinding(); Object result = null; for (Object o : s) { if (result==null) { result = o; continue; } if (c.compare(o, result)<0) result = o; } return result; } @Override public Object getFloorKey(Object set, Object key) { Set s = (Set) set; if (s.isEmpty()) return null; Comparator comparator = getKeyBinding(); Object pivot = null; for (Object o : s) { // We are trying to find pivot <= o <= key int c2 = comparator.compare(o, key); if (c2==0) return o; if (c2>0) continue; if (pivot==null) {pivot = o; continue;} int c1 = comparator.compare(pivot, o); if (c1<0) pivot = o; } return pivot; } @Override public Object getHigherKey(Object set, Object key) { Set s = (Set) set; if (s.isEmpty()) return null; Comparator comparator = getKeyBinding(); Object pivot = null; for (Object o : s) { // We are trying to find key > o > pivot int c2 = comparator.compare(key, o); if (c2>=0) continue; if (pivot==null) {pivot = o; continue;} int c1 = comparator.compare(o, pivot); if (c1<0) pivot = o; } return pivot; } @Override public Object getLastKey(Object set) { Set s = (Set) set; if (s.isEmpty()) return null; Comparator c = getKeyBinding(); Object result = null; for (Object o : s) { if (result==null) { result = o; continue; } if (c.compare(o, result)>0) result = o; } return result; } @Override public Object getLowerKey(Object set, Object key) { Set s = (Set) set; if (s.isEmpty()) return null; Comparator comparator = getKeyBinding(); Object pivot = null; for (Object o : s) { // We are trying to find pivot < o < key int c2 = comparator.compare(o, key); if (c2>=0) continue; if (pivot==null) {pivot = o; continue;} int c1 = comparator.compare(pivot, o); if (c1<0) pivot = o; } return pivot; } }