From 79ebc066bb2b34f05ea471cc627c7387add93d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Wolski?= Date: Tue, 11 Apr 2017 09:24:58 +0200 Subject: [PATCH] add NanoTimeFunctionElement - provides System.nanoTime to measure elapsed time --- .../plugins/time/NanoTimeFunctionElement.java | 50 +++++++++++++++++++ .../engine/plugins/time/TimePlugin.java | 3 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 org.coreasm.engine/src/org/coreasm/engine/plugins/time/NanoTimeFunctionElement.java diff --git a/org.coreasm.engine/src/org/coreasm/engine/plugins/time/NanoTimeFunctionElement.java b/org.coreasm.engine/src/org/coreasm/engine/plugins/time/NanoTimeFunctionElement.java new file mode 100644 index 00000000..f90298e1 --- /dev/null +++ b/org.coreasm.engine/src/org/coreasm/engine/plugins/time/NanoTimeFunctionElement.java @@ -0,0 +1,50 @@ +/* + * NanoTimeFunctionElement.java 1.0 + * + * Copyright (C) 2006 Roozbeh Farahbod + * + * Last modified by $Author: rfarahbod $ on $Date: 2011-03-29 02:05:21 +0200 (Di, 29 Mrz 2011) $. + * + * Licensed under the Academic Free License version 3.0 + * http://www.opensource.org/licenses/afl-3.0.php + * http://www.coreasm.org/afl-3.0.php + * + */ + +package org.coreasm.engine.plugins.time; + +import org.coreasm.engine.absstorage.Element; +import org.coreasm.engine.absstorage.FunctionElement; +import org.coreasm.engine.plugins.number.NumberElement; + +import java.util.List; + +/** + * Implements 'nanoTime' as a monitored function that returns the current value of the running Java Virtual Machine's + * high-resolution time source, in nanoseconds. + * + * @author André Wolski + * + */ +public class NanoTimeFunctionElement extends FunctionElement { + + /** Name of this function */ + public static final String NANOTIME_FUNC_NAME = "nanoTime"; + + public NanoTimeFunctionElement() { + super(); + setFClass(FunctionClass.fcMonitored); + } + + /* (non-Javadoc) + * @see org.coreasm.engine.absstorage.FunctionElement#getValue(java.util.List) + */ + @Override + public Element getValue(List args) { + if (args.size() > 0) + return Element.UNDEF; + else + return NumberElement.getInstance(System.nanoTime()); + } + +} diff --git a/org.coreasm.engine/src/org/coreasm/engine/plugins/time/TimePlugin.java b/org.coreasm.engine/src/org/coreasm/engine/plugins/time/TimePlugin.java index 81c35c4b..c95fcc21 100644 --- a/org.coreasm.engine/src/org/coreasm/engine/plugins/time/TimePlugin.java +++ b/org.coreasm.engine/src/org/coreasm/engine/plugins/time/TimePlugin.java @@ -37,7 +37,7 @@ */ public class TimePlugin extends Plugin implements VocabularyExtender { - public static final VersionInfo VERSION_INFO = new VersionInfo(0, 2, 0, ""); + public static final VersionInfo VERSION_INFO = new VersionInfo(0, 3, 0, ""); private final Set dependencyList; @@ -75,6 +75,7 @@ public Map getFunctions() { if (functions == null) { functions = new HashMap(); functions.put(NowFunctionElement.NOW_FUNC_NAME, new NowFunctionElement()); + functions.put(NanoTimeFunctionElement.NANOTIME_FUNC_NAME, new NanoTimeFunctionElement()); functions.put(StepCountFunctionElement.FUNC_NAME, new StepCountFunctionElement(capi)); } return functions;