Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Map.Entry;
  4. import java.util.Properties;
  5.  
  6.  
  7. public class PropertiesVerifier
  8. {
  9. private final Map<String, PropertyInfo> optionalInfo;
  10. private final Map<String, PropertyInfo> requiredInfo;
  11.  
  12. {
  13. optionalInfo = new HashMap<String, PropertyInfo>();
  14. requiredInfo = new HashMap<String, PropertyInfo>();
  15. }
  16.  
  17. public PropertiesVerifier(final PropertyInfo[] infos)
  18. {
  19. for(final PropertyInfo info : infos)
  20. {
  21. final Map<String, PropertyInfo> infoMap;
  22.  
  23. if(info.isRequired())
  24. {
  25. infoMap = requiredInfo;
  26. }
  27. else
  28. {
  29. infoMap = optionalInfo;
  30. }
  31.  
  32. infoMap.put(info.getName(), info);
  33. }
  34. }
  35.  
  36. public void verifyProperties(final Properties properties)
  37. {
  38. for(final Entry<Object, Object> property : properties.entrySet())
  39. {
  40. final String key;
  41. final String value;
  42.  
  43. key = (String)property.getKey();
  44. value = (String)property.getValue();
  45.  
  46. if(!(isValid(key, value)))
  47. {
  48. throw new IllegalArgumentException(value + " is not valid for: " + key);
  49. }
  50. }
  51. }
  52.  
  53. public boolean isRequired(final String key)
  54. {
  55. return (requiredInfo.get(key) != null);
  56. }
  57.  
  58. public boolean isOptional(final String key)
  59. {
  60. return (optionalInfo.get(key) != null);
  61. }
  62.  
  63. public boolean isKnown(final String key)
  64. {
  65. return (isRequired(key) || isOptional(key));
  66. }
  67.  
  68. public Class getType(final String key)
  69. {
  70. final PropertyInfo info;
  71.  
  72. info = getPropertyInfoFor(key);
  73.  
  74. return (info.getType());
  75. }
  76.  
  77. public boolean isValid(final String key,
  78. final String value)
  79. {
  80. final PropertyInfo info;
  81.  
  82. info = getPropertyInfoFor(key);
  83.  
  84. return (info.verify(value));
  85. }
  86.  
  87. private PropertyInfo getPropertyInfoFor(final String key)
  88. {
  89. PropertyInfo info;
  90.  
  91. info = requiredInfo.get(key);
  92.  
  93. if(info == null)
  94. {
  95. info = optionalInfo.get(key);
  96.  
  97. if(info == null)
  98. {
  99. // should be a better exception maybe... depends on how you
  100. // want to deal with it
  101. throw new IllegalArgumentException(key + "
  102. is not a valid property name");
  103. }
  104. }
  105.  
  106. return (info);
  107. }
  108.  
  109. protected final static class PropertyInfo
  110. {
  111. private final String name;
  112. private final boolean required;
  113. private final Class clazz;
  114. private final Verifier verifier;
  115.  
  116. protected PropertyInfo(final String nm,
  117. final boolean mandatory,
  118. final Class c)
  119. {
  120. this(nm, mandatory, c, getDefaultVerifier(c));
  121. }
  122.  
  123. protected PropertyInfo(final String nm,
  124. final boolean mandatory,
  125. final Class c,
  126. final Verifier v)
  127. {
  128. // check for null
  129. name = nm;
  130. required = mandatory;
  131. clazz = c;
  132. verifier = v;
  133. }
  134.  
  135. @Override
  136. public int hashCode()
  137. {
  138. return (getName().hashCode());
  139. }
  140.  
  141. @Override
  142. public boolean equals(final Object o)
  143. {
  144. final boolean retVal;
  145.  
  146. if(o instanceof PropertyInfo)
  147. {
  148. final PropertyInfo other;
  149.  
  150. other = (PropertyInfo)o;
  151. retVal = getName().equals(other.getName());
  152. }
  153. else
  154. {
  155. retVal = false;
  156. }
  157.  
  158. return (retVal);
  159. }
  160.  
  161. public boolean verify(final String value)
  162. {
  163. return (verifier.verify(value));
  164. }
  165.  
  166. public String getName()
  167. {
  168. return (name);
  169. }
  170.  
  171. public boolean isRequired()
  172. {
  173. return (required);
  174. }
  175.  
  176. public Class getType()
  177. {
  178. return (clazz);
  179. }
  180. }
  181.  
  182. private static Verifier getDefaultVerifier(final Class clazz)
  183. {
  184. final Verifier verifier;
  185.  
  186. if(clazz.equals(Boolean.class))
  187. {
  188. // shoudl use a singleton to save space...
  189. verifier = new BooleanVerifier();
  190. }
  191. else
  192. {
  193. throw new IllegalArgumentException("Unknown property type: " +
  194. clazz.getCanonicalName());
  195. }
  196.  
  197. return (verifier);
  198. }
  199.  
  200. public static interface Verifier
  201. {
  202. boolean verify(final String value);
  203. }
  204.  
  205. public static class BooleanVerifier
  206. implements Verifier
  207. {
  208. public boolean verify(final String value)
  209. {
  210. final boolean retVal;
  211.  
  212. if(value.equalsIgnoreCase("true") ||
  213. value.equalsIgnoreCase("false"))
  214. {
  215. retVal = true;
  216. }
  217. else
  218. {
  219. retVal = false;
  220. }
  221.  
  222. return (retVal);
  223. }
  224. }
  225. }
  226.  
  227. import java.util.Properties;
  228.  
  229.  
  230. public class Main
  231. {
  232. public static void main(String[] args)
  233. {
  234. final Properties properties;
  235. final PropertiesVerifier verifier;
  236.  
  237. properties = new Properties();
  238. properties.put("property.one", "true");
  239. properties.put("property.two", "false");
  240. // properties.put("property.three", "5");
  241. verifier = new PropertiesVerifier(
  242. new PropertiesVerifier.PropertyInfo[]
  243. {
  244. new PropertiesVerifier.PropertyInfo("property.one",
  245. true,
  246. Boolean.class),
  247. new PropertiesVerifier.PropertyInfo("property.two",
  248. false,
  249. Boolean.class),
  250. // new PropertiesVerifier.PropertyInfo("property.three",
  251. // true,
  252. // Boolean.class),
  253. });
  254.  
  255. System.out.println(verifier.isKnown("property.one"));
  256. System.out.println(verifier.isKnown("property.two"));
  257. System.out.println(verifier.isKnown("property.three"));
  258.  
  259. System.out.println(verifier.isRequired("property.one"));
  260. System.out.println(verifier.isRequired("property.two"));
  261. System.out.println(verifier.isRequired("property.three"));
  262.  
  263. System.out.println(verifier.isOptional("property.one"));
  264. System.out.println(verifier.isOptional("property.two"));
  265. System.out.println(verifier.isOptional("property.three"));
  266.  
  267. System.out.println(verifier.getType("property.one"));
  268. System.out.println(verifier.getType("property.two"));
  269.  
  270. // System.out.println(verifier.getType("property.tthree"));
  271. System.out.println(verifier.isValid("property.one", "true"));
  272. System.out.println(verifier.isValid("property.two", "false"));
  273. // System.out.println(verifier.isValid("property.tthree", "5"));
  274.  
  275.  
  276. verifier.verifyProperties(properties);
  277. }
  278. }
  279.  
  280. #list of required properties
  281. required=prop1,prop2,prop3
  282.  
  283. #all properties and their types
  284. prop1.type=Integer
  285. prop2.type=String
Add Comment
Please, Sign In to add comment