Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / SnippetDefinintion.java
1 package net.mograsim.logic.model.snippets;
2
3 import java.util.function.BiFunction;
4
5 import com.google.gson.Gson;
6 import com.google.gson.JsonElement;
7
8 public interface SnippetDefinintion<C, P, S>
9 {
10         public Class<P> getParamClass();
11
12         public S create(C context, P params);
13
14         public default S create(C context, JsonElement params)
15         {
16                 Class<P> paramClass = getParamClass();
17                 if (paramClass.equals(Void.class))
18                 {
19                         if (params != null)
20                                 throw new IllegalArgumentException("Params given where none were expected");
21                         return create(context, (P) null);
22                 }
23                 return create(context, new Gson().fromJson(params, getParamClass()));
24         }
25
26         public static <C, P, S> SnippetDefinintion<C, P, S> create(Class<P> paramClass, BiFunction<C, P, S> supplier)
27         {
28                 return new SnippetDefinintion<>()
29                 {
30                         @Override
31                         public Class<P> getParamClass()
32                         {
33                                 return paramClass;
34                         }
35
36                         @Override
37                         public S create(C context, P params)
38                         {
39                                 return supplier.apply(context, params);
40                         }
41                 };
42         }
43 }