View Javadoc
1   package org.djutils.eval;
2   
3   import org.djutils.metadata.MetaData;
4   
5   /**
6    * F1.java. Minimal implementation of one-argument Function with description.
7    * <p>
8    * Copyright (c) 2023-2024 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 F1 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 OneArgumentFunction f1;
28  
29      /**
30       * Construct a new one-argument function.
31       * @param id String; name of the function as it must be written in expressions
32       * @param resultClass Class&lt;?&gt;; the type of the result of the function
33       * @param metaData MetaData; meta data of the function
34       * @param f1 OneArgumentFunction; one argument function
35       */
36      F1(final String id, final Class<?> resultClass, final MetaData metaData, final OneArgumentFunction f1)
37      {
38          this.id = id;
39          this.resultClass = resultClass;
40          this.metaData = metaData;
41          this.f1 = f1;
42      }
43  
44      @Override
45      public String getId()
46      {
47          return this.id;
48      }
49  
50      @Override
51      public MetaData getMetaData()
52      {
53          return this.metaData;
54      }
55  
56      @Override
57      public Object function(final Object[] arguments) throws RuntimeException
58      {
59          return this.f1.execute(this, arguments[0]);
60      }
61  
62      /**
63       * Interface for one argument functions.
64       */
65      interface OneArgumentFunction
66      {
67          /**
68           * Prototype of the one-argument function
69           * @param functionData Function; meta data of the function
70           * @param argument Object; the argument of the function
71           * @return Object; the result type of the function
72           */
73          Object execute(Function functionData, Object argument);
74  
75      }
76  }