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

import java.util.ArrayList;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.mdr.storagemodel.StorableObject;
import org.netbeans.modules.javacore.parser.ASTProvider;
import java.util.List;

/**
 *
 * @author  mm109185
 */
public abstract class AssignmentImpl extends ExpressionImpl implements Assignment {
    private PrimaryExpression leftSide = null;
    private Operator operator = null;
    private Expression rightSide = null;
    
    /** Creates a new instance of AssignmentImpl */
    public AssignmentImpl(StorableObject o) {
        super(o);
    }
    
    public void setLeftSide(PrimaryExpression expression) {
        objectChanged(CHANGED_LEFT_SIDE);
        changeChild(getLeftSide(), expression);
        this.leftSide = expression;
    }
    
    public PrimaryExpression getLeftSide() {
        if (!childrenInited) {
            initChildren();
        }
        return leftSide;
    }
    
    public void setRightSide(Expression expression) {
        objectChanged(CHANGED_RIGHT_SIDE);
        changeChild(getRightSide(), expression);
        this.rightSide = expression;
    }
    
    public Expression getRightSide() {
        if (!childrenInited) {
            initChildren();
        }
        return rightSide;
    }
    
    public void setOperator(Operator operator) {
        objectChanged(CHANGED_OPERATOR);
        this.operator = operator;
    }
    
    public Operator getOperator() {
        if (isChanged(CHANGED_OPERATOR)) {
            return operator;
        } else {
            return getOperator(getASTree().getSubTrees()[1].getType());
        }
    }
    
    public List getChildren() {
        List list = new ArrayList(2);
        addIfNotNull(list, getLeftSide()); 
        addIfNotNull(list, getRightSide()); 
        return list;
    }

    protected void initChildren() {
        childrenInited = false;
        ASTree tree = getASTree();
        if (tree != null) {
            ASTree[] parts = tree.getSubTrees();
            leftSide = (PrimaryExpression) initOrCreate(leftSide, parts[0]);
            rightSide = (Expression) initOrCreate(rightSide, parts[2]);
        }
        childrenInited = true;
    }
    
    public String getSourceText() {
        String origElem;
        if ((origElem = checkChange()) != null)
            return origElem;
        StringBuffer buf = new StringBuffer();
        StatementImpl leftSide = (StatementImpl) getLeftSide();
        StatementImpl rightSide = (StatementImpl) getRightSide();
        Operator operator = getOperator();
        buf.append(getIndentation());
        buf.append(leftSide.getSourceText());
        buf.append(" "); // NOI18N
        buf.append(operatorToString(operator));
        buf.append(" "); // NOI18N
        buf.append(rightSide.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) getLeftSide(), CHANGED_LEFT_SIDE);
        if (isChanged(CHANGED_OPERATOR)) {
            replaceNode(diff, parser, children[1], operatorToString(getOperator()), 0, null);
        }
        getChildDiff(diff, parser, children[2], (MetadataElement) getRightSide(), CHANGED_RIGHT_SIDE);
    }

    private String operatorToString (Operator op) {
        if (op == OperatorEnum.PLUSASSIGN) return "+="; // NOI18N
        if (op == OperatorEnum.MINUSASSIGN) return "-="; // NOI18N
        if (op == OperatorEnum.PLUSASSIGN) return "*="; // NOI18N
        if (op == OperatorEnum.DIVASSIGN) return "/="; // NOI18N
        if (op == OperatorEnum.ANDASSIGN) return "&="; // NOI18N
        if (op == OperatorEnum.ORASSIGN) return "|="; // NOI18N
        if (op == OperatorEnum.XORASSIGN) return "^="; // ?? // NOI18N
        if (op == OperatorEnum.MODASSIGN) return "%="; // NOI18N
        if (op == OperatorEnum.PLUSASSIGN) return "+="; // NOI18N
        if (op == OperatorEnum.LSHIFTASSIGN) return "<<="; // NOI18N
        if (op == OperatorEnum.RSHIFTASSIGN) return ">>="; // NOI18N
        if (op == OperatorEnum.URSHIFTASSIGN) return ">>>="; // NOI18N
        if (op == OperatorEnum.ASSIGN) return "="; // NOI18N
        
        return "???"; // [PENDING] // NOI18N
    }
    
    void setData(PrimaryExpression leftSide, Operator operator, Expression rightSide) {
        changeChild(null, leftSide);
        this.leftSide = leftSide;
        this.operator = operator;
        changeChild(null, rightSide);
        this.rightSide = rightSide;
    }

    protected void _delete() {
        // --- delete components -------------------------------------------
        if (childrenInited) {
            deleteChild(leftSide);
            deleteChild(rightSide);
        }
        // --- delete links -----------------------------------------------
        // no links to delete
        // --- call super ---------------------------------------
        super._delete();
    }
    
    public void replaceChild(Element oldElement,Element newElement) {
        if (childrenInited) {
            if (oldElement.equals(leftSide)) {
                setLeftSide((PrimaryExpression)newElement);
            }
            if (oldElement.equals(rightSide)) {
                setRightSide((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.