⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quotedlist.java

📁 类似于Windows上的Excel
💻 JAVA
字号:
/*
 * MC2 -- j2me spreadsheet
 *
 * Copyright (c) 2004-2006 Michael Zemljanukha (mixaz@mail.ru)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 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 Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*
 * QuotedList.java
 *
 * Created on December 22, 2003, 7:15 PM
 */

package com.wapindustrial.calc;
import java.util.*;

/** Represents a quoted list '(), derived from FunctorList but acts as an Atom -
 * returns itself on evaluateSExp()
 */
public class QuotedList extends LispObject {
	
	public final LispObject[] value;
    
    /** Creates a QuotedList from LispObject[] array. Array content isn't copied
     * @param value items of quoted list, often taken from LispObject.value
     */    
    public QuotedList( LispObject value[] ) {
        this.value = value;
    }
    
    /** The same as QuotedList( LispObject[] ) constructor but allows to get a part of
     * array. a new array is created
     * @param _value source array of lisp objects
     * @param ind1 start index to copy from
     * @param ind2 end index to copy from, not included
     */    
    public QuotedList( FunctorList fl, int ind1, int ind2 ) {
        this( new LispObject[ind2-ind1] );
        for( int i=ind1; i<ind2; i++ )
            value[i-ind1] = fl.getArgumentN(i);
    }
    
    /** Creates QuotedList from a FunctorList.
     * @param fl a FunctorList
     * @param evaluate TODO
     */    
    public QuotedList( FunctorList fl ) {
        this( fl.toArray() );
    }
    
//    public static QuotedList createdEvaluatedQList( FunctorList fl) throws EvaluateException {
//        QuotedList ql = new QuotedList(fl);
//        for(int i=0; i<ql.value.length; i++) {
//            ql.value[i] = ql.value[i].evaluateSExp();
//        }
//        return ql;
//    }
    
    /*=====================================================================
     * hashtable utils
     *=====================================================================
     */
    
    /** Creates a QuotedList from Hashtable. Often used to write out
     * parts of complicated atoms with toList() from internal Hashtable representation,
     * for instance cells of sheets, etc.
     * @param ht a Hashtable with LispObject instances
     */    
    public QuotedList( Hashtable ht ) {
        int size = ht.size();
        value = new LispObject[size];
        int nn = 0;
        for (Enumeration e = ht.elements() ; e.hasMoreElements() ;) {
            value[nn++] = (LispObject) e.nextElement();
        }
    }
    
    /** Converts QuotedList to Hashtable, evaluates items before adding them to
     * hashtable
     * @throws EvaluateException can be thrown when evaluating items of list
     * @return hashtable of LispObject type evaluated from items of the list.
     */    
    public Hashtable toHashtable() throws EvaluateException {
        Hashtable ht = createHashtable( value.length );
        for( int i=0; i<value.length; i++ ) {
            LispObject lo = value[i].evaluateSExp();
            ht.put( lo, lo );
        }
        return ht;
    }
    // end of hashtable utils
    
    /** Evaluates members of QuotedList with calling evaluateSExp()
     * @throws EvaluateException thrown when evaluating an item
     * @return The last evaluated member of QuotedList
     * @see LispObject#evaluateSExp() evaluateSExp
     */    
    public LispObject interpret() throws EvaluateException {
        LispObject result = LispObject.NIL;
        for( int i=0; i<value.length; i++ )
            result = value[i].interpret();
        return result;
    }
    
    public void toBuffer( StringBuffer sb ) {
        sb.append( '\'' );
        sb.append( '(' );
        for( int i=0; i<value.length; i++ ) {
            if(i!=0)
            	sb.append( ' ' );
            value[i].toBuffer( sb );
        }
        sb.append( ')' );
    }
    
//#ifdef JAVA_COMPILER
    public void toJavaBuffer( StringBuffer sb, int ident, FunctorList parent ) {
        addIdent( sb, ident );
        sb.append( "new QuotedList( new LispObject[] { \n" );
        for( int i=0; i<value.length; i++ ) {
            if( i!=0 ) {
                sb.append( ",\n" );
            }
            value[i].toJavaBuffer( sb, ident+1, null );
        }
//        addIdent( sb, ident );
        sb.append( "} )" );
    }
//#endif
    
//#ifndef MINIMAL_SET                
	/* (non-Javadoc)
	 * @see com.wapindustrial.calc.LispObject#preprocess()
	 */
    public LispObject preprocess(NameObjectBase functor) throws EvaluateException {
		boolean replace = false;
		LispObject newlist[] = new LispObject[value.length];
        for( int i=0; i<value.length; i++ ) {
        	newlist[i] = value[i].preprocess(functor);
        	if(newlist[i] != value[i])
        		replace = true;
        }
        if(replace)
        	return new QuotedList(newlist);
        return this;
	}
//#endif
    
    public final short getShort( int index ) throws EvaluateException {
        LispObject lo = value[index].evaluateSExp();
        if( lo == NIL )
            return 0;
        return ((ShortAtom)lo).value;
    }

    public final long getLong( int index ) throws EvaluateException {
        LispObject lo = value[index].evaluateSExp();
        if( lo == NIL )
            return 0;
        return ((LongAtom)lo).value;
    }

    public final int getInt( int index ) throws EvaluateException {
        LispObject lo = value[index].evaluateSExp();
        if( lo == NIL )
            return 0;
        return (int)((LongAtom)lo).value;
    }

    public final FloatAtom getFloat( int index ) throws EvaluateException {
        LispObject lo = value[index].evaluateSExp();
        if( lo == NIL )
            return null;
        return (FloatAtom)lo;
    }
    
    public final String getString( int index ) throws EvaluateException {
        LispObject lo = value[index].evaluateSExp();
        if( lo == NIL )
            return null;
        if( lo.typeNumber() == TYPE_STRING )
            return ((StringAtomBase)lo).getValue();
        else
            return lo.toString();
    }

    /* (non-Javadoc)
     * @see com.wapindustrial.calc.LispObject#typeNumber()
     */
    public int typeNumber() {
        return TYPE_QUOTEDLIST;
    }
    
    /* (non-Javadoc)
     * @see com.wapindustrial.calc.LispObject#toQuotedList()
     */
    public QuotedList toQuotedList() {
        return this;
    }
    /* (non-Javadoc)
     * @see com.wapindustrial.calc.LispObject#toList()
     */
    public FunctorList toList() {
        return new FunctorListN( 
            Bfunc.BFUNC.table[Bfunc.INDEX_QLIST],
            value
        );
    }
    /* (non-Javadoc)
     * @see com.wapindustrial.calc.LispObject#toFormulaBuffer(java.lang.StringBuffer)
     */
    public void toFormulaBuffer(StringBuffer sb) {
        toList().toFormulaBuffer(sb);
    }
    /* (non-Javadoc)
     * @see com.wapindustrial.calc.LispObject#toValueBuffer(java.lang.StringBuffer, int)
     */
    public void toValueBuffer(StringBuffer sb, int format) {
        toFormulaBuffer(sb);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -