jface databinding:List,Set,Map对象的Observable代理封装

需求描述

对于一个已经存在的集合/映射对象(普通的List,Set,Map,非observable),我们希望把将它转换成一个observable对象,这个observable对象就像是原对象的代理一样,当对observable对象操作(增加删除元素)时,实际是对原对象的操作。 jface为List,Set,Map三种类型提供了对应的三种可写对象WritableList,WritableSet,WritableMap,研究了这三个类的代码,发现它们99%是满足这个需求,然并卵,因为构造函数上设计区别,造成这三个类的构造函数生成的observable对象与原对象是隔离的。 以WritableSet的构造函数为例,WritableSet的构造函数重新用外部传入的Collection的内容构造了一个新的HashSet对象,所以WritableSet中的Set对象与传入的原对象(Collection)没有半毛钱关系:

    public WritableSet(Realm realm, Collection<? extends E> c, Object elementType) {
        // 创建了一个新的Set对象传给父类的构造函数
        super(realm, new HashSet<E>(c), elementType);
        this.elementType = elementType;
    }

只有WritableList类可以实现的上述的需求,而且貌似还是为了兼容之前版本的设计失误,而留的。 参见下面WritableList的构造函数的说明org.eclipse.core.databinding.observable.list.WritableList.WritableList(Realm realm, List<E> toWrap, Object elementType)

    /**
     * Creates a writable list containing elements of the given type, wrapping
     * an existing client-supplied list. Note that for backwards compatibility
     * reasons, the contents of the created WritableList will change with the
     * contents of the given list. If this is not desired,
     * {@link #WritableList(Realm, Collection, Object)} should be used by
     * casting the second argument to {@link Collection}.
     *
     * @param realm
     *            the observable's realm
     * @param toWrap
     *            The java.util.List to wrap
     * @param elementType
     *            can be <code>null</code>
     */
    public WritableList(Realm realm, List<E> toWrap, Object elementType) {
        // 没有对toWrap做任何处理就传给了父类的构造函数,这才是我想要的
        super(realm, toWrap, elementType);
    }

所以这虽然是一个很普遍的需求,但jface并没有提供完整的支持,需要自己写代码支持。好在jface的基础框架比较扎实,所以如果搞清楚jface的类继承结构,自己写代码也不复杂,所以我为List,Set,Map三种类型的分别写了三个类用于实现Observable封装(WrappedObservableList,WrappedObservableSet,WrappedObservableMap),这三个类的代码都参考自对应的WritableList,WritableSet,WritableMap三个类,大部分代码都是直接抄来的。以下是三个类对应的实现代码

WrappedObservableList

WrappedObservableList类实现最简单,因为WritableList本身就因为历史原因支持这种代理封装,所以WrappedObservableList类只是在构造函数中加了参数检查 WrappedObservableList.java

package net.gdface.ui.binding;

import java.util.List;
import java.util.Objects;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;

/**
 * 将指定的{@link List}对象(wrappedList)封装为 {@link ObservableList},<br>
 * 对observable对象的操作同步到原对象<br>
 * @author guyadong
 *
 * @param <E>
 */
public class WrappedObservableList<E> extends WritableList<E> {

    public WrappedObservableList(List<E> wrappedList, Object elementType) {
        this(Realm.getDefault(),wrappedList, elementType);
    }

    public WrappedObservableList(Realm realm, List<E> wrappedList, Object elementType) {
        super(realm, Objects.requireNonNull(wrappedList,"the argument wrappedList must not be null"), elementType);
    }   
}

WrappedObservableSet

WrappedObservableSet类的大部分代码都抄自WritableSet WrappedObservableSet.java

package net.gdface.ui.binding;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.set.ObservableSet;

/**
 * 将指定的{@link Set}对象(wrappedSet)封装为 {@link ObservableSet},<br>
 * 对observable对象的操作同步到原对象<br>
 * override方法实现代码抄自{@link org.eclipse.core.databinding.observable.set.WritableSet}
 * @author guyadong
 *
 * @param <E>
 */
public class WrappedObservableSet<E> extends ObservableSet<E>{

