| Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |||||
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 3 | import java.util.HashMap; |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 4 | import java.util.Map; |
| 5 | import java.util.Set; | ||||
| 6 | |||||
| Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 7 | /** |
| 8 | * @author hanl | ||||
| 9 | * @date 17/06/2015 | ||||
| 10 | */ | ||||
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 11 | public class DefaultHandler { |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 12 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 13 | private Map<String, Object> defaults; |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 14 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 15 | |
| 16 | public DefaultHandler () { | ||||
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 17 | this.defaults = new HashMap<>(); |
| 18 | loadClasses(); | ||||
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 19 | } |
| 20 | |||||
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 21 | |
| 22 | private void loadClasses () { | ||||
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 23 | Set<Class<?>> cls = KustvaktClassLoader |
| 24 | .loadFromAnnotation(Configurable.class); | ||||
| 25 | for (Class clazz : cls) { | ||||
| 26 | Configurable c = (Configurable) clazz | ||||
| 27 | .getAnnotation(Configurable.class); | ||||
| 28 | try { | ||||
| 29 | this.defaults.put(c.value(), clazz.newInstance()); | ||||
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 30 | } |
| 31 | catch (InstantiationException | IllegalAccessException e) { | ||||
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 32 | throw new RuntimeException("Could not instantiate class"); |
| 33 | } | ||||
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 34 | } |
| 35 | } | ||||
| 36 | |||||
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 37 | |
| 38 | public Object getDefault (String name) { | ||||
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 39 | return this.defaults.get(name); |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 40 | } |
| 41 | |||||
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 42 | |
| 43 | public <T> T getDefault (Class<T> tClass) { | ||||
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 44 | for (Object o : this.defaults.values()) { |
| 45 | if (o.getClass().equals(tClass)) | ||||
| 46 | return (T) o; | ||||
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 47 | } |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 48 | return null; |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 49 | } |
| 50 | |||||
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 51 | |
| 52 | public void remove (String name) { | ||||
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 53 | this.defaults.remove(name); |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 54 | } |
| 55 | |||||
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 56 | |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 57 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 58 | public String toString () { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 59 | return defaults.toString(); |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 60 | } |
| Michael Hanl | 704211e | 2015-06-19 07:26:04 +0200 | [diff] [blame] | 61 | } |