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

📄 elproperty.java

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

            updateCache();
            notifyListeners(wasWriteable, oldValue, this);
        }

        private void sourceChanged(Object source, String property) {
            if (ignoreChange) {
                return;
            }

            if (property != null) {
                property = property.intern();
            }

            for (RegisteredListener rl : registeredListeners) {
                if (rl.getSource() == source && (property == null || rl.getProperty() == property)) {
                    processSourceChanged();
                    break;
                }
            }
        }

        public void propertyChange(PropertyChangeEvent e) {
            sourceChanged(e.getSource(), e.getPropertyName());
        }

        public void mapKeyValueChanged(ObservableMap map, Object key, Object lastValue) {
            if (key instanceof String) {
                sourceChanged(map, (String)key);
            }
        }

        public void mapKeyAdded(ObservableMap map, Object key) {
            if (key instanceof String) {
                sourceChanged(map, (String)key);
            }
        }

        public void mapKeyRemoved(ObservableMap map, Object key, Object value) {
            if (key instanceof String) {
                sourceChanged(map, (String)key);
            }
        }
    }

    /**
     * Creates an instance of {@code ELProperty} for the given expression.
     *
     * @param expression the expression
     * @return an instance of {@code ELProperty} for the given expression
     * @throws IllegalArgumentException if the path is null or empty
     * @throws PropertyResolutionException if there's a problem with the expression
     */
    public static final <S, V> ELProperty<S, V> create(String expression) {
        return new ELProperty<S, V>(null, expression);
    }

    /**
     * Creates an instance of {@code ELProperty} for the given base property
     * and expression. The expression is relative to the value of the base property.
     *
     * @param baseProperty the base property
     * @param expression the expression
     * @return an instance of {@code ELProperty} for the given base property and expression
     * @throws IllegalArgumentException if the path is null or empty
     * @throws PropertyResolutionException if there's a problem with the expression
     */
    public static final <S, V> ELProperty<S, V> create(Property<S, ?> baseProperty, String expression) {
        return new ELProperty<S, V>(baseProperty, expression);
    }

    /**
     * @throws IllegalArgumentException for empty or {@code null} expression.
     */
    private ELProperty(Property<S, ?> baseProperty, String expression) {
        if (expression == null || expression.length() == 0) {
            throw new IllegalArgumentException("expression must be non-null and non-empty");
        }

        try {
            this.expression = new ExpressionFactoryImpl().createValueExpression(context, expression, Object.class);
        } catch (ELException ele) {
            throw new PropertyResolutionException("Error creating EL expression " + expression, ele);
        }

        this.baseProperty = baseProperty;
    }

    /**
     * {@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 evaluating the expression
     * @see #setValue
     * @see #isWriteable
     */
    public Class<? extends V> getWriteType(S source) {
        SourceEntry entry = map.get(source);

        if (entry != null) {
            entry.validateCache(-1);
 
            if (!entry.cachedIsWriteable) {
                throw new UnsupportedOperationException("Unwriteable");
            }
 
            return (Class<? extends V>)entry.cachedWriteType;
        }

        try {
            expression.setSource(getBeanFromSource(source, true));
            Expression.Result result = expression.getResult(context, false);

            if (result.getType() == Expression.Result.Type.UNRESOLVABLE) {
                log("getWriteType()", "expression is unresolvable");
                throw new UnsupportedOperationException("Unwriteable");
            }

            if (expression.isReadOnly(context)) {
                log("getWriteType()", "property is unwriteable");
                throw new UnsupportedOperationException("Unwriteable");
            }

            return (Class<? extends V>)expression.getType(context);
        } catch (ELException ele) {
            throw new PropertyResolutionException("Error evaluating EL expression " + expression + " on " + source, ele);
        } finally {
            expression.setSource(null);
        }
    }

    /**
     * {@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 evaluating the expression
     * @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;
        }

        try {
            expression.setSource(getBeanFromSource(source, true));
            Expression.Result result = expression.getResult(context, false);

            if (result.getType() == Expression.Result.Type.UNRESOLVABLE) {
                log("getValue()", "expression is unresolvable");
                throw new UnsupportedOperationException("Unreadable");
            }

            return (V)result.getResult();
        } catch (ELException ele) {
            throw new PropertyResolutionException("Error evaluating EL expression " + expression + " on " + source, ele);
        } finally {
            expression.setSource(null);
        }
    }

    /**
     * {@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 evaluating the expression
     * @see #isWriteable
     * @see #getWriteType
     */
    public void setValue(S source, V value) {
        SourceEntry entry = map.get(source);

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

            try {
                entry.ignoreChange = true;
                expression.setSource(getBeanFromSource(source, false));
                expression.setValue(context, value);
            } catch (ELException ele) {
                throw new PropertyResolutionException("Error evaluating EL expression " + expression + " on " + source, ele);
            } finally {
                entry.ignoreChange = false;
                expression.setSource(null);
            }
 
            Object oldValue = entry.cachedValue;
            // PENDING(shannonh) - too heavyweight; should just update cached value
            entry.updateCache();
            notifyListeners(entry.cachedIsWriteable, oldValue, entry);

            return;
        }

        try {
            expression.setSource(getBeanFromSource(source, true));
            Expression.Result result = expression.getResult(context, false);

            if (result.getType() == Expression.Result.Type.UNRESOLVABLE) {
                log("setValue()", "expression is unresolvable");
                throw new UnsupportedOperationException("Unwriteable");
            }

            if (expression.isReadOnly(context)) {
                log("setValue()", "property is unwriteable");
                throw new UnsupportedOperationException("Unwriteable");
            }

            expression.setValue(context, value);
        } catch (ELException ele) {
            throw new PropertyResolutionException("Error evaluating EL expression " + expression + " on " + source, ele);
        } finally {
            expression.setSource(null);
        }
    }

    /**
     * {@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 evaluating the expression
     * @see #isWriteable
     */
    public boolean isReadable(S source) {
        SourceEntry entry = map.get(source);

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

        try {
            expression.setSource(getBeanFromSource(source, true));
            Expression.Result result = expression.getResult(context, false);

            if (result.getType() == Expression.Result.Type.UNRESOLVABLE) {
                log("isReadable()", "expression is unresolvable");
                return false;
            }
            
            return true;
        } catch (ELException ele) {
            throw new PropertyResolutionException("Error evaluating EL expression " + expression + " on " + source, ele);
        } finally {
            expression.setSource(null);
        }
    }

    /**
     * {@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 evaluating the expression
     * @see #isReadable
     */
    public boolean isWriteable(S source) {
        SourceEntry entry = map.get(source);

        if (entry != null) {
            entry.validateCache(-1);
            return entry.cachedIsWriteable;
        }
        
        try {
            expression.setSource(getBeanFromSource(source, true));
            Expression.Result result = expression.getResult(context, false);

            if (result.getType() == Expression.Result.Type.UNRESOLVABLE) {
                log("isWriteable()", "expression is unresolvable");
                return false;
            }

            if (expression.isReadOnly(context)) {
                log("isWriteable()", "property is unwriteable");
                return false;
            }

            return true;
        } catch (ELException ele) {
            throw new PropertyResolutionException("Error evaluating EL expression " + expression + " on " + source, ele);
        } finally {
            expression.setSource(null);
        }
    }

    private Object getBeanFromSource(S source, boolean logErrors) {

⌨️ 快捷键说明

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