1 package org.djutils.eval;
2
3 import org.djutils.metadata.MetaData;
4
5 /**
6 * F0.java. Minimal implementation of zero-argument Function with description.
7 * <p>
8 * Copyright (c) 2023-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
10 * distributed under a three-clause BSD-style license, which can be found at
11 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
12 * </p>
13 * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14 */
15 class F0 implements Function
16 {
17 /** Id of the function. */
18 final String id;
19
20 /** Type of the parameters of this function (also contains name and description). */
21 final MetaData metaData;
22
23 /** Type of the result of this function. */
24 final Class<?> resultClass;
25
26 /** The zero argument function. */
27 final ZeroArgumentFunction f0;
28
29 /**
30 * Construct a new zero argument function.
31 * @param id name of the function as it must be written in expressions
32 * @param resultClass the type of the result of the function
33 * @param metaData meta data of the function
34 * @param f0 zero argument function
35 */
36 F0(final String id, final Class<?> resultClass, final MetaData metaData, final ZeroArgumentFunction f0)
37 {
38 this.id = id;
39 this.resultClass = resultClass;
40 this.metaData = metaData;
41 this.f0 = f0;
42 }
43
44 /**
45 * Construct a new zero-argument function with constant result.
46 * @param id name of the function as it must be written in expressions
47 * @param constantResult the result of the zero argument function
48 * @param metaData meta data of the function
49 */
50 F0(final String id, final Object constantResult, final MetaData metaData)
51 {
52 this(id, constantResult.getClass(), metaData, (f) -> constantResult);
53 }
54
55 @Override
56 public String getId()
57 {
58 return this.id;
59 }
60
61 @Override
62 public MetaData getMetaData()
63 {
64 return this.metaData;
65 }
66
67 @Override
68 public Object function(final Object[] arguments) throws RuntimeException
69 {
70 return this.f0.execute(this);
71 }
72
73 /**
74 * Interface for zero argument functions.
75 */
76 interface ZeroArgumentFunction
77 {
78 /**
79 * Prototype of the zero-argument function
80 * @param functionData meta data of the function
81 * @return the result type of the function
82 */
83 Object execute(Function functionData);
84 }
85
86 }