Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed some tests in italian #492

Merged
merged 1 commit into from Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
170 changes: 82 additions & 88 deletions src/main/java/com/cronutils/descriptor/refactor/TimeDescriptor.java
@@ -1,88 +1,82 @@
package com.cronutils.descriptor.refactor;

import com.cronutils.model.Cron;
import com.cronutils.model.field.CronField;
import com.cronutils.model.field.CronFieldName;
import com.cronutils.model.field.expression.Always;
import com.cronutils.model.field.expression.Every;
import com.cronutils.model.field.expression.On;
import com.cronutils.utils.Preconditions;
import com.cronutils.utils.StringUtils;

import java.text.ChoiceFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class TimeDescriptor {

private final ResourceBundle resourceBundle;

public TimeDescriptor(final ResourceBundle resourceBundle) {
this.resourceBundle = Preconditions.checkNotNull(resourceBundle, "The resource bundle must not be null");
}


public String describe(final Cron cron) {
return describe(cron.retrieveFieldsAsMap());
}

private String describe(final Map<CronFieldName, CronField> expressions) {

if (expressions.containsKey(CronFieldName.SECOND)) {
final CronField cronField = expressions.get(CronFieldName.SECOND);
if (cronField.getExpression() instanceof Always) {
return describeEverySecond(1);
} else if (cronField.getExpression() instanceof On) {
return describeAtSecond(((On)cronField.getExpression()).getTime().getValue());
} else if (cronField.getExpression() instanceof Every) {
return describeEverySecond(((Every)cronField.getExpression()).getPeriod().getValue());
}
}


return StringUtils.EMPTY;
}

private String describeEverySecond(final int second) {
final double[] secondsLimit = {1, 2};
final String[] secondsStrings = {
resourceBundle.getString("oneSecond"),
resourceBundle.getString("multipleSeconds")
};
final double[] everyLimit = {1,2};
final String[] everyStrings = {
resourceBundle.getString("every_one"),
resourceBundle.getString("every_multi")
};

final ChoiceFormat secondsChoiceFormat = new ChoiceFormat(secondsLimit, secondsStrings);
final ChoiceFormat everyChoiceFormat = new ChoiceFormat(everyLimit, everyStrings);
final String pattern = resourceBundle.getString("pattern_every_seconds");

final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK);

final Format[] formats = { everyChoiceFormat, secondsChoiceFormat, NumberFormat.getInstance() };
messageFormat.setFormats(formats);
final Object[] messageArguments = {second, second, second };
final String result = messageFormat.format(messageArguments);
return result;
}

private String describeAtSecond(final int second) {

final String pattern = resourceBundle.getString("pattern_at_second");

final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK);
final Format[] formats = {NumberFormat.getInstance()};
messageFormat.setFormats(formats);

final Object[] messageArguments = { second };
final String result = messageFormat.format(messageArguments);
return result;
}

}
package com.cronutils.descriptor.refactor;

import java.text.ChoiceFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import com.cronutils.model.Cron;
import com.cronutils.model.field.CronField;
import com.cronutils.model.field.CronFieldName;
import com.cronutils.model.field.expression.Always;
import com.cronutils.model.field.expression.Every;
import com.cronutils.model.field.expression.On;
import com.cronutils.utils.Preconditions;
import com.cronutils.utils.StringUtils;

public class TimeDescriptor {

private final ResourceBundle resourceBundle;

public TimeDescriptor(final ResourceBundle resourceBundle) {
this.resourceBundle = Preconditions.checkNotNull(resourceBundle, "The resource bundle must not be null");
}

public String describe(final Cron cron) {
return describe(cron.retrieveFieldsAsMap());
}

private String describe(final Map<CronFieldName, CronField> expressions) {

if (expressions.containsKey(CronFieldName.SECOND)) {
final CronField cronField = expressions.get(CronFieldName.SECOND);
if (cronField.getExpression() instanceof Always) {
return describeEverySecond(1);
} else if (cronField.getExpression() instanceof On) {
return describeAtSecond(((On) cronField.getExpression()).getTime().getValue());
} else if (cronField.getExpression() instanceof Every) {
return describeEverySecond(((Every) cronField.getExpression()).getPeriod().getValue());
}
}

return StringUtils.EMPTY;
}

private String describeEverySecond(final int second) {
final double[] secondsLimit = { 1, 2 };
final String[] secondsStrings = { resourceBundle.getString("oneSecond"),
resourceBundle.getString("multipleSeconds") };
final double[] everyLimit = { 1, 2 };
final String[] everyStrings = { resourceBundle.getString("every_one"),
resourceBundle.getString("every_multi") };

final ChoiceFormat secondsChoiceFormat = new ChoiceFormat(secondsLimit, secondsStrings);
final ChoiceFormat everyChoiceFormat = new ChoiceFormat(everyLimit, everyStrings);
final String pattern = resourceBundle.getString("pattern_every_seconds");

final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK);

final Format[] formats = { everyChoiceFormat, secondsChoiceFormat, NumberFormat.getInstance() };
messageFormat.setFormats(formats);
final Object[] messageArguments = { second, second, second };
final String result = messageFormat.format(messageArguments);
return result;
}

