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

org.nuiton.eugene.models.object.xml.ObjectModelClassImpl Maven / Gradle / Ivy

/* *##% 
 * EUGene :: EUGene
 * Copyright (C) 2004 - 2009 CodeLutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * ##%*/

package org.nuiton.eugene.models.object.xml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.nuiton.eugene.models.object.ObjectModelAttribute;
import org.nuiton.eugene.models.object.ObjectModelClass;
import org.nuiton.eugene.models.object.ObjectModelClassifier;
import org.nuiton.eugene.models.object.ObjectModelOperation;

/**
 * ObjectModelClassImpl.
 *
 * Created: 14 janv. 2004
 *
 * @author Cédric Pineau 
 * Copyright Code Lutin
 * @version $Revision: 755 $
 *
 * Last update : $Date: 2009-12-15 00:50:27 +0100 (mar., 15 déc. 2009) $
 * by : $Author: tchemit $
 */
public class ObjectModelClassImpl extends ObjectModelClassifierImpl implements ObjectModelClass {

    protected List superclasses = null;
    protected Map superclassesDiscriminators = new HashMap();
    protected List superclassesRefs = new ArrayList();
    protected List specialisations = null;
    protected List innerClasses = null;
    protected boolean abstractz = false;

    public ObjectModelClassImpl() {
        super();
    }

    public void clearSuperclasses() {
        superclasses = null;
        superclassesRefs.clear();
    }

    public void addSuperclass(ObjectModelImplRef ref) {
        superclassesRefs.add(ref);
        // superclassesRefs is modified, superclasses must be reset
        superclasses = null;
    }

    /**
     * Digester method to add innerClass to this ObjectModelClass.
     * @param innerClass the ObjectModelClass to add
     */
    public void addInnerClassifier(ObjectModelClassifierImpl innerClass) {
        innerClass.setDeclaringElement(this);
        innerClass.setInner(true);
        if (innerClasses == null) {
            innerClasses = new ArrayList();
        }
        innerClasses.add(innerClass);
    }

    

    public void setAbstract(boolean abstractz) {
        this.abstractz = abstractz;
    }

    @Override
    public Collection getSuperclasses() {
        if (superclasses == null) {
            superclasses = new ArrayList();
            for (ObjectModelImplRef superclassesRef : superclassesRefs) {
                ObjectModelImplSuperClassRef ref = (ObjectModelImplSuperClassRef) superclassesRef;
                ObjectModelClass superclass = objectModelImpl.getClass(ref.getName());

                if (superclass == null) {
                    ExternalCacheExtension cache = objectModelImpl.getExtension(
                            ExternalCacheExtension.OBJECTMODEL_EXTENSION, ExternalCacheExtension.class);

                    superclass = cache.getCache(ref, ObjectModelClassImpl.class);
                }

                superclasses.add(superclass);
                superclassesDiscriminators.put(superclass, ref.getDiscriminator());
            }
        }
        return superclasses;
    }

    @Override
    public Collection getInnerClassifiers() {
        return this.innerClasses;
    }

    /**
     * Returns the discriminator for the given superclass.
     * 
     * @return the discriminator for the given superclass as a String if it
     *         exists, null otherwise.
     */
    @Override
    public String getDiscriminator(ObjectModelClass superclass) {
        return superclassesDiscriminators.get(superclass);
    }

    /**
     * Returns all known direct specialized classes for this class.
     * 
     * @see ObjectModelClass
     * 
     * @return a Collection containing all known direct specialized
     *         ObjectModelClass for this class.
     */
    @Override
    public Collection getSpecialisations() {
        if (specialisations == null) {
            specialisations = new ArrayList();
            for (Object o : objectModelImpl.getClasses()) {
                ObjectModelClass candidateClass = (ObjectModelClass) o;
                if (candidateClass.getSuperclasses().contains(this)) {
                    specialisations.add(candidateClass);
                }
            }
        }
        return specialisations;
    }

    /**
     * Returns all known specialized classes for this class for the specified
     * discriminator.
     * 
     * @see ObjectModelClass
     * 
     * @return a Collection containing all known specialized ObjectModelClass
     *         for this class for the specified discriminator.
     */
    @Override
    public Collection getSpecialisations(String discriminator) {
        List discriminatedSpecialisations = new ArrayList();
        for (ObjectModelClass candidateClass : getSpecialisations()) {
            if (discriminator.equals(candidateClass.getDiscriminator(this))) {
                discriminatedSpecialisations.add(candidateClass);
            }
        }
        return discriminatedSpecialisations;
    }

    /**
     * Returns whether this class is abstract or not.
     * 
     * @return a boolean indicating whether this class is abstract or not.
     */
    @Override
    public boolean isAbstract() {
        return abstractz;
    }

//    @Override
//    public boolean isInner() {
//        return inner;
//    }

    @Override
    public Collection getAllOtherOperations(
            boolean distinct) {
        Collection result = getAllInterfaceOperations(distinct);
        getAllSuperclassOperations(result);
        return result;
    }

    @Override
    public Collection getAllSuperclassOperations(
            boolean distinct) {
        Collection result;
        if (distinct) {
            result = new HashSet();
        } else {
            result = new LinkedList();
        }
        getAllSuperclassOperations(result);
        return result;
    }

    protected Collection getAllSuperclassOperations(
            Collection result) {
        for (Object o : getSuperclasses()) {
            ObjectModelClassImpl clazz = (ObjectModelClassImpl) o;
            result.addAll(clazz.getOperations());
            clazz.getAllSuperclassOperations(result);
            clazz.getAllInterfaceOperations(result);
        }
        return result;
    }

    @Override
    public Collection getAllOtherAttributes() {
        Collection result = getAllInterfaceAttributes();
        getAllOtherAttributes(result);
        return result;
    }

    protected Collection getAllOtherAttributes(
            Collection result) {
        for (Object o : getSuperclasses()) {
            ObjectModelClassImpl clazz = (ObjectModelClassImpl) o;
            result.addAll(clazz.getAttributes());
            clazz.getAllOtherAttributes(result);
        }
        return result;
    }

    @Override
    public String toString() {
        StringBuffer result = new StringBuffer();
        result.append("class ").append(getQualifiedName()).append("<<").append(getStereotypes()).append(">> tagvalue: ").append(getTagValues()).append(" ");
        result.append("extends ");
        for (Iterator i = getSuperclasses().iterator(); i.hasNext();) {
            result.append(((ObjectModelClassifier) i.next()).getName());
            if (i.hasNext()) {
                result.append(", ");
            }
        }
        result.append("implements ");
        for (Iterator i = getInterfaces().iterator(); i.hasNext();) {
            result.append(((ObjectModelClassifier) i.next()).getName());
            if (i.hasNext()) {
                result.append(", ");
            }
        }
        return result.toString();
    }

//    /**
//     * Returns whether this classifier is a class or not
//     *
//     * @see ObjectModelClass
//     * 
//     * @return a boolean indicating whether this classifier is a class or not.
//     */
//    @Override
//    public boolean isClass() {
//        return true;
//    }
//
//    /**
//     * Returns whether this classifier is an interface or not
//     *
//     * @see ObjectModelInterface
//     *
//     * @return a boolean indicating whether this classifier is an interface or
//     *         not.
//     */
//    @Override
//    public boolean isInterface() {
//        return false;
//    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy