1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.esigate.util;
16
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Properties;
20
21 import org.esigate.ConfigurationException;
22
23
24
25
26
27
28
29
30 public final class PropertiesUtil {
31
32 private PropertiesUtil() {
33
34 }
35
36
37
38
39
40
41
42
43
44
45 public static Collection<String> getPropertyValue(Properties properties, String propertyName,
46 Collection<String> defaultValue) {
47 Collection<String> result = defaultValue;
48 String propertyValue = properties.getProperty(propertyName);
49 if (propertyValue != null) {
50 result = toCollection(propertyValue);
51 if (result.contains("*") && result.size() > 1) {
52 throw new ConfigurationException(propertyName + " must be a comma-separated list or *");
53 }
54 }
55 return result;
56 }
57
58
59
60
61
62
63
64 static Collection<String> toCollection(String list) {
65 Collection<String> result = new ArrayList<>();
66 if (list != null) {
67 String[] values = list.split(",");
68 for (String value : values) {
69 String trimmed = value.trim();
70 if (!trimmed.isEmpty()) {
71 result.add(trimmed);
72 }
73 }
74 }
75 return result;
76 }
77
78 public static int getPropertyValue(Properties props, String name, int defaultValue) {
79 String value = props.getProperty(name);
80 int result = defaultValue;
81 if (value != null) {
82 result = Integer.parseInt(value);
83 }
84 return result;
85 }
86
87 public static boolean getPropertyValue(Properties props, String name, boolean defaultValue) {
88 String value = props.getProperty(name);
89 boolean result = defaultValue;
90 if (value != null) {
91 result = Boolean.parseBoolean(value);
92 }
93 return result;
94 }
95
96 public static float getPropertyValue(Properties properties, String name, float defaultValue) {
97 String value = properties.getProperty(name);
98 float result = defaultValue;
99 if (value != null) {
100 result = Float.parseFloat(value);
101 }
102 return result;
103 }
104
105 public static long getPropertyValue(Properties properties, String name, long defaultValue) {
106 String value = properties.getProperty(name);
107 long result = defaultValue;
108 if (value != null) {
109 result = Long.parseLong(value);
110 }
111 return result;
112 }
113
114 }