All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.util.scan.api.metadata.CommonMetadata Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2006-2012 Bull S.A.S.
 *
 * 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.ow2.util.scan.api.metadata;

import java.lang.annotation.ElementType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.ow2.util.scan.api.IMetadataViewFactory;
import org.ow2.util.scan.api.configurator.IViewConfigurator;
import org.ow2.util.scan.api.metadata.structures.IAnnotation;

/**
 * @author Florent Benoit
 */
public abstract class CommonMetadata implements IMetadata {

    /**
     *
     */
    private static final long serialVersionUID = 3745752097459520045L;

    private IMetadata parentMetadata;

    private final List viewConfigurators;

    private final List annotations;

    private final Map, Object> objects;
    private final Map properties;


    public CommonMetadata() {
        this.objects = new ConcurrentHashMap<>();
        this.properties = new ConcurrentHashMap<>();
        this.viewConfigurators = new ArrayList<>();
        this.annotations = new ArrayList<>();
    }

    @Override
    public void addViewConfigurator(IViewConfigurator viewConfigurator) {
        this.viewConfigurators.add(viewConfigurator);
    }

    @Override
    public  void set(Class annotationClass, Annotation annotation) {
        if (annotation == null) {
            return;
        }
        this.objects.put(annotationClass, annotation);
    }

    @Override
    public  Annotation get(Class annotationClass) {
        return annotationClass.cast(this.objects.get(annotationClass));
    }

    @Override
    public  T as(Class clazz) {
        // search a matching view
        for (IViewConfigurator viewConfigurator : viewConfigurators) {
            IMetadataViewFactory metadataView = viewConfigurator.getView(getElementType(), clazz);
            if (metadataView != null) {
                return metadataView.build(this);
            }
        }
        // no matching view
        return null;
    }

    protected abstract ElementType getElementType();

    @SuppressWarnings("unchecked")
    @Override
    public  T getProperty(String name) {
        return (T) properties.get(name);
    }

    @Override
    public boolean getBoolean(String name) {
        Boolean b = getProperty(name);
        if (b == null) {
            return false;
        }
        return b;
    }


    @Override
    public  void setProperty(String name, T value) {
        if (name == null) {
            return;
        }
        // remove the property if null
        if (value == null) {
            if (properties.containsKey(name)) {
                properties.remove(name);
            }
            return;
        }
        properties.put(name, value);
    }


    @Override
    public void setParent(IMetadata parentMetadata) {
        this.parentMetadata = parentMetadata;
    }

    @Override
    public IMetadata getParent() {
        return parentMetadata;
    }


    @Override
    public void addAnnotation(IAnnotation annotation) {
        annotations.add(annotation);
    }

    @Override
    public List getAnnotations() {
        return annotations;
    }

    @Override
    public List getViewConfigurators() {
        return viewConfigurators;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy