org.codehaus.groovy.grails.commons.metaclass.WeakGenericDynamicProperty.java Source code

Java tutorial

Introduction

Here is the source code for org.codehaus.groovy.grails.commons.metaclass.WeakGenericDynamicProperty.java

Source

/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.groovy.grails.commons.metaclass;

import groovy.lang.MissingPropertyException;
import org.apache.commons.collections.map.ReferenceMap;

import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A generic dyanmic property for any type used a soft hashmap implementation for generic properties
 * registered as global in the MetaClass
 * 
 * @author Graeme Rocher
 * @since Oct 27, 2005
 */
public class WeakGenericDynamicProperty extends AbstractDynamicProperty {

    private Class type;
    private boolean readyOnly;
    private Map<String, SoftReference<Object>> propertyToInstanceMap = new ConcurrentHashMap<String, SoftReference<Object>>();
    private Object initialValue;
    private FunctionCallback initialValueGenerator;

    /**
     *
     * @param propertyName The name of the property
     * @param type The type of the property
     * @param initialValue The initial value of the property
     * @param readOnly True for read-only property
     */
    public WeakGenericDynamicProperty(String propertyName, Class type, Object initialValue, boolean readOnly) {
        super(propertyName);
        if (type == null)
            throw new IllegalArgumentException("Constructor argument 'type' cannot be null");
        this.readyOnly = readOnly;
        this.type = type;
        this.initialValue = initialValue;
    }

    /**
     *
     * @param propertyName The name of the property
     * @param type The type of the property
     * @param readOnly True for read-only property
     */
    public WeakGenericDynamicProperty(String propertyName, Class type, boolean readOnly) {
        super(propertyName);
        if (type == null)
            throw new IllegalArgumentException("Constructor argument 'type' cannot be null");
        this.readyOnly = readOnly;
        this.type = type;
    }

    /**
     * <p>Variant that allows supply of a lazy-initialization function for the initial value.</p>
     * <p>This function is called only on the first access to the property for a given object, unless
     * the function returns null in which case it will be called again if another request is made.</p>
     *
     * @param propertyName The name of the property
     * @param type The type of the property
     * @param readOnly True for read-only property
     * @param initialValueGenerator Callback function which will be called for initial value of property
     */
    public WeakGenericDynamicProperty(String propertyName, Class type, FunctionCallback initialValueGenerator,
            boolean readOnly) {
        this(propertyName, type, readOnly);
        this.initialValueGenerator = initialValueGenerator;
    }

    public Object get(Object object) {
        String propertyKey = System.identityHashCode(object) + getPropertyName();

        SoftReference<Object> ref = propertyToInstanceMap.get(propertyKey);
        Object val = (ref != null) ? ref.get() : null;
        if (val != null) {
            return val;
        } else if (this.initialValueGenerator != null) {
            final Object value = this.initialValueGenerator.execute(object);
            propertyToInstanceMap.put(propertyKey, new SoftReference<Object>(value));
            return value;
        } else if (this.initialValue != null) {
            propertyToInstanceMap.put(propertyKey, new SoftReference<Object>(this.initialValue));
            return this.initialValue;
        }
        return null;
    }

    public void set(Object object, Object newValue) {
        if (!readyOnly) {
            if (this.type.isInstance(newValue))
                propertyToInstanceMap.put(String.valueOf(System.identityHashCode(object)) + getPropertyName(),
                        new SoftReference<Object>(newValue));
            else if (newValue != null)
                throw new MissingPropertyException(
                        "Property '" + this.getPropertyName() + "' for object '" + object.getClass()
                                + "' cannot be set with value '" + newValue + "'. Incorrect type.",
                        object.getClass());
        } else {
            throw new MissingPropertyException("Property '" + this.getPropertyName() + "' for object '"
                    + object.getClass() + "' is read-only!", object.getClass());
        }
    }
}