ISODateComponent.java

1
/*
2
 * Copyright 2006 - 2013
3
 *     Stefan Balev     <stefan.balev@graphstream-project.org>
4
 *     Julien Baudry    <julien.baudry@graphstream-project.org>
5
 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
6
 *     Yoann Pign��      <yoann.pigne@graphstream-project.org>
7
 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
8
 * 
9
 * This file is part of GraphStream <http://graphstream-project.org>.
10
 * 
11
 * GraphStream is a library whose purpose is to handle static or dynamic
12
 * graph, create them from scratch, file or any source and display them.
13
 * 
14
 * This program is free software distributed under the terms of two licenses, the
15
 * CeCILL-C license that fits European law, and the GNU Lesser General Public
16
 * License. You can  use, modify and/ or redistribute the software under the terms
17
 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
18
 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
19
 * the Free Software Foundation, either version 3 of the License, or (at your
20
 * option) any later version.
21
 * 
22
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
23
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
24
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
25
 * 
26
 * You should have received a copy of the GNU Lesser General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 * 
29
 * The fact that you are presently reading this means that you have had
30
 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
31
 */
32
package org.graphstream.util.time;
33
34
import java.text.DateFormatSymbols;
35
import java.util.Calendar;
36
import java.util.Locale;
37
38
/**
39
 * Defines components of {@link ISODateIO}.
40
 * 
41
 */
