|
1 | | -package com.cronutils.descriptor.refactor; |
2 | | - |
3 | | -import com.cronutils.model.Cron; |
4 | | -import com.cronutils.model.field.CronField; |
5 | | -import com.cronutils.model.field.CronFieldName; |
6 | | -import com.cronutils.model.field.expression.Always; |
7 | | -import com.cronutils.model.field.expression.Every; |
8 | | -import com.cronutils.model.field.expression.On; |
9 | | -import com.cronutils.utils.Preconditions; |
10 | | -import com.cronutils.utils.StringUtils; |
11 | | - |
12 | | -import java.text.ChoiceFormat; |
13 | | -import java.text.Format; |
14 | | -import java.text.MessageFormat; |
15 | | -import java.text.NumberFormat; |
16 | | -import java.util.Locale; |
17 | | -import java.util.Map; |
18 | | -import java.util.ResourceBundle; |
19 | | - |
20 | | -public class TimeDescriptor { |
21 | | - |
22 | | - private final ResourceBundle resourceBundle; |
23 | | - |
24 | | - public TimeDescriptor(final ResourceBundle resourceBundle) { |
25 | | - this.resourceBundle = Preconditions.checkNotNull(resourceBundle, "The resource bundle must not be null"); |
26 | | - } |
27 | | - |
28 | | - |
29 | | - public String describe(final Cron cron) { |
30 | | - return describe(cron.retrieveFieldsAsMap()); |
31 | | - } |
32 | | - |
33 | | - private String describe(final Map<CronFieldName, CronField> expressions) { |
34 | | - |
35 | | - if (expressions.containsKey(CronFieldName.SECOND)) { |
36 | | - final CronField cronField = expressions.get(CronFieldName.SECOND); |
37 | | - if (cronField.getExpression() instanceof Always) { |
38 | | - return describeEverySecond(1); |
39 | | - } else if (cronField.getExpression() instanceof On) { |
40 | | - return describeAtSecond(((On)cronField.getExpression()).getTime().getValue()); |
41 | | - } else if (cronField.getExpression() instanceof Every) { |
42 | | - return describeEverySecond(((Every)cronField.getExpression()).getPeriod().getValue()); |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - |
47 | | - return StringUtils.EMPTY; |
48 | | - } |
49 | | - |
50 | | - private String describeEverySecond(final int second) { |
51 | | - final double[] secondsLimit = {1, 2}; |
52 | | - final String[] secondsStrings = { |
53 | | - resourceBundle.getString("oneSecond"), |
54 | | - resourceBundle.getString("multipleSeconds") |
55 | | - }; |
56 | | - final double[] everyLimit = {1,2}; |
57 | | - final String[] everyStrings = { |
58 | | - resourceBundle.getString("every_one"), |
59 | | - resourceBundle.getString("every_multi") |
60 | | - }; |
61 | | - |
62 | | - final ChoiceFormat secondsChoiceFormat = new ChoiceFormat(secondsLimit, secondsStrings); |
63 | | - final ChoiceFormat everyChoiceFormat = new ChoiceFormat(everyLimit, everyStrings); |
64 | | - final String pattern = resourceBundle.getString("pattern_every_seconds"); |
65 | | - |
66 | | - final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK); |
67 | | - |
68 | | - final Format[] formats = { everyChoiceFormat, secondsChoiceFormat, NumberFormat.getInstance() }; |
69 | | - messageFormat.setFormats(formats); |
70 | | - final Object[] messageArguments = {second, second, second }; |
71 | | - final String result = messageFormat.format(messageArguments); |
72 | | - return result; |
73 | | - } |
74 | | - |
75 | | - private String describeAtSecond(final int second) { |
76 | | - |
77 | | - final String pattern = resourceBundle.getString("pattern_at_second"); |
78 | | - |
79 | | - final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK); |
80 | | - final Format[] formats = {NumberFormat.getInstance()}; |
81 | | - messageFormat.setFormats(formats); |
82 | | - |
83 | | - final Object[] messageArguments = { second }; |
84 | | - final String result = messageFormat.format(messageArguments); |
85 | | - return result; |
86 | | - } |
87 | | - |
88 | | -} |
| 1 | +package com.cronutils.descriptor.refactor; |
| 2 | + |
| 3 | +import java.text.ChoiceFormat; |
| 4 | +import java.text.Format; |
| 5 | +import java.text.MessageFormat; |
| 6 | +import java.text.NumberFormat; |
| 7 | +import java.util.Locale; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.ResourceBundle; |
| 10 | + |
| 11 | +import com.cronutils.model.Cron; |
| 12 | +import com.cronutils.model.field.CronField; |
| 13 | +import com.cronutils.model.field.CronFieldName; |
| 14 | +import com.cronutils.model.field.expression.Always; |
| 15 | +import com.cronutils.model.field.expression.Every; |
| 16 | +import com.cronutils.model.field.expression.On; |
| 17 | +import com.cronutils.utils.Preconditions; |
| 18 | +import com.cronutils.utils.StringUtils; |
| 19 | + |
| 20 | +public class TimeDescriptor { |
| 21 | + |
| 22 | + private final ResourceBundle resourceBundle; |
| 23 | + |
| 24 | + public TimeDescriptor(final ResourceBundle resourceBundle) { |
| 25 | + this.resourceBundle = Preconditions.checkNotNull(resourceBundle, "The resource bundle must not be null"); |
| 26 | + } |
| 27 | + |
| 28 | + public String describe(final Cron cron) { |
| 29 | + return describe(cron.retrieveFieldsAsMap()); |
| 30 | + } |
| 31 | + |
| 32 | + private String describe(final Map<CronFieldName, CronField> expressions) { |
| 33 | + |
| 34 | + if (expressions.containsKey(CronFieldName.SECOND)) { |
| 35 | + final CronField cronField = expressions.get(CronFieldName.SECOND); |
| 36 | + if (cronField.getExpression() instanceof Always) { |
| 37 | + return describeEverySecond(1); |
| 38 | + } else if (cronField.getExpression() instanceof On) { |
| 39 | + return describeAtSecond(((On) cronField.getExpression()).getTime().getValue()); |
| 40 | + } else if (cronField.getExpression() instanceof Every) { |
| 41 | + return describeEverySecond(((Every) cronField.getExpression()).getPeriod().getValue()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return StringUtils.EMPTY; |
| 46 | + } |
| 47 | + |
| 48 | + private String describeEverySecond(final int second) { |
| 49 | + final double[] secondsLimit = { 1, 2 }; |
| 50 | + final String[] secondsStrings = { resourceBundle.getString("oneSecond"), |
| 51 | + resourceBundle.getString("multipleSeconds") }; |
| 52 | + final double[] everyLimit = { 1, 2 }; |
| 53 | + final String[] everyStrings = { resourceBundle.getString("every_one"), |
| 54 | + resourceBundle.getString("every_multi") }; |
| 55 | + |
| 56 | + final ChoiceFormat secondsChoiceFormat = new ChoiceFormat(secondsLimit, secondsStrings); |
| 57 | + final ChoiceFormat everyChoiceFormat = new ChoiceFormat(everyLimit, everyStrings); |
| 58 | + final String pattern = resourceBundle.getString("pattern_every_seconds"); |
| 59 | + |
| 60 | + final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK); |
| 61 | + |
| 62 | + final Format[] formats = { everyChoiceFormat, secondsChoiceFormat, NumberFormat.getInstance() }; |
| 63 | + messageFormat.setFormats(formats); |
| 64 | + final Object[] messageArguments = { second, second, second }; |
| 65 | + final String result = messageFormat.format(messageArguments); |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + private String describeAtSecond(final int second) { |
| 70 | + |
| 71 | + final String pattern = resourceBundle.getString("pattern_at_second"); |
| 72 | + |
| 73 | + final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK); |
| 74 | + final Format[] formats = { NumberFormat.getInstance() }; |
| 75 | + messageFormat.setFormats(formats); |
| 76 | + |
| 77 | + final Object[] messageArguments = { second }; |
| 78 | + final String result = messageFormat.format(messageArguments); |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments