1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.cache;
17
18 import java.io.Serializable;
19 import java.util.concurrent.TimeUnit;
20
21
22
23
24
25
26 public final class CacheEntryConfiguration implements Serializable
27 {
28
29
30
31
32
33
34
35 private static final long serialVersionUID = 1L;
36
37
38
39
40 private static final int DEFAULT_MAX_IDLE_TIME = -1;
41
42
43
44
45 private static final int DEFAULT_LIFESPAN_TIME = -1;
46
47
48
49
50 private static final String TO_STRING_DELIMITER = ":";
51
52
53
54
55 private final CacheEntryConfigurationType enumEntry;
56
57
58
59
60
61
62
63
64
65
66
67
68 public CacheEntryConfiguration(final CacheEntryConfigurationType enumEntry)
69 {
70 this.enumEntry = enumEntry;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public int getLifespan()
87 {
88 return getIntegerValue(enumEntry.systemPropertyLifespanName(),
89 DEFAULT_LIFESPAN_TIME);
90 }
91
92
93
94
95
96
97
98 public TimeUnit getLifespanTimeUnit()
99 {
100 return getTimeUnitValue(enumEntry.systemPropertyLifespanTimeUnitName(),
101 TimeUnit.SECONDS);
102 }
103
104
105
106
107
108
109 public int getMaxIdleTime()
110 {
111 return getIntegerValue(enumEntry.systemPropertyMayIdleTimeName(),
112 DEFAULT_MAX_IDLE_TIME);
113 }
114
115
116
117
118
119
120 public TimeUnit getMaxIdleTimeTimeUnit()
121 {
122 return getTimeUnitValue(enumEntry.systemPropertyMaxIdleTimeTimeUnitName(),
123 TimeUnit.SECONDS);
124 }
125
126 private int getIntegerValue(final String propertyName, final int defaultValue)
127 {
128 try
129 {
130 final int value =
131 Integer.parseInt(System.getProperty(propertyName,
132 String.valueOf(defaultValue)));
133 return value;
134 }
135 catch (final NumberFormatException e)
136 {
137 return defaultValue;
138 }
139 }
140
141 private TimeUnit getTimeUnitValue(final String propertyName,
142 final TimeUnit defaultValue)
143 {
144 try
145 {
146 final String timeUnitValue =
147 System.getProperty(propertyName, defaultValue.toString());
148 final TimeUnit timeUnit = TimeUnit.valueOf(TimeUnit.class, timeUnitValue);
149 return timeUnit;
150 }
151 catch (final IllegalArgumentException e)
152 {
153 return defaultValue;
154 }
155 }
156
157
158
159
160
161
162
163
164
165
166 @Override
167 public String toString()
168 {
169 final StringBuilder buffer = new StringBuilder();
170 buffer.append("name:").append(TO_STRING_DELIMITER)
171 .append(enumEntry.toString()).append(TO_STRING_DELIMITER);
172 buffer.append("lifespan:").append(TO_STRING_DELIMITER)
173 .append(getLifespan()).append(TO_STRING_DELIMITER);
174 buffer.append("lifespan TimeUnit:").append(TO_STRING_DELIMITER)
175 .append(getLifespanTimeUnit()).append(TO_STRING_DELIMITER);
176 buffer.append("maxIdleTime:").append(TO_STRING_DELIMITER)
177 .append(getMaxIdleTime()).append(TO_STRING_DELIMITER);
178 buffer.append("maxIdelTime TimeUnit:").append(TO_STRING_DELIMITER)
179 .append(getMaxIdleTimeTimeUnit()).append(TO_STRING_DELIMITER);
180 return buffer.toString();
181 }
182
183 }