42
public abstract class ISODateComponent {
43
44
	/**
45
	 * Directives shortcut of the component. This property can not be changed.
46
	 */
47
	protected final String directive;
48
	/**
49
	 * Replacement of the directive. Could be a regular expression. The value
50
	 * catch will be sent to the component with
51
	 * <i>set(catched_value,Calendar)</i>. This property can not be changed.
52
	 */
53
	protected final String replace;
54
55
	/**
56
	 * Build a new component composed of a directive name ("%.") and a
57
	 * replacement value.
58
	 * 
59
	 * @param directive
60
	 *            directive name, should start with a leading '%'.
61
	 * @param replace
62
	 *            replace the directive with the value given here.
63
	 */
64
	public ISODateComponent(String directive, String replace) {
65
		this.directive = directive;
66
		this.replace = replace;
67
	}
68
69
	/**
70
	 * Access to the directive name of the component.
71
	 * 
72
	 * @return directive of the component.
73
	 */
74
	public String getDirective() {
75 1 1. getDirective : mutated return of Object value for org/graphstream/util/time/ISODateComponent::getDirective to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return directive;
76
	}
77
78
	/**
79
	 * Return true if this component is an alias. An alias can contain other
80
	 * directive name and its replacement should be parse again.
81
	 * 
82
	 * @return true if component is an alias.
83
	 */
84
	public boolean isAlias() {
85 1 1. isAlias : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return false;
86
	}
87
88
	/**
89
	 * Get the replacement value of this component.
90
	 * 
91
	 * @return replacement value
92
	 */
93
	public String getReplacement() {
94 1 1. getReplacement : mutated return of Object value for org/graphstream/util/time/ISODateComponent::getReplacement to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return replace;
95
	}
96
97
	/**
98
	 * Handle the value catched with the replacement value.
99
	 * 
100
	 * @param value
101
	 *            value matching the replacement string
102
	 * @param calendar
103
	 *            calendar we are working on
104
	 */
105
	public abstract void set(String value, Calendar calendar);
106
107
	/**
108
	 * Get a string representation of this component for a given calendar.
109
	 * 
110
	 * @param calendar
111
	 *            the calendar
112
	 * @return string representation of this component.
113
	 */
114
	public abstract String get(Calendar calendar);
115
116
	/**
117
	 * Defines an alias component. Such component does nothing else that replace
118
	 * them directive by another string.
119
	 */
120
	public static class AliasComponent extends ISODateComponent {
121
122
		public AliasComponent(String shortcut, String replace) {
123
			super(shortcut, replace);
124
		}
125
126
		public boolean isAlias() {
127 1 1. isAlias : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
			return true;
128
		}
129
130
		public void set(String value, Calendar calendar) {
131
			// Nothing to do
132
		}
133
134
		public String get(Calendar calendar) {
135 1 1. get : mutated return of Object value for org/graphstream/util/time/ISODateComponent$AliasComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return "";
136
		}
137
	}
138
139
	/**
140
	 * Defines a text component. Such component does nothing else that append
141
	 * text to the resulting regular expression.
142
	 */
143
	public static class TextComponent extends ISODateComponent {
144
		public TextComponent(String value) {
145
			super(null, value);
146
		}
147
148
		public void set(String value, Calendar calendar) {
149
			// Nothing to do
150
		}
151
152
		public String get(Calendar calendar) {
153 1 1. get : mutated return of Object value for org/graphstream/util/time/ISODateComponent$TextComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return replace;
154
		}
155
	}
156
157
	/**
158
	 * Defines a component associated with a field of a calendar. When a value
159
	 * is handled, component will try to set the associated field of the
160
	 * calendar.
161
	 */
162
	public static class FieldComponent extends ISODateComponent {
163
		protected final int field;
164
		protected final int offset;
165
		protected final String format;
166
167
		public FieldComponent(String shortcut, String replace, int field,
168
				String format) {
169
			this(shortcut, replace, field, 0, format);
170
		}
171
172
		public FieldComponent(String shortcut, String replace, int field,
173
				int offset, String format) {
174
			super(shortcut, replace);
175
			this.field = field;
176
			this.offset = offset;
177
			this.format = format;
178
		}
179
180
		public void set(String value, Calendar calendar) {
181 3 1. set : changed conditional boundary → NO_COVERAGE
2. set : negated conditional → NO_COVERAGE
3. set : negated conditional → NO_COVERAGE
			while (value.charAt(0) == '0' && value.length() > 1)
182
				value = value.substring(1);
183
			int val = Integer.parseInt(value);
184 2 1. set : Replaced integer addition with subtraction → NO_COVERAGE
2. set : removed call to java/util/Calendar::set → NO_COVERAGE
			calendar.set(field, val + offset);
185
		}
186
187
		public String get(Calendar calendar) {
188 1 1. get : mutated return of Object value for org/graphstream/util/time/ISODateComponent$FieldComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format(format, calendar.get(field));
189
		}
190
	}
191
192
	/**
193
	 * Base for locale-dependent component.
194
	 */
195
	protected static abstract class LocaleDependentComponent extends
196
			ISODateComponent {
197
		protected Locale locale;
198
		protected DateFormatSymbols symbols;
199
200
		public LocaleDependentComponent(String shortcut, String replace) {
201
			this(shortcut, replace, Locale.getDefault());
202
		}
203
204
		public LocaleDependentComponent(String shortcut, String replace,
205
				Locale locale) {
206
			super(shortcut, replace);
207
			this.locale = locale;
208
			this.symbols = DateFormatSymbols.getInstance(locale);
209
		}
210
	}
211
212
	/**
213
	 * Component handling AM/PM.
214
	 */
215
	public static class AMPMComponent extends LocaleDependentComponent {
216
		public AMPMComponent() {
217
			super("%p", "AM|PM|am|pm");
218
		}
219
220
		public void set(String value, Calendar calendar) {
221 1 1. set : negated conditional → NO_COVERAGE
			if (value.equalsIgnoreCase(symbols.getAmPmStrings()[Calendar.AM]))
222 1 1. set : removed call to java/util/Calendar::set → NO_COVERAGE
				calendar.set(Calendar.AM_PM, Calendar.AM);
223
			else if (value
224 1 1. set : negated conditional → NO_COVERAGE
					.equalsIgnoreCase(symbols.getAmPmStrings()[Calendar.PM]))
225 1 1. set : removed call to java/util/Calendar::set → NO_COVERAGE
				calendar.set(Calendar.AM_PM, Calendar.PM);
226
		}
227
228
		public String get(Calendar calendar) {
229 1 1. get : mutated return of Object value for org/graphstream/util/time/ISODateComponent$AMPMComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return symbols.getAmPmStrings()[calendar.get(Calendar.AM_PM)];
230
		}
231
	}
232
233
	/**
234
	 * Component handling utc offset (+/- 0000).
235
	 */
236
	public static class UTCOffsetComponent extends ISODateComponent {
237
		public UTCOffsetComponent() {
238
			super("%z", "[-+]\\d{4}");
239
		}
240
241
		public void set(String value, Calendar calendar) {
242
			String hs = value.substring(1, 3);
243
			String ms = value.substring(3, 5);
244 1 1. set : negated conditional → NO_COVERAGE
			if (hs.charAt(0) == '0')
245
				hs = hs.substring(1);
246 1 1. set : negated conditional → NO_COVERAGE
			if (ms.charAt(0) == '0')
247
				ms = ms.substring(1);
248
249 1 1. set : negated conditional → NO_COVERAGE
			int i = value.charAt(0) == '+' ? 1 : -1;
250
			int h = Integer.parseInt(hs);
251
			int m = Integer.parseInt(ms);
252
253 5 1. set : Replaced integer multiplication with division → NO_COVERAGE
2. set : Replaced integer addition with subtraction → NO_COVERAGE
3. set : Replaced integer multiplication with division → NO_COVERAGE
4. set : Replaced integer multiplication with division → NO_COVERAGE
5. set : removed call to java/util/TimeZone::setRawOffset → NO_COVERAGE
			calendar.getTimeZone().setRawOffset(i * (h * 60 + m) * 60000);
254
		}
255
256
		public String get(Calendar calendar) {
257
			int offset = calendar.getTimeZone().getRawOffset();
258
			String sign = "+";
259
260 2 1. get : changed conditional boundary → NO_COVERAGE
2. get : negated conditional → NO_COVERAGE
			if (offset < 0) {
261
				sign = "-";
262 1 1. get : removed negation → NO_COVERAGE
				offset = -offset;
263
			}
264
265 1 1. get : Replaced integer division with multiplication → NO_COVERAGE
			offset /= 60000;
266
267 1 1. get : Replaced integer division with multiplication → NO_COVERAGE
			int h = offset / 60;
268 1 1. get : Replaced integer modulus with multiplication → NO_COVERAGE
			int m = offset % 60;
269
270 1 1. get : mutated return of Object value for org/graphstream/util/time/ISODateComponent$UTCOffsetComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format("%s%02d%02d", sign, h, m);
271
		}
272
	}
273
274
	/**
275
	 * Component handling a number of milliseconds since the epoch (january, 1st
276
	 * 1970).
277
	 */
278
	public static class EpochComponent extends ISODateComponent {
279
		public EpochComponent() {
280
			super("%K", "\\d+");
281
		}
282
283
		public void set(String value, Calendar calendar) {
284
			long e = Long.parseLong(value);
285 1 1. set : removed call to java/util/Calendar::setTimeInMillis → NO_COVERAGE
			calendar.setTimeInMillis(e);
286
		}
287
288
		public String get(Calendar calendar) {
289 1 1. get : mutated return of Object value for org/graphstream/util/time/ISODateComponent$EpochComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format("%d", calendar.getTimeInMillis());
290
		}
291
	}
292
293
	/**
294
	 * Defines a not implemented component. Such components throw an Error if
295
	 * used.
296
	 */
297
	public static class NotImplementedComponent extends ISODateComponent {
298
		public NotImplementedComponent(String shortcut, String replace) {
299
			super(shortcut, replace);
300
		}
301
302
		public void set(String value, Calendar cal) {
303
			throw new Error("not implemented component");
304
		}
305
306
		public String get(Calendar calendar) {
307
			throw new Error("not implemented component");
308
		}
309
	}
310
311
	public static final ISODateComponent ABBREVIATED_WEEKDAY_NAME = new NotImplementedComponent(
312
			"%a", "\\w+[.]");
313
	public static final ISODateComponent FULL_WEEKDAY_NAME = new NotImplementedComponent(
314
			"%A", "\\w+");
315
	public static final ISODateComponent ABBREVIATED_MONTH_NAME = new NotImplementedComponent(
316
			"%b", "\\w+[.]");
317
	public static final ISODateComponent FULL_MONTH_NAME = new NotImplementedComponent(
318
			"%B", "\\w+");
319
	public static final ISODateComponent LOCALE_DATE_AND_TIME = new NotImplementedComponent(
320
			"%c", null);
321
	public static final ISODateComponent CENTURY = new NotImplementedComponent(
322
			"%C", "\\d\\d");
323
	public static final ISODateComponent DAY_OF_MONTH_2_DIGITS = new FieldComponent(
324
			"%d", "[012]\\d|3[01]", Calendar.DAY_OF_MONTH, "%02d");
325
	public static final ISODateComponent DATE = new AliasComponent("%D",
326
			"%m/%d/%y");
327
	public static final ISODateComponent DAY_OF_MONTH = new FieldComponent(
328
			"%e", "\\d|[12]\\d|3[01]", Calendar.DAY_OF_MONTH, "%2d");
329
	public static final ISODateComponent DATE_ISO8601 = new AliasComponent(
330
			"%F", "%Y-%m-%d");
331
	public static final ISODateComponent WEEK_BASED_YEAR_2_DIGITS = new FieldComponent(
332
			"%g", "\\d\\d", Calendar.YEAR, "%02d");
333
	public static final ISODateComponent WEEK_BASED_YEAR_4_DIGITS = new FieldComponent(
334
			"%G", "\\d{4}", Calendar.YEAR, "%04d");
335
	public static final ISODateComponent ABBREVIATED_MONTH_NAME_ALIAS = new AliasComponent(
336
			"%h", "%b");
337
	public static final ISODateComponent HOUR_OF_DAY = new FieldComponent("%H",
338
			"[01]\\d|2[0123]", Calendar.HOUR_OF_DAY, "%02d");
339
	public static final ISODateComponent HOUR = new FieldComponent("%I",
340
			"0\\d|1[012]", Calendar.HOUR, "%02d");
341
	public static final ISODateComponent DAY_OF_YEAR = new FieldComponent("%j",
342
			"[012]\\d\\d|3[0-5]\\d|36[0-6]", Calendar.DAY_OF_YEAR, "%03d");
343
	public static final ISODateComponent MILLISECOND = new FieldComponent("%k",
344
			"\\d{3}", Calendar.MILLISECOND, "%03d");
345
	public static final ISODateComponent EPOCH = new EpochComponent();
346
	public static final ISODateComponent MONTH = new FieldComponent("%m",
347
			"0[1-9]|1[012]", Calendar.MONTH, -1, "%02d");
348
	public static final ISODateComponent MINUTE = new FieldComponent("%M",
349
			"[0-5]\\d", Calendar.MINUTE, "%02d");
350
	public static final ISODateComponent NEW_LINE = new AliasComponent("%n",
351
			"\n");
352
	public static final ISODateComponent AM_PM = new AMPMComponent();
353
	public static final ISODateComponent LOCALE_CLOCK_TIME_12_HOUR = new NotImplementedComponent(
354
			"%r", "");
355
	public static final ISODateComponent HOUR_AND_MINUTE = new AliasComponent(
356
			"%R", "%H:%M");
357
	public static final ISODateComponent SECOND = new FieldComponent("%S",
358
			"[0-5]\\d|60", Calendar.SECOND, "%02d");
359
	public static final ISODateComponent TABULATION = new AliasComponent("%t",
360
			"\t");
361
	public static final ISODateComponent TIME_ISO8601 = new AliasComponent(
362
			"%T", "%H:%M:%S");
363
	public static final ISODateComponent DAY_OF_WEEK_1_7 = new FieldComponent(
364
			"%u", "[1-7]", Calendar.DAY_OF_WEEK, -1, "%1d");
365
	public static final ISODateComponent WEEK_OF_YEAR_FROM_SUNDAY = new FieldComponent(
366
			"%U", "[0-4]\\d|5[0123]", Calendar.WEEK_OF_YEAR, 1, "%2d");
367
	public static final ISODateComponent WEEK_NUMBER_ISO8601 = new NotImplementedComponent(
368
			"%V", "0[1-9]|[2-4]\\d|5[0123]");
369
	public static final ISODateComponent DAY_OF_WEEK_0_6 = new FieldComponent(
370
			"%w", "[0-6]", Calendar.DAY_OF_WEEK, "%01d");
371
	public static final ISODateComponent WEEK_OF_YEAR_FROM_MONDAY = new FieldComponent(
372
			"%W", "[0-4]\\d|5[0123]", Calendar.WEEK_OF_YEAR, "%02d");
373
	public static final ISODateComponent LOCALE_DATE_REPRESENTATION = new NotImplementedComponent(
374
			"%x", "");
375
	public static final ISODateComponent LOCALE_TIME_REPRESENTATION = new NotImplementedComponent(
376
			"%X", "");
377
	public static final ISODateComponent YEAR_2_DIGITS = new FieldComponent(
378
			"%y", "\\d\\d", Calendar.YEAR, "%02d");
379
	public static final ISODateComponent YEAR_4_DIGITS = new FieldComponent(
380
			"%Y", "\\d{4}", Calendar.YEAR, "%04d");
381
	public static final ISODateComponent UTC_OFFSET = new UTCOffsetComponent();
382
	public static final ISODateComponent LOCALE_TIME_ZONE_NAME = new NotImplementedComponent(
383
			"%Z", "\\w*");
384
	public static final ISODateComponent PERCENT = new AliasComponent("%%", "%");
385
}

