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-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.jmiimpl.javamodel;

import java.util.*;
import org.netbeans.jmi.javamodel.ConditionalExpression;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.Expression;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ASTProvider;

/**
 *
 * @author  Martin Matula
 */
public abstract class ConditionalExpressionImpl extends ExpressionImpl implements ConditionalExpression {
    private Expression condition = null;
    private Expression truePart = null;
    private Expression falsePart = null;
    
    /** Creates a new instance of ConditionalExpressionImpl */
    public ConditionalExpressionImpl(StorableObject o) {
        super(o);
    }
    
    public void setCondition(Expression condition) {
        objectChanged(CHANGED_CONDITION);
        changeChild(getCondition(), condition);
        this.condition = condition;
    }
    
    public Expression getCondition() {
        if (!childrenInited) {
            initChildren();
        }
        return condition;
    }
    
    public void setTruePart(Expression truePart) {
        objectChanged(CHANGED_TRUE_PART);
        changeChild(getTruePart(), truePart);
        this.truePart = truePart;
    }
    
    public Expression getTruePart() {
        if (!childrenInited) {
            initChildren();
        }
        return truePart;
    }
    
    public void setFalsePart(Expression falsePart) {
        objectChanged(CHANGED_FALSE_PART);
        changeChild(getFalsePart(), falsePart);
        this.falsePart = falsePart;
    }
    
    public Expression getFalsePart() {
        if (!childrenInited) {
            initChildren();
        }
        return falsePart;
    }
    
    public List getChildren() {
        List list = new ArrayList(3);
        addIfNotNull(list, getCondition()); 
        addIfNotNull(list, getTruePart()); 
        addIfNotNull(list, getFalsePart()); 
        return list;
    }
    
    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            ASTree[] parts = tree.getSubTrees();
            condition = (Expression) initOrCreate(condition, parts[0]);
            truePart = (Expression) initOrCreate(truePart, parts[1]);
            falsePart = (Expression) initOrCreate(falsePart, parts[2]);
        }
        childrenInited = true;
    }
    
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        StringBuffer buf = new StringBuffer();
        StatementImpl cond = (StatementImpl) getCondition();
        StatementImpl falsePart = (StatementImpl) getFalsePart();
        StatementImpl truePart = (StatementImpl) getTruePart();
        buf.append(cond.getSourceText());
        formatElementPart(COND_EXPR_QUESTION, buf);
        buf.append(truePart.getSourceText());
        formatElementPart(COND_EXPR_COLON, buf);
        buf.append(falsePart.getSourceText());
        return buf.toString();
    }
        
    public void getDiff(List diff) {
        ASTProvider parser = getParser();
        ASTree tree = getASTree();
        ASTree[] children = tree.getSubTrees();

        getChildDiff(diff, parser, children[0], (MetadataElement) getCondition(), CHANGED_CONDITION);
        getChildDiff(diff, parser, children[1], (MetadataElement) getTruePart(), CHANGED_TRUE_PART);
        getChildDiff(diff, parser, children[0], (MetadataElement) getFalsePart(), CHANGED_FALSE_PART);
    }
    
    void setData(Expression condition, Expression truePart, Expression falsePart) {
        changeChild(null, condition);
        this.condition = condition;
        changeChild(null, truePart);
        this.truePart = truePart;
        changeChild(null, falsePart);
        this.falsePart = falsePart;
    }

    protected void _delete() {
        // --- delete components -------------------------------------------
        if (childrenInited) {
            deleteChild(condition);
            deleteChild(truePart);
            deleteChild(falsePart);
        }
        // --- delete links -----------------------------------------------
        // no links to delete
        // --- call super ---------------------------------------
        super._delete();
    }
    
    public void replaceChild(Element oldElement,Element newElement) {
        if (childrenInited) {
            if (oldElement.equals(condition)) {
                setCondition((Expression)newElement);
            } else if (oldElement.equals(truePart)) {
                setTruePart((Expression)newElement);
            } else if (oldElement.equals(falsePart)) {
                setFalsePart((Expression)newElement);
            }
        }
    }
}
... 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.