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

org.jclarion.clarion.runtime.expr.ConcatExpr Maven / Gradle / Ivy

There is a newer version: 1.86
Show newest version
/**
 * Copyright 2010, by Andrew Barnham
 *
 * The contents of this file are subject to
 * GNU Lesser General Public License (LGPL), v.3
 * http://www.gnu.org/licenses/lgpl.txt
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 */
package org.jclarion.clarion.runtime.expr;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jclarion.clarion.Clarion;
import org.jclarion.clarion.ClarionObject;

public class ConcatExpr extends CExpr {
    
    public static CExpr construct(CExpr left,CExpr right) {
        if (!(left instanceof ConcatExpr)) {
            left=new ConcatExpr(left);
        }
        ((ConcatExpr)left).add(right);
        return left;
    }
    
    private CExpr left;
    private List right;
    
    public ConcatExpr(CExpr left)
    {
        this.left=left;
        right=new ArrayList();
    }
    
    public void add(CExpr right)
    {
        this.right.add(right);
    }

    @Override
    public ClarionObject eval() {
        
        ClarionObject result = left.eval();
        
        for ( CExpr rhs : right ) {
            result=Clarion.newString(result.concat(rhs.eval()));
        }
        
        return result;
    }

    @Override
    public int precendence() {
        return 4;
    }

    @Override
    public Iterator directChildren() {
        return new JoinIterator(left,right);
    }

    /**
     *  Strictly speaking - SQL yielded will not be equivalent to runtime
     *  evaluation = because of ClarionString insistance on being padded
     *  to its memory defined length. Oh - well - soldier on...
     */
    @Override
    public boolean generateString(StringBuilder out, boolean strict) {
        
        int startPos=out.length();
        
        CExpr curr=left;
        Iterator scan = right.iterator();
        
        while (curr!=null) {
            if (out.length()!=startPos) {
                out.append("||");
            }
            if (curr.precendence()




© 2015 - 2025 Weber Informatics LLC | Privacy Policy