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

com.scudata.expression.fn.convert.ToChinese Maven / Gradle / Ivy

Go to download

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

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

import com.scudata.array.IArray;
import com.scudata.array.NumberArray;
import com.scudata.array.StringArray;
import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.common.StringUtils;
import com.scudata.dm.Context;
import com.scudata.expression.Function;
import com.scudata.resources.EngineMessage;

/**
 * ??????תΪ????????д????
 * @author runqian
 *
 */
public class ToChinese extends Function {
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("chn" + mm.getMessage("function.missingParam"));
		} else if (!param.isLeaf()) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("chn" + mm.getMessage("function.invalidParam"));
		}
	}

	public Object calculate(Context ctx) {
		Object result = param.getLeafExpression().calculate(ctx);
		if (result instanceof Number) {
			boolean abbreviate = false, uppercase = false, rmb = false;
			if (option != null) {
				if (option.indexOf('a') != -1) abbreviate = true;
				if (option.indexOf('u') != -1) uppercase = true;
				if (option.indexOf('b') != -1) rmb = true;
			}
	
			if (rmb) {
				double d = ((Number)result).doubleValue();
				return StringUtils.toRMB(d, abbreviate, uppercase);
			} else {
				long l = ((Number)result).longValue();
				return StringUtils.toChinese(l, abbreviate, uppercase);
			}
		} else if (result == null) {
			return null;
		} else {
			MessageManager mm = EngineMessage.get();
			throw new RQException("chn" + mm.getMessage("function.paramTypeError"));
		}
	}
	
	/**
	 * ??????????еĽ??
	 * @param ctx ??????????
	 * @return IArray
	 */
	public IArray calculateAll(Context ctx) {
		IArray array = param.getLeafExpression().calculateAll(ctx);
		int len = array.size();
		String []resultValues = new String[len + 1];
		
		boolean abbreviate = false, uppercase = false, rmb = false;
		if (option != null) {
			if (option.indexOf('a') != -1) abbreviate = true;
			if (option.indexOf('u') != -1) uppercase = true;
			if (option.indexOf('b') != -1) rmb = true;
		}

		if (array instanceof NumberArray) {
			NumberArray numberArray = (NumberArray)array;
			for (int i = 1; i <= len; ++i) {
				if (!array.isNull(i)) {
					if (rmb) {
						double d = numberArray.getDouble(i);
						resultValues[i] = StringUtils.toRMB(d, abbreviate, uppercase);
					} else {
						long l = numberArray.getLong(i);
						resultValues[i] = StringUtils.toChinese(l, abbreviate, uppercase);
					}
				}
			}
		} else {
			for (int i = 1; i <= len; ++i) {
				Object obj = array.get(i);
				if (obj instanceof Number) {
					if (rmb) {
						double d = ((Number)obj).doubleValue();
						resultValues[i] = StringUtils.toRMB(d, abbreviate, uppercase);
					} else {
						long l = ((Number)obj).longValue();
						resultValues[i] = StringUtils.toChinese(l, abbreviate, uppercase);
					}
				} else if (obj != null) {
					MessageManager mm = EngineMessage.get();
					throw new RQException("chn" + mm.getMessage("function.paramTypeError"));
				}
			}
		}
		
		StringArray result = new StringArray(resultValues, len);
		result.setTemporary(true);
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy