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

com.scudata.expression.fn.Mode 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 java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.expression.Function;
import com.scudata.expression.IParam;
import com.scudata.resources.EngineMessage;

/**
 * ȡ??????/????г??ִ??????ij?Ա??
 * @author runqian
 *
 */
public class Mode extends Function {
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("mode" + mm.getMessage("function.missingParam"));
		}
	}

	public Object calculate(Context ctx) {
		IParam param = this.param;
		if (param.isLeaf()) {
			return param.getLeafExpression().calculate(ctx);
		} else {
			int size = param.getSubSize();
			HashMap map = new HashMap(size);
			for (int i = 0; i < size; ++i) {
				IParam sub = param.getSub(i);
				if (sub != null) {
					Object obj = sub.getLeafExpression().calculate(ctx);
					if (obj != null) {
						Integer n = map.get(obj);
						if (n == null) {
							map.put(obj, 1);
						} else {
							map.put(obj, n + 1);
						}
					}
				}
			}

			Object result = null;
			int count = 0;
			Set> entrySet = map.entrySet();
			Iterator> itr = entrySet.iterator();
			while (itr.hasNext()) {
				Map.Entry entry = itr.next();
				if (entry.getValue() > count) {
					result = entry.getKey();
					count = entry.getValue();
				}
			}
			
			return result;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy