Serializing now serializes everything; among many other things:
[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.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Set;
7
8 public class CodeSnippetSupplier<C, S>
9 {
10         private final Map<String, String> standardSnippetIDClassNames = new HashMap<>();
11         private final Set<String> standardSnippetIDSetUnmodifiable = Collections.unmodifiableSet(standardSnippetIDClassNames.keySet());
12         private final Map<String, SnippetDefinintion<C, ?, S>> snippetSuppliersForClassNames = new HashMap<>();
13         private final SnippetDefinintion<C, ?, S> defaultSnippetSupplier;
14
15         public CodeSnippetSupplier(SnippetDefinintion<C, ?, S> defaultSnippetSupplier)
16         {
17                 this.defaultSnippetSupplier = defaultSnippetSupplier;
18         }
19
20         public void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName)
21         {
22                 standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName);
23         }
24
25         public Set<String> getStandardSnippetIDs()
26         {
27                 return standardSnippetIDSetUnmodifiable;
28         }
29
30         public void setSnippetSupplier(String id, SnippetDefinintion<C, ?, S> snippetSupplier)
31         {
32                 snippetSuppliersForClassNames.put(id, snippetSupplier);
33         }
34
35         // TODO report errors
36         public SnippetDefinintion<C, ?, S> getSnippetSupplier(String id)
37         {
38                 if (id != null)
39                 {
40                         String snippetClassName;
41                         if (id.startsWith("class:"))
42                                 snippetClassName = id.substring(6);
43                         else
44                                 snippetClassName = standardSnippetIDClassNames.get(id);
45                         if (snippetClassName != null)
46                         {
47                                 tryLoadSnippetClass(snippetClassName);
48                                 SnippetDefinintion<C, ?, S> snippetSupplier = snippetSuppliersForClassNames.get(snippetClassName);
49                                 if (snippetSupplier != null)
50                                         return snippetSupplier;
51                         }
52                         System.err.println("Couldn't load snippet " + id + "; using default");
53                 }
54                 if (defaultSnippetSupplier == null)
55                         throw new IllegalArgumentException("No default snippet set");
56                 return defaultSnippetSupplier;
57         }
58
59         // static helpers
60
61         private static void tryLoadSnippetClass(String snippetClassName)
62         {
63                 tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
64         }
65
66         public static void tryInvokeStaticInitializer(String className, String errorMessageFormat)
67         {
68                 try
69                 {
70                         Class.forName(className, true, CodeSnippetSupplier.class.getClassLoader());
71                 }
72                 catch (ClassNotFoundException e)
73                 {
74                         System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage());
75                 }
76         }
77
78 }