]> gerrit.simantics Code Review - simantics/platform.git/blob
07ca39015fb4f363f1d1f8484ece89ebbb5fb12f
[simantics/platform.git] /
1 package org.simantics.scl.compiler.commands;
2
3 import gnu.trove.map.hash.THashMap;
4
5 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
6 import org.simantics.scl.compiler.top.ExpressionEvaluator;
7 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
8 import org.simantics.scl.compiler.types.Type;
9 import org.simantics.scl.compiler.types.Types;
10 import org.simantics.scl.runtime.function.Function1;
11
12 public class ValueToStringConverter {
13     final RuntimeEnvironment environment;
14     final THashMap<Type, Function1> showInstances = new THashMap<Type, Function1>(); 
15
16     public ValueToStringConverter(RuntimeEnvironment environment) {
17         this.environment = environment;
18     }
19
20     public String show(Object value, Type type) throws SCLExpressionCompilationException {
21         Function1 showInstance = showInstances.get(type);
22         if(showInstance == null) {
23             showInstance = (Function1)new ExpressionEvaluator(environment, "show")
24             .expectedType(Types.function(type, Types.STRING))
25             .eval();
26             showInstances.put(type, showInstance); 
27         }
28         return (String)showInstance.apply(value);
29     }
30 }