Javafx 将一个观察者镜像到另一个

Javafx 将一个观察者镜像到另一个,javafx,mirror,observablelist,Javafx,Mirror,Observablelist,在JavaFX中,我有一个对象的ObservableList,需要另一个ObservableList,它将镜像第一个列表,但包含每个对象的字符串表示。有什么比编写一个自定义的ListChangeListener来进行转换更简单的方法吗?我有一个StringConverter,它可以提供镜像值 类似地,给定一个可观察列表,如何创建第二个可观察列表,该列表在索引0处有一个常量条目,并镜像从索引1开始的第一个列表 对于第一个问题,最简单的方法是使用框架。那就简单到 ObservableList<

在JavaFX中,我有一个对象的
ObservableList
,需要另一个
ObservableList
,它将镜像第一个列表,但包含每个对象的字符串表示。有什么比编写一个自定义的
ListChangeListener
来进行转换更简单的方法吗?我有一个
StringConverter
,它可以提供镜像值

  • 类似地,给定一个
    可观察列表
    ,如何创建第二个
    可观察列表
    ,该列表在索引0处有一个常量条目,并镜像从索引1开始的第一个列表


  • 对于第一个问题,最简单的方法是使用框架。那就简单到

    ObservableList<String> stringList = EasyBind.map(myBaseList, myConverter::toString);
    


    对于第一个问题,最简单的方法是使用框架。那就简单到

    ObservableList<String> stringList = EasyBind.map(myBaseList, myConverter::toString);
    


    对于第一个问题,最简单的方法是使用框架。那就简单到

    ObservableList<String> stringList = EasyBind.map(myBaseList, myConverter::toString);
    


    对于第一个问题,最简单的方法是使用框架。那就简单到

    ObservableList<String> stringList = EasyBind.map(myBaseList, myConverter::toString);
    


    下面是James_D回答的略短版本,使用Lombok的@Delegate避免编写所有的委托方法

    /**
     * A List that mirrors a base List by applying a converter on all items.
     * @param <E> item type of the target List
     * @param <F> item type of the base list
     */
    public class MirroringList<E, F> extends TransformationList<E, F> implements ObservableList<E> {
    
        /** mapping function from base list item type to target list item type */
        private final Function<F, E> converter;
    
        public MirroringList(ObservableList<? extends F> list, Function<F, E> converter) {
            super(list);
            this.converter = converter;
        }
    
        @Override
        public int getSourceIndex(int index) {
            return index;
        }
    
        @Override
        public E get(int index) {
            return converter.apply(getSource().get(index));
        }
    
        @Override
        public int size() {
            return getSource().size();
        }
    
        @Override
        protected void sourceChanged(Change<? extends F> change) {
            fireChange(new DelegatingChange(change, this));
        }
    
    
        /**
         * An implementation of {@link Change} that delegates all methods to a specified change except {@link #getRemoved()} 
         */
        private class DelegatingChange extends Change<E> implements DelegatingChangeExcluded<E> {
    
            @Delegate(excludes = DelegatingChangeExcluded.class)
            private final Change<? extends F> change;
    
            public DelegatingChange(Change<? extends F> change, MirroringList<E, F> list) {
                super(list);
                this.change = change;
            }
    
            @Override
            protected int[] getPermutation() {
                return new int[0];
            }
    
            @Override
            public List<E> getRemoved() {
                return change.getRemoved().stream()
                        .map(converter)
                        .collect(Collectors.toList());
            }
        }
    
        /**
         * This interface is only used to exclude some methods from delegated methods via Lombok's @{@link Delegate}
         * so that the compiler doesn't complain.
         */
        @SuppressWarnings("unused")
        private interface DelegatingChangeExcluded<E> {
            List<E> getRemoved();
            ObservableList<? extends E> getList();
            List<E> getAddedSubList();
        }
    }
    
    /**
    *通过对所有项应用转换器来镜像基本列表的列表。
    *@param目标列表的项目类型
    *@param基本列表的项目类型
    */
    公共类MirroringList扩展TransformationList实现ObservableList{
    /**将函数从基本列表项类型映射到目标列表项类型*/
    专用最终函数转换器;
    
    公共镜像列表(ObservableList这里是James_D回答的略短版本,使用Lombok的@Delegate来避免编写所有的委托方法

    /**
     * A List that mirrors a base List by applying a converter on all items.
     * @param <E> item type of the target List
     * @param <F> item type of the base list
     */
    public class MirroringList<E, F> extends TransformationList<E, F> implements ObservableList<E> {
    
        /** mapping function from base list item type to target list item type */
        private final Function<F, E> converter;
    
        public MirroringList(ObservableList<? extends F> list, Function<F, E> converter) {
            super(list);
            this.converter = converter;
        }
    
        @Override
        public int getSourceIndex(int index) {
            return index;
        }
    
        @Override
        public E get(int index) {
            return converter.apply(getSource().get(index));
        }
    
        @Override
        public int size() {
            return getSource().size();
        }
    
        @Override
        protected void sourceChanged(Change<? extends F> change) {
            fireChange(new DelegatingChange(change, this));
        }
    
    
        /**
         * An implementation of {@link Change} that delegates all methods to a specified change except {@link #getRemoved()} 
         */
        private class DelegatingChange extends Change<E> implements DelegatingChangeExcluded<E> {
    
            @Delegate(excludes = DelegatingChangeExcluded.class)
            private final Change<? extends F> change;
    
            public DelegatingChange(Change<? extends F> change, MirroringList<E, F> list) {
                super(list);
                this.change = change;
            }
    
            @Override
            protected int[] getPermutation() {
                return new int[0];
            }
    
            @Override
            public List<E> getRemoved() {
                return change.getRemoved().stream()
                        .map(converter)
                        .collect(Collectors.toList());
            }
        }
    
        /**
         * This interface is only used to exclude some methods from delegated methods via Lombok's @{@link Delegate}
         * so that the compiler doesn't complain.
         */
        @SuppressWarnings("unused")
        private interface DelegatingChangeExcluded<E> {
            List<E> getRemoved();
            ObservableList<? extends E> getList();
            List<E> getAddedSubList();
        }
    }
    
    /**
    *通过对所有项应用转换器来镜像基本列表的列表。
    *@param目标列表的项目类型
    *@param基本列表的项目类型
    */
    公共类MirroringList扩展TransformationList实现ObservableList{
    /**将函数从基本列表项类型映射到目标列表项类型*/
    专用最终函数转换器;
    
    公共镜像列表(ObservableList这里是James_D回答的略短版本,使用Lombok的@Delegate来避免编写所有的委托方法

    /**
     * A List that mirrors a base List by applying a converter on all items.
     * @param <E> item type of the target List
     * @param <F> item type of the base list
     */
    public class MirroringList<E, F> extends TransformationList<E, F> implements ObservableList<E> {
    
        /** mapping function from base list item type to target list item type */
        private final Function<F, E> converter;
    
        public MirroringList(ObservableList<? extends F> list, Function<F, E> converter) {
            super(list);
            this.converter = converter;
        }
    
        @Override
        public int getSourceIndex(int index) {
            return index;
        }
    
        @Override
        public E get(int index) {
            return converter.apply(getSource().get(index));
        }
    
        @Override
        public int size() {
            return getSource().size();
        }
    
        @Override
        protected void sourceChanged(Change<? extends F> change) {
            fireChange(new DelegatingChange(change, this));
        }
    
    
        /**
         * An implementation of {@link Change} that delegates all methods to a specified change except {@link #getRemoved()} 
         */
        private class DelegatingChange extends Change<E> implements DelegatingChangeExcluded<E> {
    
            @Delegate(excludes = DelegatingChangeExcluded.class)
            private final Change<? extends F> change;
    
            public DelegatingChange(Change<? extends F> change, MirroringList<E, F> list) {
                super(list);
                this.change = change;
            }
    
            @Override
            protected int[] getPermutation() {
                return new int[0];
            }
    
            @Override
            public List<E> getRemoved() {
                return change.getRemoved().stream()
                        .map(converter)
                        .collect(Collectors.toList());
            }
        }
    
        /**
         * This interface is only used to exclude some methods from delegated methods via Lombok's @{@link Delegate}
         * so that the compiler doesn't complain.
         */
        @SuppressWarnings("unused")
        private interface DelegatingChangeExcluded<E> {
            List<E> getRemoved();
            ObservableList<? extends E> getList();
            List<E> getAddedSubList();
        }
    }
    
    /**
    *通过对所有项应用转换器来镜像基本列表的列表。
    *@param目标列表的项目类型
    *@param基本列表的项目类型
    */
    公共类MirroringList扩展TransformationList实现ObservableList{
    /**将函数从基本列表项类型映射到目标列表项类型*/
    专用最终函数转换器;
    
    公共镜像列表(ObservableList这里是James_D回答的略短版本,使用Lombok的@Delegate来避免编写所有的委托方法

    /**
     * A List that mirrors a base List by applying a converter on all items.
     * @param <E> item type of the target List
     * @param <F> item type of the base list
     */
    public class MirroringList<E, F> extends TransformationList<E, F> implements ObservableList<E> {
    
        /** mapping function from base list item type to target list item type */
        private final Function<F, E> converter;
    
        public MirroringList(ObservableList<? extends F> list, Function<F, E> converter) {
            super(list);
            this.converter = converter;
        }
    
        @Override
        public int getSourceIndex(int index) {
            return index;
        }
    
        @Override
        public E get(int index) {
            return converter.apply(getSource().get(index));
        }
    
        @Override
        public int size() {
            return getSource().size();
        }
    
        @Override
        protected void sourceChanged(Change<? extends F> change) {
            fireChange(new DelegatingChange(change, this));
        }
    
    
        /**
         * An implementation of {@link Change} that delegates all methods to a specified change except {@link #getRemoved()} 
         */
        private class DelegatingChange extends Change<E> implements DelegatingChangeExcluded<E> {
    
            @Delegate(excludes = DelegatingChangeExcluded.class)
            private final Change<? extends F> change;
    
            public DelegatingChange(Change<? extends F> change, MirroringList<E, F> list) {
                super(list);
                this.change = change;
            }
    
            @Override
            protected int[] getPermutation() {
                return new int[0];
            }
    
            @Override
            public List<E> getRemoved() {
                return change.getRemoved().stream()
                        .map(converter)
                        .collect(Collectors.toList());
            }
        }
    
        /**
         * This interface is only used to exclude some methods from delegated methods via Lombok's @{@link Delegate}
         * so that the compiler doesn't complain.
         */
        @SuppressWarnings("unused")
        private interface DelegatingChangeExcluded<E> {
            List<E> getRemoved();
            ObservableList<? extends E> getList();
            List<E> getAddedSubList();
        }
    }
    
    /**
    *通过对所有项应用转换器来镜像基本列表的列表。
    *@param目标列表的项目类型
    *@param基本列表的项目类型
    */
    公共类MirroringList扩展TransformationList实现ObservableList{
    /**将函数从基本列表项类型映射到目标列表项类型*/
    专用最终函数转换器;
    公共镜像列表(可观察列表)