Mutations

75

1.1
Location : getDirective
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent::getDirective to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

85

1.1
Location : isAlias
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

94

1.1
Location : getReplacement
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent::getReplacement to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

127

1.1
Location : isAlias
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

135

1.1
Location : get
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent$AliasComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

153

1.1
Location : get
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent$TextComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

181

1.1
Location : set
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : set
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : set
Killed by : none
negated conditional → NO_COVERAGE

184

1.1
Location : set
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

2.2
Location : set
Killed by : none
removed call to java/util/Calendar::set → NO_COVERAGE

188

1.1
Location : get
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent$FieldComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

221

1.1
Location : set
Killed by : none
negated conditional → NO_COVERAGE

222

1.1
Location : set
Killed by : none
removed call to java/util/Calendar::set → NO_COVERAGE

224

1.1
Location : set
Killed by : none
negated conditional → NO_COVERAGE

225

1.1
Location : set
Killed by : none
removed call to java/util/Calendar::set → NO_COVERAGE

229

1.1
Location : get
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent$AMPMComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

244

1.1
Location : set
Killed by : none
negated conditional → NO_COVERAGE

246

1.1
Location : set
Killed by : none
negated conditional → NO_COVERAGE

249

1.1
Location : set
Killed by : none
negated conditional → NO_COVERAGE

253

1.1
Location : set
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

2.2
Location : set
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

3.3
Location : set
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

4.4
Location : set
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

5.5
Location : set
Killed by : none
removed call to java/util/TimeZone::setRawOffset → NO_COVERAGE

260

1.1
Location : get
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : get
Killed by : none
negated conditional → NO_COVERAGE

262

1.1
Location : get
Killed by : none
removed negation → NO_COVERAGE

265

1.1
Location : get
Killed by : none
Replaced integer division with multiplication → NO_COVERAGE

267

1.1
Location : get
Killed by : none
Replaced integer division with multiplication → NO_COVERAGE

268

1.1
Location : get
Killed by : none
Replaced integer modulus with multiplication → NO_COVERAGE

270

1.1
Location : get
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent$UTCOffsetComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

285

1.1
Location : set
Killed by : none
removed call to java/util/Calendar::setTimeInMillis → NO_COVERAGE

289

1.1
Location : get
Killed by : none
mutated return of Object value for org/graphstream/util/time/ISODateComponent$EpochComponent::get to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.33