Skip to content

Commit

Permalink
Merge pull request jmrozanec#288 from Naxos84/#285_fix_sonarqube_issues
Browse files Browse the repository at this point in the history
jmrozanec#285 Fix some sonarqube issues
  • Loading branch information
jmrozanec authored Nov 30, 2017
2 parents 3f7db44 + cad8d5f commit b5bd239
Show file tree
Hide file tree
Showing 74 changed files with 1,585 additions and 1,534 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A Java library to parse, validate, migrate crons as well as get human readable d
[![Coverage Status](https://coveralls.io/repos/jmrozanec/cron-utils/badge.png)](https://coveralls.io/r/jmrozanec/cron-utils)

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/35b1b558473d42c4980432a3ecf84f6c)](https://www.codacy.com/app/jmrozanec/cron-utils?utm_source=github.com&utm_medium=referral&utm_content=jmrozanec/cron-utils&utm_campaign=Badge_Grade)
[![Quality Gate](https://sonarcloud.io/api/badges/gate?key=cron-utils)](https://sonarcloud.io/dashboard/index/cron-utils)
[![Project stats by OpenHub](https://www.openhub.net/p/cron-utils/widgets/project_thin_badge.gif)](https://www.openhub.net/p/cron-utils/)

**Download**
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/cronutils/Function.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Copyright 2014 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;

/**
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/cronutils/StringValidations.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ public class StringValidations {
private static final SpecialChar[] SPECIAL_CHARS = new SpecialChar[] { L, LW, W };
private static final Pattern NUMS_AND_CHARS_PATTERN = Pattern.compile("[#\\?/\\*0-9]");

private Pattern stringToIntKeysPattern;
private Pattern lwPattern;
private final Pattern stringToIntKeysPattern;
private final Pattern lwPattern;

public StringValidations(FieldConstraints constraints) {
this.lwPattern = buildLWPattern(constraints.getSpecialChars());
this.stringToIntKeysPattern = buildStringToIntPattern(constraints.getStringMappingKeySet());
public StringValidations(final FieldConstraints constraints) {
lwPattern = buildLWPattern(constraints.getSpecialChars());
stringToIntKeysPattern = buildStringToIntPattern(constraints.getStringMappingKeySet());
}

@VisibleForTesting
Pattern buildStringToIntPattern(Set<String> strings) {
Pattern buildStringToIntPattern(final Set<String> strings) {
return buildWordsPattern(strings);
}

@VisibleForTesting
public String removeValidChars(String exp) {
Matcher numsAndCharsMatcher = NUMS_AND_CHARS_PATTERN.matcher(exp.toUpperCase());
Matcher stringToIntKeysMatcher = stringToIntKeysPattern.matcher(numsAndCharsMatcher.replaceAll(""));
Matcher specialWordsMatcher = lwPattern.matcher(stringToIntKeysMatcher.replaceAll(""));
public String removeValidChars(final String exp) {
final Matcher numsAndCharsMatcher = NUMS_AND_CHARS_PATTERN.matcher(exp.toUpperCase());
final Matcher stringToIntKeysMatcher = stringToIntKeysPattern.matcher(numsAndCharsMatcher.replaceAll(""));
final Matcher specialWordsMatcher = lwPattern.matcher(stringToIntKeysMatcher.replaceAll(""));
return specialWordsMatcher.replaceAll("").replaceAll("\\s+", "").replaceAll(",", "").replaceAll("-", "");
}

@VisibleForTesting
Pattern buildLWPattern(Set<SpecialChar> specialChars) {
Set<String> scs = new HashSet<>();
for (SpecialChar sc : SPECIAL_CHARS) {
Pattern buildLWPattern(final Set<SpecialChar> specialChars) {
final Set<String> scs = new HashSet<>();
for (final SpecialChar sc : SPECIAL_CHARS) {
if (specialChars.contains(sc)) {
scs.add(sc.name());
}
Expand All @@ -66,15 +66,15 @@ Pattern buildLWPattern(Set<SpecialChar> specialChars) {
}

@VisibleForTesting
Pattern buildWordsPattern(Set<String> words) {
StringBuilder builder = new StringBuilder(ESCAPED_START);
Iterator<String> iterator = words.iterator();
Pattern buildWordsPattern(final Set<String> words) {
final StringBuilder builder = new StringBuilder(ESCAPED_START);
final Iterator<String> iterator = words.iterator();

if (!iterator.hasNext()) {
builder.append(ESCAPED_END);
return Pattern.compile(builder.toString());
}
String next = iterator.next();
final String next = iterator.next();
builder.append(next);
while (iterator.hasNext()) {
builder.append("|");
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/cronutils/builder/CronBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.cronutils.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;

import com.cronutils.model.Cron;
Expand All @@ -38,46 +38,46 @@

public class CronBuilder {

private final Map<CronFieldName, CronField> fields = new HashMap<>();
private CronDefinition definition;
private final Map<CronFieldName, CronField> fields = new EnumMap<>(CronFieldName.class);
private final CronDefinition definition;

private CronBuilder(CronDefinition definition) {
private CronBuilder(final CronDefinition definition) {
this.definition = definition;
}

public static CronBuilder cron(CronDefinition definition) {
public static CronBuilder cron(final CronDefinition definition) {
return new CronBuilder(definition);
}

public CronBuilder withDoY(FieldExpression expression) {
public CronBuilder withDoY(final FieldExpression expression) {
return addField(DAY_OF_YEAR, expression);
}

public CronBuilder withYear(FieldExpression expression) {
public CronBuilder withYear(final FieldExpression expression) {
return addField(YEAR, expression);
}

public CronBuilder withDoM(FieldExpression expression) {
public CronBuilder withDoM(final FieldExpression expression) {
return addField(DAY_OF_MONTH, expression);
}

public CronBuilder withMonth(FieldExpression expression) {
public CronBuilder withMonth(final FieldExpression expression) {
return addField(MONTH, expression);
}

public CronBuilder withDoW(FieldExpression expression) {
public CronBuilder withDoW(final FieldExpression expression) {
return addField(DAY_OF_WEEK, expression);
}

public CronBuilder withHour(FieldExpression expression) {
public CronBuilder withHour(final FieldExpression expression) {
return addField(HOUR, expression);
}

public CronBuilder withMinute(FieldExpression expression) {
public CronBuilder withMinute(final FieldExpression expression) {
return addField(MINUTE, expression);
}

public CronBuilder withSecond(FieldExpression expression) {
public CronBuilder withSecond(final FieldExpression expression) {
return addField(SECOND, expression);
}

Expand All @@ -86,10 +86,10 @@ public Cron instance() {
}

@VisibleForTesting
CronBuilder addField(CronFieldName name, FieldExpression expression) {
CronBuilder addField(final CronFieldName name, final FieldExpression expression) {
checkState(definition != null, "CronBuilder not initialized.");

FieldConstraints constraints = definition.getFieldDefinition(name).getConstraints();
final FieldConstraints constraints = definition.getFieldDefinition(name).getConstraints();
expression.accept(new ValidationFieldExpressionVisitor(constraints, definition.isStrictRanges()));
fields.put(name, new CronField(name, expression, constraints));

Expand Down
55 changes: 28 additions & 27 deletions src/main/java/com/cronutils/cli/CronUtilsCLI.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
* Copyright 2017 bflorat, 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.cli;

import org.apache.commons.cli.CommandLine;
Expand All @@ -13,42 +26,30 @@
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;

/*
* Copyright 2017 bflorat, 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.
*/
public class CronUtilsCLI {
private static final String HELP = "help";

private CronUtilsCLI() {
super();
}

public static void main(String[] args) throws Exception {
public static void main(final String[] args) throws Exception {
cronValidation(args);
}

private static void cronValidation(String[] args) throws ParseException {
Options options = new Options();
private static void cronValidation(final String[] args) throws ParseException {
final Options options = new Options();
options.addOption("v", "validate", false, "Action of validation (default)");
options.addOption("f", "format", true,
"Cron expression format to validate. Possible values are: CRON4J, QUARTZ, UNIX");
options.addOption("e", "expression", true, "Cron expression to validate. Example: '* 1 * * *'");
options.addOption("h", HELP, false, "Help");

String header = "Cron expressions validation by cron-utils\n\n";
String footer = "\nPlease report issues at https://github.com/jmrozanec/cron-utils/issues";
final String header = "Cron expressions validation by cron-utils\n\n";
final String footer = "\nPlease report issues at https://github.com/jmrozanec/cron-utils/issues";

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
final CommandLineParser parser = new DefaultParser();
final CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption(HELP) || cmd.getOptions().length == 0) {
showHelp(options, header, footer);
return;
Expand All @@ -58,19 +59,19 @@ private static void cronValidation(String[] args) throws ParseException {
return;
}
if (cmd.hasOption('v')) {
String format = cmd.getOptionValue("f");
String expression = cmd.getOptionValue("e");
final String format = cmd.getOptionValue("f");
final String expression = cmd.getOptionValue("e");

CronType cronType = CronType.valueOf(format);
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(cronType);
CronParser cronParser = new CronParser(cronDefinition);
Cron quartzCron = cronParser.parse(expression);
final CronType cronType = CronType.valueOf(format);
final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(cronType);
final CronParser cronParser = new CronParser(cronDefinition);
final Cron quartzCron = cronParser.parse(expression);
quartzCron.validate();
}
}

private static void showHelp(Options options, String header, String footer) {
HelpFormatter formatter = new HelpFormatter();
private static void showHelp(final Options options, final String header, final String footer) {
final HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("cron-validator", header, options, footer, true);
}
}
Loading

0 comments on commit b5bd239

Please sign in to comment.