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

📄 listelresolver.java

📁 java属性邦定的(JSR-295)的一个实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param context The context of this evaluation.
     * @param base The list to be modified. Only bases of type 
     *     <code>List</code> are handled by this resolver.
     * @param property The index of the value to be set. Will be coerced
     *     into an integer.
     * @param val The value to be set at the given index.
     * @throws ClassCastException if the class of the specified element 
     *     prevents it from being added to this list.
     * @throws NullPointerException if context is <code>null</code>, or
     *     if the value is <code>null</code> and this <code>List</code>
     *     does not support <code>null</code> elements.
     * @throws IllegalArgumentException if the property could not be coerced
     *     into an integer, or if some aspect of the specified element 
     *     prevents it from being added to this list.
     * @throws PropertyNotWritableException if this resolver was constructed
     *     in read-only mode, or if the set operation is not supported by 
     *     the underlying list.
     * @throws ELException if an exception was thrown while performing
     *     the property or variable resolution. The thrown exception
     *     must be included as the cause property of this exception, if
     *     available.
     */
    public void setValue(ELContext context,
                         Object base,
                         Object property,
                         Object val) {

        if (context == null) {
            throw new NullPointerException();
        }

        if (base != null && base instanceof List) {
            context.setPropertyResolved(true);
            List list = (List) base;
            int index = toInteger(property);
            if (isReadOnly) {
                throw new PropertyNotWritableException();
            }
            try {
                list.set(index, val);
            } catch (UnsupportedOperationException ex) {
                throw new PropertyNotWritableException();
            } catch (IndexOutOfBoundsException ex) {
                throw new PropertyNotFoundException();
            } catch (ClassCastException ex) {
                throw ex;
            } catch (NullPointerException ex) {
                throw ex;
            } catch (IllegalArgumentException ex) {
                throw ex;
            }
        }
    }

    static private Class<?> theUnmodifiableListClass =
        Collections.unmodifiableList(new ArrayList()).getClass();

    /**
     * If the base object is a list, returns whether a call to 
     * {@link #setValue} will always fail.
     *
     * <p>If the base is a <code>List</code>, the <code>propertyResolved</code>
     * property of the <code>ELContext</code> object must be set to
     * <code>true</code> by this resolver, before returning. If this property
     * is not <code>true</code> after this method is called, the caller 
     * should ignore the return value.</p>
     *
     * <p>If this resolver was constructed in read-only mode, this method will
     * always return <code>true</code>.</p>
     *
     * <p>If a <code>List</code> was created using 
     * {@link java.util.Collections#unmodifiableList}, this method must
     * return <code>true</code>. Unfortunately, there is no Collections API
     * method to detect this. However, an implementation can create a
     * prototype unmodifiable <code>List</code> and query its runtime type
     * to see if it matches the runtime type of the base object as a 
     * workaround.</p>
     *
     * @param context The context of this evaluation.
     * @param base The list to analyze. Only bases of type <code>List</code>
     *     are handled by this resolver.
     * @param property The index of the element in the list to return the 
     *     acceptable type for. Will be coerced into an integer, but 
     *     otherwise ignored by this resolver.
     * @return If the <code>propertyResolved</code> property of 
     *     <code>ELContext</code> was set to <code>true</code>, then
     *     <code>true</code> if calling the <code>setValue</code> method
     *     will always fail or <code>false</code> if it is possible that
     *     such a call may succeed; otherwise undefined.
     * @throws PropertyNotFoundException if the given index is out of 
     *     bounds for this list.
     * @throws NullPointerException if context is <code>null</code>
     * @throws ELException if an exception was thrown while performing
     *     the property or variable resolution. The thrown exception
     *     must be included as the cause property of this exception, if
     *     available.
     */
    public boolean isReadOnly(ELContext context,
                              Object base,
                              Object property) {

        if (context == null) {
            throw new NullPointerException();
        }

        if (base != null && base instanceof List) {
            context.setPropertyResolved(true);
            List list = (List) base;
            int index = toInteger(property);
            if (index < 0 || index >= list.size()) {
                throw new PropertyNotFoundException();
            } 
            return list.getClass() == theUnmodifiableListClass || isReadOnly;
        }
        return false;
    }

    /**
     * Always returns <code>null</code>, since there is no reason to 
     * iterate through set set of all integers.
     *
     * <p>The {@link #getCommonPropertyType} method returns sufficient
     * information about what properties this resolver accepts.</p>
     *
     * @param context The context of this evaluation.
     * @param base The list. Only bases of type <code>List</code> are 
     *     handled by this resolver.
     * @return <code>null</code>.
     */
    public Iterator<FeatureDescriptor> getFeatureDescriptors(
                                          ELContext context,
                                          Object base) {
        return null;
    }

    /**
     * If the base object is a list, returns the most general type that 
     * this resolver accepts for the <code>property</code> argument.
     * Otherwise, returns <code>null</code>.
     *
     * <p>Assuming the base is a <code>List</code>, this method will always
     * return <code>Integer.class</code>. This is because <code>List</code>s
     * accept integers as their index.</p>
     *
     * @param context The context of this evaluation.
     * @param base The list to analyze. Only bases of type <code>List</code>
     *     are handled by this resolver.
     * @return <code>null</code> if base is not a <code>List</code>; otherwise
     *     <code>Integer.class</code>.
     */
    public Class<?> getCommonPropertyType(ELContext context,
                                               Object base) {
        if (base != null && base instanceof List) {
            return Integer.class;
        }
        return null;
    }
    
    private int toInteger(Object p) {
        if (p instanceof Integer) {
            return ((Integer) p).intValue();
        }
        if (p instanceof Character) {
            return ((Character) p).charValue();
        }
        if (p instanceof Boolean) {
            return ((Boolean) p).booleanValue()? 1: 0;
        }
        if (p instanceof Number) {
            return ((Number) p).intValue();
        }
        if (p instanceof String) {
            return Integer.parseInt((String) p);
        }
        throw new IllegalArgumentException();
    }

    private boolean isReadOnly;
}

⌨️ 快捷键说明

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