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