private String describeAtSecond(final int second) {

final String pattern = resourceBundle.getString("pattern_at_second");

final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK);
final Format[] formats = { NumberFormat.getInstance() };
messageFormat.setFormats(formats);

final Object[] messageArguments = { second };
final String result = messageFormat.format(messageArguments);
return result;
}

}
@@ -1,53 +1,53 @@
/*
* Copyright 2015 jmrozanec
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cronutils.model.field.expression;

import com.cronutils.model.field.expression.visitor.FieldExpressionVisitor;
import com.cronutils.utils.Preconditions;

import java.io.Serializable;

public abstract class FieldExpression implements Serializable {

private static final long serialVersionUID = 5138279438874391617L;

public And and(final FieldExpression exp) {
return new And().and(this).and(exp);
}

/**
* Represents FieldExpression as string.
*
* @return String representation, never null.
*/
public abstract String asString();

/**
* Accept a visitor to perform some action on the instance. Current instance is cloned, so that we ensure immutability. Clone of this
* instance is returned after visitor.visit(clone) was invoked.
*
* @param visitor - FieldExpressionVisitor instance, never null
* @return FieldExpression copied instance with visitor action performed.
*/
public abstract FieldExpression accept(final FieldExpressionVisitor visitor);

public static FieldExpression always() {
return Always.INSTANCE;
}

public static FieldExpression questionMark() {
return QuestionMark.INSTANCE;
}
}
/*
* Copyright 2015 jmrozanec
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cronutils.model.field.expression;
import java.io.Serializable;
import com.cronutils.model.field.expression.visitor.FieldExpressionVisitor;
public abstract class FieldExpression implements Serializable {
private static final long serialVersionUID = 5138279438874391617L;
public And and(final FieldExpression exp) {
return new And().and(this).and(exp);
}
/**
* Represents FieldExpression as string.
*
* @return String representation, never null.
*/
public abstract String asString();
/**
* Accept a visitor to perform some action on the instance. Current instance is
* cloned, so that we ensure immutability. Clone of this instance is returned
* after visitor.visit(clone) was invoked.
*
* @param visitor - FieldExpressionVisitor instance, never null
* @return FieldExpression copied instance with visitor action performed.
*/
public abstract FieldExpression accept(final FieldExpressionVisitor visitor);
public static FieldExpression always() {
return Always.INSTANCE;
}
public static FieldExpression questionMark() {
return QuestionMark.INSTANCE;
}
}
58 changes: 33 additions & 25 deletions src/main/resources/CronUtilsI18N_it.properties
@@ -1,25 +1,33 @@
between_x_and_y=tra {0} e {1}
every=ogni
and=e
at=alle
day=giorno
days=giorni
hour=ora
hours=ore
minute=minuto
minutes=minuti
second=secondo
seconds=secondi
month=mese
months=mesi
year=anno
years=anni
between=tra
of_every_month=di ogni mese
of_the_month=del mese
last=ultimo
the_nearest_weekday_to_the=il giorno della settimana più vicino a
last_day_of_month=ultimo giorno del mese
last_weekday_of_month=ultimo giorno della settimana nel mese
day_before_last_day_of_month=un giorno prima dell'ultimo giorno del mese
days_before_last_day_of_month={0} giorni prima dell'ultimo giorno del mese
between_x_and_y=tra {0} e {1}
every=ogni
and=e
at=alle
day=giorno
days=giorni
hour=ora
hours=ore
minute=minuto
minutes=minuti
second=secondo
seconds=secondi
month=mese
months=mesi
year=anno
years=anni
between=tra
of_every_month=di ogni mese
of_the_month=del mese
last=ultimo
the_nearest_weekday_to_the=il giorno della settimana piu' vicino a
last_day_of_month=ultimo giorno del mese
last_weekday_of_month=ultimo giorno della settimana nel mese
day_before_last_day_of_month=un giorno prima dell'ultimo giorno del mese
days_before_last_day_of_month={0} giorni prima dell'ultimo giorno del mese

pattern_every_seconds = {0} {1}
every_one=ogni
every_multi=ogni
oneSecond=secondo
multipleSeconds={2} secondi

pattern_at_second=al secondo {0}