    /**
     * @param realm
     * @param wrappedSet 为null抛出异常
     * @param elementType
     */
    protected WrappedObservableSet(Realm realm, Set<E> wrappedSet, Object elementType) {
        super(realm, Objects.requireNonNull(wrappedSet,"the argument wrappedSet must not be null"), elementType);       
    }

    public WrappedObservableSet(Set<E> wrappedSet, Object elementType) {
        this(Realm.getDefault(),wrappedSet, elementType);
    }
    public WrappedObservableSet(Set<E> wrappedSet) {
        this(Realm.getDefault(),wrappedSet, null);
    }
    @Override
    public boolean add(E o) {
        getterCalled();
        boolean added = wrappedSet.add(o);
        if (added) {
            Set<E> removals = Collections.emptySet();
            fireSetChange(Diffs.createSetDiff(Collections.singleton(o), removals));
        }
        return added;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        getterCalled();
        Set<E> additions = new HashSet<E>();
        Iterator<? extends E> it = c.iterator();
        while (it.hasNext()) {
            E element = it.next();
            if (wrappedSet.add(element)) {
                additions.add(element);
            }
        }
        if (additions.size() > 0) {
            Set<E> removals = Collections.emptySet();
            fireSetChange(Diffs.createSetDiff(additions, removals));
            return true;
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean remove(Object o) {
        getterCalled();
        boolean removed = wrappedSet.remove(o);
        if (removed) {
            Set<E> additions = Collections.emptySet();
            fireSetChange(Diffs.createSetDiff(additions,
                    Collections.singleton((E) o)));
        }
        return removed;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean removeAll(Collection<?> c) {
        getterCalled();
        Set<E> removes = new HashSet<E>();
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            Object element = it.next();
            if (wrappedSet.remove(element)) {
                removes.add((E) element);
            }
        }
        if (removes.size() > 0) {
            Set<E> additions = Collections.emptySet();
            fireSetChange(Diffs.createSetDiff(additions, removes));
            return true;
        }
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        getterCalled();
        Set<E> removes = new HashSet<E>();
        Iterator<E> it = wrappedSet.iterator();
        while (it.hasNext()) {
            E element = it.next();
            if (!c.contains(element)) {
                it.remove();
                removes.add(element);
            }
        }
        if (removes.size() > 0) {
            Set<E> additions = Collections.emptySet();
            fireSetChange(Diffs.createSetDiff(additions, removes));
            return true;
        }
        return false;
    }

    @Override
    public void clear() {
        getterCalled();
        Set<E> removes = new HashSet<E>(wrappedSet);
        Set<E> additions = Collections.emptySet();
        wrappedSet.clear();
        fireSetChange(Diffs.createSetDiff(additions, removes));
    }
}

WrappedObservableMap

WrappedObservableMap的大部分代码也都是抄自WritableMap WrappedObservableMap.java

package net.gdface.ui.binding;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.map.MapDiff;
import org.eclipse.core.databinding.observable.map.ObservableMap;
import org.eclipse.core.internal.databinding.observable.Util;

/**
 * 将指定的{@link Map}对象(wrappedMap)封装为 {@link ObservableMap},<br>
 * 对observable对象的操作同步到原对象<br>
 * override方法实现代码抄自 {@link org.eclipse.core.databinding.observable.map.WritableMap}
 * @author guyadong
 *
 * @param <K>
 * @param <V>
 */
public class WrappedObservableMap<K, V> extends ObservableMap<K, V> {

    private final Object keyType;
    private final Object valueType;

    public WrappedObservableMap(Map<K, V> wrappedMap) {
        this(Realm.getDefault(),wrappedMap, null, null);
    }

    public WrappedObservableMap(Map<K, V> wrappedMap, Object keyType, Object valueType) {
        this(Realm.getDefault(),wrappedMap, keyType, valueType);
    }
    /**
     * @param realm
     * @param wrappedMap 为null抛出异常
     * @param keyType
     * @param valueType
     */
    public WrappedObservableMap(Realm realm, Map<K, V> wrappedMap, Object keyType, Object valueType) {
        super(realm, Objects.requireNonNull(wrappedMap,"the argument wrappedMap must not be null"));
        this.keyType=keyType;
        this.valueType=valueType;
    }

    @Override
    public Object getKeyType() {
        return keyType;
    }

    @Override
    public Object getValueType() {
        return valueType;
    }
    @Override
    public V put(K key, V value) {
        checkRealm();

        boolean containedKeyBefore = wrappedMap.containsKey(key);
        V result = wrappedMap.put(key, value);
        boolean containedKeyAfter = wrappedMap.containsKey(key);

        if (containedKeyBefore != containedKeyAfter
                || !Util.equals(result, value)) {
            MapDiff<K, V> diff;
            if (containedKeyBefore) {
                if (containedKeyAfter) {
                    diff = Diffs.createMapDiffSingleChange(key, result, value);
                } else {
                    diff = Diffs.createMapDiffSingleRemove(key, result);
                }
            } else {
                diff = Diffs.createMapDiffSingleAdd(key, value);
            }
            fireMapChange(diff);
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    public V remove(Object key) {
        checkRealm();
        if (wrappedMap.containsKey(key)) {
            V result = wrappedMap.remove(key);
            fireMapChange(Diffs.createMapDiffSingleRemove((K) key, result));
            return result;
        }
        return null;
    }

    @Override
    public void clear() {
        checkRealm();
        if (!isEmpty()) {
            Map<K, V> copy = new HashMap<>(wrappedMap);
            wrappedMap.clear();
            fireMapChange(Diffs.createMapDiffRemoveAll(copy));
        }
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        checkRealm();
        Set<K> addedKeys = new HashSet<>(map.size());
        Map<K, V> changes = new HashMap<>(map.size());
        for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
            boolean add = !wrappedMap.containsKey(entry.getKey());
            V previousValue = wrappedMap.put(entry.getKey(), entry.getValue());
            if (add) {
                addedKeys.add(entry.getKey());
            } else {
                changes.put(entry.getKey(), previousValue);
            }
        }
        if (!addedKeys.isEmpty() || !changes.isEmpty()) {
            Set<K> removedKeys = Collections.emptySet();
            fireMapChange(Diffs.createMapDiff(addedKeys, removedKeys,
                    changes.keySet(), changes, wrappedMap));
        }
    }

}

注意

上面代码中使用了java8才有方法,所以要在java8下编译

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏LinkedBear的个人空间

唠唠SE的集合-09——Map接口 原

将键映射到值的对象,一个映射不能包含重复的键,但值可以重复。每个键最多只能映射到一个值。

822
来自专栏小灰灰

JDK容器学习之LinkedHashMap(二):迭代遍历的实现方式

LinkedHashMap 如何保障有序的遍历 前一篇《JDK容器学习之LinkedHashMap (一):底层存储结构分析》 中介绍了LinkedHashM...

2057
来自专栏码匠的流水账

聊聊storm的PartialKeyGrouping

storm-core-1.2.2-sources.jar!/org/apache/storm/grouping/PartialKeyGrouping.java

913
来自专栏Danny的专栏

Java基础——Map接口

  通常来说,Map是一个由键值对组成的数据结构,且在集合中每个键是唯一的。下面就以K和V来代表键和值,来说明一下java中关于Map的几个问题。

682
来自专栏Java帮帮-微信公众号-技术文章全总结

Java基础-18(02)总结Map,HashMap,HashMap与Hashtable区别,Collections工具类

(8)Hashtable和HashMap的区别? package cn.itcast_07; import java.util.Hashtable; /* *...

2735
来自专栏拭心的安卓进阶之路

Java 集合深入理解(15):AbstractMap

今天来了解下 AbstractMap。 ? 什么是 AbstractMap AbstractMap 是 Map 接口的的实现类之一,也是 HashMap, T...

1959
来自专栏码匠的流水账

聊聊storm的reportError

storm-2.0.0/storm-client/src/jvm/org/apache/storm/task/IErrorReporter.java

873
来自专栏码匠的流水账

聊聊storm的reportError

storm-2.0.0/storm-client/src/jvm/org/apache/storm/task/IErrorReporter.java

963
来自专栏Golang语言社区

Go语言实现set

package set type Set interface { Add(e interface{}) bool Re...

2806
来自专栏数据之美

Sort Map by Value in Java

实际项目或者业务当中,经常会有需求要求对 hashmap 按值排序,并返回指定顺序的 TopN 个元素,今天就来分享下具体的代码及其原理实现。 package ...

1816

扫码关注云+社区