MPROMEditor now calls its columns "Opcode" and "muPC"
[Mograsim.git] / plugins / 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.JsonElement;
6
7 import net.mograsim.logic.model.util.JsonHandler;
8
9 public interface SnippetDefinintion<C, P, S>
10 {
11         public Class<P> getParamClass();
12
13         public S create(C context, P params);
14
15         public default S create(C context, JsonElement params)
16         {
17                 Class<P> paramClass = getParamClass();
18                 if (paramClass.equals(Void.class))
19                 {
20                         if (params != null)
21                                 throw new IllegalArgumentException("Params given where none were expected");
22                         return create(context, (P) null);
23                 }
24                 return create(context, JsonHandler.fromJson(params, getParamClass()));
25         }
26
27         public static <C, P, S> SnippetDefinintion<C, P, S> create(Class<P> paramClass, BiFunction<C, P, S> supplier)
28         {
29                 return new SnippetDefinintion<>()
30                 {
31                         @Override
32                         public Class<P> getParamClass()
33                         {
34                                 return paramClass;
35                         }
36
37                         @Override
38                         public S create(C context, P params)
39                         {
40                                 return supplier.apply(context, params);
41                         }
42                 };
43         }
44 }