⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanproperty.java

📁 java属性邦定的(JSR-295)的一个实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                }
            }
        }

        private void bindingPropertyChanged(PropertyStateEvent pse) {
            validateCache(0);
            Object oldValue = cachedValue;
            boolean wasWriteable = cachedIsWriteable();
            updateCachedBean();
            updateCachedSources(0);
            updateCachedValue();
            updateCachedWriter();
            notifyListeners(wasWriteable, oldValue, this);
        }
        
        private void cachedValueChanged(int index) {
            validateCache(index);
            
            boolean wasWriteable = cachedIsWriteable();
            Object oldValue = cachedValue;
            
            updateCachedSources(index);
            updateCachedValue();
            if (index != path.length()) {
                updateCachedWriter();
            }
            
            notifyListeners(wasWriteable, oldValue, this);
        }
        
        private void mapValueChanged(ObservableMap map, Object key) {
            if (ignoreChange) {
                return;
            }
            
            int index = getSourceIndex(map);
            
            if (index == -1) {
                throw new AssertionError();
            }
            
            if (key.equals(path.get(index))) {
                cachedValueChanged(index + 1);
            }
        }

        public void propertyStateChanged(PropertyStateEvent pe) {
            if (!pe.getValueChanged()) {
                return;
            }

            bindingPropertyChanged(pe);
        }

        private void propertyValueChanged(PropertyChangeEvent pce) {
            if (ignoreChange) {
                return;
            }
            
            int index = getSourceIndex(pce.getSource());
            
            if (index == -1) {
                throw new AssertionError();
            }
            
            String propertyName = pce.getPropertyName();
            if (propertyName == null || path.get(index).equals(propertyName)) {
                cachedValueChanged(index + 1);
            }
        }

        public void propertyChange(PropertyChangeEvent e) {
           propertyValueChanged(e);
        }

        public void mapKeyValueChanged(ObservableMap map, Object key, Object lastValue) {
            mapValueChanged(map, key);
        }

        public void mapKeyAdded(ObservableMap map, Object key) {
            mapValueChanged(map, key);
        }

        public void mapKeyRemoved(ObservableMap map, Object key, Object value) {
            mapValueChanged(map, key);
        }
    }

    /**
     * Creates an instance of {@code BeanProperty} for the given path.
     *
     * @param path the path
     * @return an instance of {@code BeanProperty} for the given path
     * @throws IllegalArgumentException if the path is null, or contains
     *         no property names
     */
    public static final <S, V> BeanProperty<S, V> create(String path) {
        return new BeanProperty<S, V>(null, path);
    }

    /**
     * Creates an instance of {@code BeanProperty} for the given base property
     * and path. The path is relative to the value of the base property.
     *
     * @param baseProperty the base property
     * @param path the path
     * @return an instance of {@code BeanProperty} for the given base property and path
     * @throws IllegalArgumentException if the path is null, or contains
     *         no property names
     */
    public static final <S, V> BeanProperty<S, V> create(Property<S, ?> baseProperty, String path) {
        return new BeanProperty<S, V>(baseProperty, path);
    }

    /**
     * @throws IllegalArgumentException for empty or {@code null} path.
     */
    private BeanProperty(Property<S, ?> baseProperty, String path) {
        this.path = PropertyPath.createPropertyPath(path);
        this.baseProperty = baseProperty;
    }

    private Object getLastSource(S source) {
        Object src = getBeanFromSource(source);

        if (src == null || src == NOREAD) {
            return src;
        }

        for (int i = 0; i < path.length() - 1; i++) {
            src = getProperty(src, path.get(i));
            if (src == null) {
                log("getLastSource()", "missing source");
                return null;
            }
            
            if (src == NOREAD) {
                log("getLastSource()", "missing read method");
                return NOREAD;
            }
        }
        
        return src;
    }

    /**
     * {@inheritDoc}
     * <p>
     * See the class level documentation for the definition of <a href="#WRITEABILITY">writeability</a>.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws PropertyResolutionException if an exception occurs while resolving the path
     * @see #setValue
     * @see #isWriteable
     */
    public Class<? extends V> getWriteType(S source) {
        SourceEntry entry = map.get(source);

        if (entry != null) {
            entry.validateCache(-1);
 
            if (entry.cachedWriter == null) {
                throw new UnsupportedOperationException("Unwriteable");
            }
 
            return (Class<? extends V>)getType(entry.cache[path.length() - 1], path.getLast());
        }

        return (Class<? extends V>)getType(getLastSource(source), path.getLast());
    }
    
    /**
     * {@inheritDoc}
     * <p>
     * See the class level documentation for the definition of <a href="#READABILITY">readability</a>.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws PropertyResolutionException if an exception occurs while resolving the path
     * @see #isReadable
     */
    public V getValue(S source) {
        SourceEntry entry = map.get(source);

        if (entry != null) {
            entry.validateCache(-1);
 
            if (entry.cachedValue == NOREAD) {
                throw new UnsupportedOperationException("Unreadable");
            }
 
            return (V)entry.cachedValue;
        }
        
        Object src = getLastSource(source);
        if (src == null || src == NOREAD) {
            throw new UnsupportedOperationException("Unreadable");
        }
        
        src = getProperty(src, path.getLast());
        if (src == NOREAD) {
            log("getValue()", "missing read method");
            throw new UnsupportedOperationException("Unreadable");
        }
        
        return (V)src;
    }

    /**
     * {@inheritDoc}
     * <p>
     * See the class level documentation for the definition of <a href="#WRITEABILITY">writeability</a>.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws PropertyResolutionException if an exception occurs while resolving the path
     * @see #isWriteable
     * @see #getWriteType
     */
    public void setValue(S source, V value) {
        SourceEntry entry = map.get(source);

        if (entry != null) {
            entry.validateCache(-1);
 
            if (entry.cachedWriter == null) {
                throw new UnsupportedOperationException("Unwritable");
            }

            try {
                entry.ignoreChange = true;
                write(entry.cachedWriter, entry.cache[path.length() - 1], path.getLast(), value);
            } finally {
                entry.ignoreChange = false;
            }
 
            Object oldValue = entry.cachedValue;
            entry.updateCachedValue();
            notifyListeners(entry.cachedIsWriteable(), oldValue, entry);
        } else {
            setProperty(getLastSource(source), path.getLast(), value);
        }
    }
    
    /**
     * {@inheritDoc}
     * <p>
     * See the class level documentation for the definition of <a href="#READABILITY">readability</a>.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws PropertyResolutionException if an exception occurs while resolving the path
     * @see #isWriteable
     */
    public boolean isReadable(S source) {
        SourceEntry entry = map.get(source);

        if (entry != null) {
            entry.validateCache(-1);
            return entry.cachedIsReadable();
        }
        
        Object src = getLastSource(source);
        if (src == null || src == NOREAD) {
            return false;
        }
        
        Object reader = getReader(src, path.getLast());
        if (reader == null) {
            log("isReadable()", "missing read method");
            return false;
        }

        return true;
    }

    /**
     * {@inheritDoc}
     * <p>
     * See the class level documentation for the definition of <a href="#WRITEABILITY">writeability</a>.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws PropertyResolutionException if an exception occurs while resolving the path
     * @see #isReadable
     */
    public boolean isWriteable(S source) {
        SourceEntry entry = map.get(source);

        if (entry != null) {
            entry.validateCache(-1);
            return entry.cachedIsWriteable();
        }

        Object src = getLastSource(source);
        if (src == null || src == NOREAD) {
            return false;
        }

        Object writer = getWriter(src, path.getLast());
        if (writer == null) {
            log("isWritable()", "missing write method");
            return false;
        }

        return true;
    }

    private Object getBeanFromSource(S source) {
        if (baseProperty == null) {
            if (source == null) {
                log("getBeanFromSource()", "source is null");
            }

            return source;
        }

        if (!baseProperty.isReadable(source)) {
            log("getBeanFromSource()", "unreadable source property");
            return NOREAD;
        }

        Object bean = baseProperty.getValue(source);
        if (bean == null) {
            log("getBeanFromSource()", "source property returned null");
            return null;
        }
        
        return bean;
    }

    protected final void listeningStarted(S source) {
        SourceEntry entry = map.get(source);
        if (entry == null) {
            entry = new SourceEntry(source);
            map.put(source, entry);
        }
    }

    protected final void listeningStopped(S source) {
        SourceEntry entry = map.remove(source);
        if (entry != null) {
            entry.cleanup();
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -