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

com.scudata.expression.fn.CellName Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20240823
Show newest version
package com.scudata.expression.fn;

import com.scudata.array.ConstArray;
import com.scudata.array.IArray;
import com.scudata.array.ObjectArray;
import com.scudata.common.CellLocation;
import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.expression.Expression;
import com.scudata.expression.Function;
import com.scudata.expression.IParam;
import com.scudata.resources.EngineMessage;

/**
 * ???С??кţ???1??ʼ??????????Excel????????
 * cellname(r, c)
 * @author RunQian
 *
 */
public class CellName extends Function {
	private Expression rowExp;
	private Expression colExp;
	
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("cellname" + mm.getMessage("function.missingParam"));
		} else if (param.getSubSize() != 2) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("cellname" + mm.getMessage("function.invalidParam"));
		}
		
		IParam rowParam = param.getSub(0);
		IParam colParam = param.getSub(1);
		if (rowParam == null || colParam == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("cellname" + mm.getMessage("function.invalidParam"));
		}
		
		rowExp = rowParam.getLeafExpression();
		colExp = colParam.getLeafExpression();
	}

	public Object calculate(Context ctx) {
		Object row = rowExp.calculate(ctx);
		if (!(row instanceof Number)) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("cellname" + mm.getMessage("function.paramTypeError"));
		}
		
		Object col = colExp.calculate(ctx);
		if (!(col instanceof Number)) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("cellname" + mm.getMessage("function.paramTypeError"));
		}
		
		return CellLocation.getCellId(((Number)row).intValue(), ((Number)col).intValue());
	}

	/**
	 * ??????????еĽ??
	 * @param ctx ??????????
	 * @return IArray
	 */
	public IArray calculateAll(Context ctx) {
		IArray rowArray = rowExp.calculateAll(ctx);
		IArray colArray = colExp.calculateAll(ctx);
		
		if (!rowArray.isNumberArray() || !colArray.isNumberArray()) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("cellname" + mm.getMessage("function.paramTypeError"));
		}
		
		int len = rowArray.size();		
		if (rowArray instanceof ConstArray && colArray instanceof ConstArray) {
			if (rowArray.isNull(1) || colArray.isNull(1)) {
				MessageManager mm = EngineMessage.get();
				throw new RQException("cellname" + mm.getMessage("function.paramTypeError"));
			}
			
			int row = ((ConstArray)rowArray).getInt(1);
			int col = ((ConstArray)colArray).getInt(1);
			String id = CellLocation.getCellId(row, col);
			return new ConstArray(id, len);
		} else {
			ObjectArray result = new ObjectArray(len);
			result.setTemporary(true);
			
			for (int i = 1; i < len; ++i) {
				if (rowArray.isNull(i) || colArray.isNull(i)) {
					result.push(null);
				} else {
					int row = rowArray.getInt(i);
					int col = colArray.getInt(i);
					String id = CellLocation.getCellId(row, col);
					result.push(id);
				}
			}
			
			return result;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy