Restructured serializing/deserializing
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / CodeSnippetSupplier.java
1 package net.mograsim.logic.model.snippets;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 public class CodeSnippetSupplier<C, S>
7 {
8         private final Map<String, String> standardSnippetIDClassNames = new HashMap<>();
9         private final Map<String, SnippetDefinintion<C, ?, S>> snippetSuppliersForClassNames = new HashMap<>();
10         private final SnippetDefinintion<C, ?, S> defaultSnippetSupplier;
11
12         public CodeSnippetSupplier(SnippetDefinintion<C, ?, S> defaultSnippetSupplier)
13         {
14                 this.defaultSnippetSupplier = defaultSnippetSupplier;
15         }
16
17         public void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName)
18         {
19                 standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName);
20         }
21
22         public void setSnippetSupplier(String id, SnippetDefinintion<C, ?, S> snippetSupplier)
23         {
24                 snippetSuppliersForClassNames.put(id, snippetSupplier);
25         }
26
27         // TODO report errors
28         public SnippetDefinintion<C, ?, S> getSnippetSupplier(String id)
29         {
30                 if (id != null)
31                 {
32                         String snippetClassName;
33                         if (id.startsWith("class:"))
34                                 snippetClassName = id.substring(6);
35                         else
36                                 snippetClassName = standardSnippetIDClassNames.get(id);
37                         if (snippetClassName != null)
38                         {
39                                 tryLoadSnippetClass(snippetClassName);
40                                 SnippetDefinintion<C, ?, S> snippetSupplier = snippetSuppliersForClassNames.get(snippetClassName);
41                                 if (snippetSupplier != null)
42                                         return snippetSupplier;
43                         }
44                         System.err.println("Couldn't load snippet " + id + "; using default");
45                 }
46                 if (defaultSnippetSupplier == null)
47                         throw new IllegalArgumentException("No default snippet set");
48                 return defaultSnippetSupplier;
49         }
50
51         // static helpers
52
53         private static void tryLoadSnippetClass(String snippetClassName)
54         {
55                 tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
56         }
57
58         public static void tryInvokeStaticInitializer(String className, String errorMessageFormat)
59         {
60                 try
61                 {
62                         Class.forName(className, true, CodeSnippetSupplier.class.getClassLoader());
63                 }
64                 catch (ClassNotFoundException e)
65                 {
66                         System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage());
67                 }
68         }
69
70 }