1 package eu.ehri.project.definitions; 2 3 import com.google.common.collect.Lists; 4 5 import java.util.Map; 6 import java.util.MissingResourceException; 7 import java.util.ResourceBundle; 8 import java.util.stream.Collectors; 9 10 public interface DefinitionList { 11 12 Boolean isMultiValued(); 13 String name(); 14 15 static ResourceBundle bundle = ResourceBundle.getBundle("eu.ehri.project.definitions.messages"); 16 17 static Map<String,String> getMap(DefinitionList[] items, Boolean multivalued) { 18 return Lists.newArrayList(items).stream() 19 .filter(i -> multivalued == null || i.isMultiValued() == multivalued) 20 .map(i -> Lists.newArrayList(i.getName(), i.getDescription())) 21 .collect(Collectors.toMap(i -> i.get(0), i -> i.get(1))); 22 } 23 24 static Map<String,String> getMap(DefinitionList[] items) { 25 return getMap(items, null); 26 } 27 28 default String getResourceKey(String key) { 29 try { 30 return bundle.getString(key); 31 } catch (MissingResourceException e) { 32 return "!" + key + "!"; 33 } 34 } 35 36 default String messageKey() { 37 return String.format("%s.%s", getClass().getSimpleName(), name()); 38 } 39 40 default String getName() { 41 return getResourceKey(messageKey()); 42 } 43 44 default String getDescription() { 45 return getResourceKey(String.format("%s.description", messageKey())); 46 } 47 }