alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.parser;

import java.util.List;
import java.lang.reflect.Modifier;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.ParameterizedType;
import org.netbeans.modules.javacore.jmiimpl.javamodel.JavaClassImpl;

/**
 *
 * @author  Tomas Hurka
 */
class ClassInfoMeasure implements Measure {
    public static final ClassInfoMeasure INSTANCE = new ClassInfoMeasure();

    private static final int TYPE_WEIGHT = 100;
    private static final int NAME_WEIGHT = 60;
    private static final int EXTENDS_WEIGHT = 10;
    private static final int IMPLEMENTS_WEIGHT = 10;
    private static final int MEMBERS_WEIGHT = 20;
    
    /** Creates a new instance of ClassMeasure */
    private ClassInfoMeasure() {
    }
    
    /** Compares JavaClass to ASTree object.
     * @param refObject Must be JavaClass
     * @param asTree
     * @return distance
     */
    public int getDistance(Object refObject, Object ast) {
        if (refObject instanceof ParameterizedType)
            refObject=((ParameterizedType)refObject).getDefinition();
        
        if (ast instanceof ClassInfo) {
            JavaClassImpl refInfo = (JavaClassImpl) refObject;
            ClassInfo astInfo = (ClassInfo) ast;
            int result = 0;

            if (refInfo.isInterface() != Modifier.isInterface(astInfo.modifiers)) {
                result = TYPE_WEIGHT ;
            }
            result += StringMeasure.INSTANCE.getDistance(refInfo.getName(), astInfo.name) * NAME_WEIGHT;
            
            if (refInfo.isPersisted()) {
                result += ClassNameMeasure.INSTANCE.getDistance(refInfo.getSuperclassRef(), astInfo.superclass) * EXTENDS_WEIGHT;
                List ifcRefs = refInfo.getInterfaceRefs();
                result += new ArrayMeasure(ClassNameMeasure.INSTANCE).getDistance(ifcRefs == null ? new Object[0] : ifcRefs.toArray(), astInfo.interfaces) * IMPLEMENTS_WEIGHT;
                String[] refMemberNames = InfoUtil.getElementNames(refInfo.getFeatures());
                String[] astMemberNames = InfoUtil.getElementNames(astInfo.features);
                result += new ArrayMeasure(StringMeasure.INSTANCE).getDistance(refMemberNames, astMemberNames) * MEMBERS_WEIGHT;
            }
            result = result / 100;
            return result > INFINITE_DISTANCE ? INFINITE_DISTANCE : result;
        } else if (ast instanceof TypeParamRef) {
            if (((JavaClass) refObject).getName().equals(((TypeParamRef)ast).getName()))
                return 0;
            return INFINITE_DISTANCE;
        }
        return INFINITE_DISTANCE;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.