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

📄 classmapping.java

📁 定要上载质量高而定要上载质量高而定要上载质量高而定要上载质量高而定要上载质量高而
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * specified {@code Object} and dispatches to
     * {@code removeClassMapping(Class key)}. If {@code obj} is {@code null},
     * then this method returns {@code null}.
     * <p>
     * Removing the mapping for the specified {@code Class} will also remove it
     * for all non-specific subclasses. This means that subclasses of the
     * specified {@code Class} will require specific mappings if the it is
     * desired for the existing mapping behavior for these classes to remain the
     * same.
     * <p>
     * If any instance mappings exist for the specified {@code Class}, they are
     * also removed. This means non-specific subclass instance mappings will
     * also be removed.
     * 
     * @param obj
     *            the {@code Object} whose {@code Class} will be removed from
     *            the internal mapping
     * @return the {@code Class} whose mapping has been removed
     * @see #removeClassMapping(Class)
     * @see #addClassMapping(Object, Class)
     * @see #getClassMapping(Object)
     * @see #getClassInstance(Class)
     */
    public Class removeClassMapping(Object obj) {
        Class key = obj == null ? null : obj.getClass();
        return removeClassMapping(key);
    }

    /**
     * Removes any existing class mappings for the {@code Class} type of the
     * specified {@code Object}. This method calls {@code getClass()} on the
     * specified {@code Object} and dispatches to
     * {@code removeClassMapping(Class key)}. If {@code obj} is {@code null},
     * then this method returns {@code null}.
     * <p>
     * Removing the mapping for the specified {@code Class} will also remove it
     * for all non-specific subclasses. This means that subclasses of the
     * specified {@code Class} will require specific mappings if the it is
     * desired for the existing mapping behavior for these classes to remain the
     * same.
     * <p>
     * If any instance mappings exist for the specified {@code Class}, they are
     * also removed. This means non-specific subclass instance mappings will
     * also be removed.
     * 
     * @param key
     *            the {@code Class} whose internal mapping will be removed
     * @return the {@code Class} whose mapping has been removed
     * @see #addClassMapping(Class, Class)
     * @see #addClassMapping(Class, Class, Object)
     * @see #getClassMapping(Object)
     * @see #getClassInstance(Class)
     */
    public Class removeClassMapping(Class key) {
        if (key == null)
            return null;

        Class c = null;
        synchronized (classes) {
            c = (Class) classes.remove(key);
        }

        synchronized (instances) {
            instances.remove(key);
        }

        return c;
    }

    /**
     * Returns the {@code Class} associated with the {@code Class} of the
     * specified {@code Object}. If {@code obj} is {@code null}, this method
     * will return the value retrieved from {@code getDefaultMapping()}.
     * Otherwise, this method calls {@code obj.getClass()} and dispatches to
     * {@code getClassMapping(Class key)}.
     * <p>
     * If no mapping has been defined for the specified {@code Class}, then
     * it's superclass is checked, and then that classes' superclass, and so on
     * until {@code java.lang.Object} is reached. If a mapping is found anywhere
     * within the superclass hierarchy, then the mapped {@code Class} is
     * returned. Otherwise, the value returned by {@code getDefaultMapping()} is
     * returned.
     * 
     * @param obj
     *            the {@code Object} whose {@code Class's} internal mapping will
     *            be returned
     * @return the {@code Class} that is mapped internally to the specified key
     *         {@code Class}
     * @see #getDefaultMapping()
     * @see #addClassMapping(Object, Class)
     * @see #removeClassMapping(Object)
     */
    public Class getClassMapping(Object obj) {
        Class key = obj == null ? null : obj.getClass();
        return getClassMapping(key);
    }

    /**
     * Returns the {@code Class} associated with the specified {@code Class}.
     * If {@code key} is {@code null}, this method will return the value
     * retrieved from {@code getDefaultMapping()}. If no mapping has been
     * defined for the specified {@code Class}, then it's superclass is
     * checked, and then that classes' superclass, and so on until
     * {@code java.lang.Object} is reached. If a mapping is found anywhere
     * within the superclass hierarchy, then the mapped {@code Class} is
     * returned. Otherwise, the value returned by {@code getDefaultMapping()} is
     * returned.
     * 
     * @param key
     *            the {@code Class} whose internal mapping will be returned
     * @return the {@code Class} that is mapped internally to the specified
     *         {@code key}
     * @see #getDefaultMapping()
     * @see #addClassMapping(Class, Class)
     * @see #removeClassMapping(Class)
     */
    public Class getClassMapping(Class key) {
        if (key == null)
            return defaultClass;

        Class value = null;

        synchronized (classes) {
            for (Class c = key; c != null && value == null; c = c
                    .getSuperclass()) {
                value = (Class) classes.get(c);
            }
        }

        return value == null ? defaultClass : value;
    }

    /**
     * Returns the {@code Object} instance associated with the specified
     * {@code Class}. If {@code key} is {@code null}, this method will return
     * the value retrieved from {@code getDefaultInstance()}. If no mapping has
     * been defined for the specified {@code Class}, then it's superclass is
     * checked, and then that classes' superclass, and so on until
     * {@code java.lang.Object} is reached. If an instance mapping is found
     * anywhere within the superclass hierarchy, then the mapped {@code Object}
     * is returned. Otherwise, the value returned by
     * {@code getDefaultInstance()} is returned.
     * 
     * @param key
     *            the {@code Class} whose internal mapping will be returned
     * @return the {@code Object} instance that is mapped internally to the
     *         specified {@code key}
     * @see #getDefaultInstance()
     * @see #addClassMapping(Class, Class, Object)
     * @see #removeClassMapping(Class)
     */
    public Object getClassInstance(Class key) {
        if (key == null)
            return defaultInstance;

        Object value = null;

        synchronized (instances) {
            for (Class c = key; c != null && value == null; c = c
                    .getSuperclass()) {
                value = instances.get(c);
            }
        }

        return value == null ? defaultInstance : value;
    }

    /**
     * Returns the default {@code Class} used for situations in which there is
     * no internal class mapping. This property is read-only and is initialized
     * within the {@code ClassMapping} constructor.
     * 
     * @return the default {@code Class} used for situations in which there is
     *         no internal class mapping.
     * @see #ClassMapping(Class, Object)
     */
    public Class getDefaultMapping() {
        return defaultClass;
    }

    /**
     * Returns the default {@code Object} used for situations in which there is
     * no internal instance mapping. This property is read-only and is
     * initialized within the {@code ClassMapping} constructor.
     * 
     * @return the default {@code Object} used for situations in which there is
     *         no internal instance mapping.
     * @see #ClassMapping(Class, Object)
     */
    public Object getDefaultInstance() {
        return defaultInstance;
    }
}

⌨️ 快捷键说明

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