Skip to content

Commit

Permalink
Avoiding Optional in model
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed May 1, 2018
1 parent 9df7684 commit a2c0357
Show file tree
Hide file tree
Showing 30 changed files with 184 additions and 155 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,34 @@
Changelog of Violations lib.

## Unreleased
### GitHub [#33](https://github.com/tomasbjerre/violations-lib/issues/33) Add support for NullAway

**NullAway**


[72244241bbd2ead](https://github.com/tomasbjerre/violations-lib/commit/72244241bbd2ead) Tomas Bjerre *2018-04-14 05:08:26*


### GitHub [#35](https://github.com/tomasbjerre/violations-lib/issues/35) cpplint parser does not recognize any violations

**Testing cpplint variant**


[8455fc596c6a547](https://github.com/tomasbjerre/violations-lib/commit/8455fc596c6a547) Tomas Bjerre *2018-04-13 11:45:16*


### No issue

**Adding another PMD test**


[c36c7bd5a99a442](https://github.com/tomasbjerre/violations-lib/commit/c36c7bd5a99a442) Tomas Bjerre *2018-03-07 18:05:26*

**Fixed typo (assertion on wrong issue checked).**


[5381b5d9381a6d1](https://github.com/tomasbjerre/violations-lib/commit/5381b5d9381a6d1) Ulli Hafner *2018-03-02 22:56:12*

**Bumping version to fix release**


Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 1.54
version = 1.55
14 changes: 7 additions & 7 deletions src/main/java/se/bjurr/violations/lib/ViolationsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ViolationsApi {
private File startFile;
private String reporter;

public static String getDetailedReport(List<Violation> violations) {
public static String getDetailedReport(final List<Violation> violations) {
return new DetailedReportCreator(violations) //
.create();
}
Expand All @@ -30,17 +30,17 @@ public static ViolationsApi violationsApi() {

private ViolationsApi() {}

public ViolationsApi findAll(Parser parser) {
public ViolationsApi findAll(final Parser parser) {
this.parser = checkNotNull(parser, "parser");
return this;
}

public ViolationsApi withReporter(String reporter) {
public ViolationsApi withReporter(final String reporter) {
this.reporter = checkNotNull(reporter, "reporter");
return this;
}

public ViolationsApi inFolder(String folder) {
public ViolationsApi inFolder(final String folder) {
startFile = new File(checkNotNull(folder, "folder"));
if (!startFile.exists()) {
throw new RuntimeException(folder + " not found");
Expand Down Expand Up @@ -72,7 +72,7 @@ public List<Violation> violations() {
+ " "
+ v.getSeverity()
+ " ("
+ v.getRule().or("?")
+ v.getRule()
+ ") "
+ v.getFile()
+ " "
Expand All @@ -84,11 +84,11 @@ public List<Violation> violations() {
return foundViolations;
}

private String makeWindowsFriendly(String regularExpression) {
private String makeWindowsFriendly(final String regularExpression) {
return regularExpression.replace("/", "(?:/|\\\\)");
}

public ViolationsApi withPattern(String regularExpression) {
public ViolationsApi withPattern(final String regularExpression) {
pattern = makeWindowsFriendly(regularExpression);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private StringBuilder toDetailed(
final String message = addNewlines(violation.getMessage(), maxMessageColumnWidth);
final String line = addNewlines(violation.getStartLine().toString(), maxLineColumnWidth);
final String severity = addNewlines(violation.getSeverity().name(), maxSeverityColumnWidth);
final String rule = addNewlines(violation.getRule().or(""), maxRuleColumnWidth);
final String rule = addNewlines(violation.getRule(), maxRuleColumnWidth);
final String reporter = addNewlines(violation.getReporter(), maxReporterColumnWidth);
final String[] row = {reporter, rule, severity, line, message};
rows.add(row);
Expand Down
51 changes: 25 additions & 26 deletions src/main/java/se/bjurr/violations/lib/model/Violation.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package se.bjurr.violations.lib.model;

import static se.bjurr.violations.lib.util.Optional.fromNullable;
import static se.bjurr.violations.lib.util.Utils.checkNotNull;
import static se.bjurr.violations.lib.util.Utils.emptyToNull;
import static se.bjurr.violations.lib.util.Utils.firstNonNull;
import static se.bjurr.violations.lib.util.Utils.nullToEmpty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import se.bjurr.violations.lib.reports.Parser;
import se.bjurr.violations.lib.util.Optional;

public class Violation implements Serializable, Comparable<Violation> {
public static class ViolationBuilder {
Expand All @@ -32,67 +31,67 @@ public Violation build() {
return new Violation(this);
}

public ViolationBuilder setColumn(Integer column) {
public ViolationBuilder setColumn(final Integer column) {
this.column = column;
return this;
}

public ViolationBuilder setEndLine(Integer endLine) {
public ViolationBuilder setEndLine(final Integer endLine) {
this.endLine = endLine;
return this;
}

public ViolationBuilder setFile(String file) {
public ViolationBuilder setFile(final String file) {
this.file = file;
return this;
}

public ViolationBuilder setMessage(String message) {
public ViolationBuilder setMessage(final String message) {
this.message = message;
return this;
}

public ViolationBuilder setParser(Parser parser) {
public ViolationBuilder setParser(final Parser parser) {
this.parser = parser;
return this;
}

public ViolationBuilder setReporter(String reporter) {
public ViolationBuilder setReporter(final String reporter) {
this.reporter = reporter;
return this;
}

public ViolationBuilder setRule(String rule) {
public ViolationBuilder setRule(final String rule) {
this.rule = rule;
return this;
}

public ViolationBuilder setSeverity(SEVERITY severity) {
public ViolationBuilder setSeverity(final SEVERITY severity) {
this.severity = severity;
return this;
}

public ViolationBuilder setSource(String source) {
public ViolationBuilder setSource(final String source) {
this.source = source;
return this;
}

public ViolationBuilder setSpecific(String specificsKey, Integer specificsValue) {
public ViolationBuilder setSpecific(final String specificsKey, final Integer specificsValue) {
specifics.put(specificsKey, Integer.toString(specificsValue));
return this;
}

public ViolationBuilder setSpecific(String specificsKey, String specificsValue) {
public ViolationBuilder setSpecific(final String specificsKey, final String specificsValue) {
specifics.put(specificsKey, specificsValue);
return this;
}

public ViolationBuilder setSpecifics(Map<String, String> specifics) {
public ViolationBuilder setSpecifics(final Map<String, String> specifics) {
this.specifics = specifics;
return this;
}

public ViolationBuilder setStartLine(Integer startLine) {
public ViolationBuilder setStartLine(final Integer startLine) {
this.startLine = startLine;
return this;
}
Expand Down Expand Up @@ -135,7 +134,7 @@ public Violation() {
parser = null;
}

public Violation(ViolationBuilder vb) {
public Violation(final ViolationBuilder vb) {
parser = checkNotNull(vb.parser, "reporter");
if (vb.reporter != null && !vb.reporter.trim().isEmpty()) {
reporter = vb.reporter;
Expand All @@ -154,7 +153,7 @@ public Violation(ViolationBuilder vb) {
}

@Override
public boolean equals(Object obj) {
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
Expand Down Expand Up @@ -237,8 +236,8 @@ public boolean equals(Object obj) {
return true;
}

public Optional<Integer> getColumn() {
return fromNullable(column);
public Integer getColumn() {
return firstNonNull(column, -1);
}

public Integer getEndLine() {
Expand All @@ -257,7 +256,7 @@ public Parser getParser() {
return parser;
}

public void setReporter(String reporter) {
public void setReporter(final String reporter) {
this.reporter = checkNotNull(reporter, "reporter");
}

Expand All @@ -266,17 +265,17 @@ public String getReporter() {
}

/** Rule that was matched. All tools don't use rules, so this may be null. */
public Optional<String> getRule() {
return fromNullable(rule);
public String getRule() {
return nullToEmpty(rule);
}

public SEVERITY getSeverity() {
return severity;
}

/** The thing that contains the violations. Like a Java/C# class. */
public Optional<String> getSource() {
return fromNullable(source);
public String getSource() {
return nullToEmpty(source);
}

/**
Expand Down Expand Up @@ -335,11 +334,11 @@ public String toString() {
}

@Override
public int compareTo(Violation o) {
public int compareTo(final Violation o) {
return comparingString(this).compareTo(comparingString(o));
}

private String comparingString(Violation o) {
private String comparingString(final Violation o) {
return o.file
+ "_"
+ (Integer.MAX_VALUE - o.getStartLine())
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/bjurr/violations/lib/util/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static <T> Optional<T> absent() {
return Absent.withType();
}

public static <T> Optional<T> fromNullable(T nullableReference) {
public static <T> Optional<T> fromNullable(final T nullableReference) {
return nullableReference == null ? Optional.<T>absent() : new Present<>(nullableReference);
}

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/se/bjurr/violations/lib/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,42 @@
import se.bjurr.violations.lib.model.Violation;

public class Utils {
public static <T> T checkNotNull(T reference, String errorMessage) {
public static <T> T checkNotNull(final T reference, final String errorMessage) {
if (reference == null) {
throw new NullPointerException(errorMessage);
}
return reference;
}

public static String emptyToNull(String str) {
public static String emptyToNull(final String str) {
if (str == null || str.isEmpty()) {
return null;
}
return str;
}

public static <T> T firstNonNull(T f, T s) {
public static String nullToEmpty(final String nullableReference) {
return nullableReference == null ? "" : nullableReference;
}

public static <T> T firstNonNull(final T f, final T s) {
if (f != null) {
return f;
}
return s;
}

@SuppressWarnings("static-access")
public static InputStream getResource(String filename) {
public static InputStream getResource(final String filename) {
return Thread.currentThread().getContextClassLoader().getSystemResourceAsStream(filename);
}

public static boolean isNullOrEmpty(String str) {
public static boolean isNullOrEmpty(final String str) {
return str == null || str.isEmpty();
}

@SuppressWarnings("resource")
public static String toString(InputStream inputStream) throws IOException {
public static String toString(final InputStream inputStream) throws IOException {
final Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
final String result = scanner.hasNext() ? scanner.next() : "";
scanner.close();
Expand All @@ -48,15 +52,16 @@ public static String toString(InputStream inputStream) throws IOException {
return result;
}

public static String toString(URL resource) throws IOException {
public static String toString(final URL resource) throws IOException {
try {
return toString(resource.openStream());
} catch (final IOException e) {
throw e;
}
}

public static List<Violation> setReporter(List<Violation> violations, String reporter) {
public static List<Violation> setReporter(
final List<Violation> violations, final String reporter) {
for (final Violation v : violations) {
v.setReporter(reporter);
}
Expand Down
Loading

0 comments on commit a2c0357

Please sign in to comment.