Skip to content

Commit

Permalink
Xlint:all and code quality improvements,
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartinsanta committed Jan 26, 2024
1 parent d05be32 commit 233b1ba
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package es.urjc.etsii.grafo.autoconfig.exception;

import java.io.Serial;

public class AlgorithmParsingException extends RuntimeException {

@Serial
private static final long serialVersionUID = 1L;

public AlgorithmParsingException(String message, Throwable cause) {
super(message, cause);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package es.urjc.etsii.grafo.exception;

import java.io.Serial;

/**
* Thrown by any algorithm component when the combination of parameters given is not valid
*/
public class IllegalAlgorithmConfigException extends IllegalArgumentException {

@Serial
private static final long serialVersionUID = 1L;

/**
* Initialize the exception
* @param explanation Explanation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package es.urjc.etsii.grafo.exception;

import java.io.Serial;

/**
* Exception thrown when there is any problem loading an instance
*/
public class InstanceImportException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;

public InstanceImportException(String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package es.urjc.etsii.grafo.exception;

import java.io.Serial;

/**
* Thrown when an invalid random usage is detected, which would affect experiment reproducibility, and suggests alternatives to use.
*/
public class InvalidRandomException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package es.urjc.etsii.grafo.exception;

import java.io.Serial;

/**
* Thrown if the solution is not valid at different solving stages
*/
public class InvalidSolutionException extends RuntimeException {

@Serial
private static final long serialVersionUID = 1L;

/**
* Create a new InvalidSolutionException with the given reason
* @param cause why the solution is not valid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package es.urjc.etsii.grafo.exception;

import java.io.Serial;

/**
* Thrown when the maximum allocated resources are consumed,
* or when even if they are not consumed,
* it is estimated with high confidence that we cannot complete the operation.
*/
public class ResourceLimitException extends RuntimeException {

@Serial
private static final long serialVersionUID = 1L;

/**
* Initialize the exception
* @param message Explanation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static <S extends Solution<S,I>, I extends Instance> Improver<S,I> nul(){
}

@SafeVarargs
@SuppressWarnings("varargs")
public static <S extends Solution<S,I>, I extends Instance> Improver<S,I> serial(FMode fmode, Improver<S, I>... improvers){
return new SequentialImprover<>(fmode, improvers);
}
Expand Down Expand Up @@ -108,13 +109,14 @@ public static class SequentialImprover<S extends Solution<S,I>,I extends Instanc
private final Improver<S,I>[] improvers;

@SafeVarargs
@SuppressWarnings("varargs")
public SequentialImprover(FMode fmode, Improver<S, I>... improvers) {
super(fmode);
this.improvers = improvers;
}

@AutoconfigConstructor
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtype"})
public SequentialImprover(
@ProvidedParam FMode fmode,
Improver<S, I> improverA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public String toString() {
* @return a new neighborhood which returns the moves from each neighborhood as a sequence
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <M extends Move<S, I>, S extends Solution<S, I>, I extends Instance> Neighborhood<M, S, I> concat(Neighborhood<M, S, I>... neighborhoods) {
return new ConcatNeighborhood<>(neighborhoods);
}
Expand All @@ -81,6 +82,7 @@ public static <M extends Move<S, I>, S extends Solution<S, I>, I extends Instanc
* @return a new neighborhood which returns the moves from each neighborhood alternating between them
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <M extends Move<S, I>, S extends Solution<S, I>, I extends Instance> Neighborhood<M, S, I> interleave(Neighborhood<M, S, I>... neighborhoods) {
return new InterleavedNeighborhood<>(neighborhoods);
}
Expand All @@ -107,6 +109,7 @@ public static <M extends Move<S, I>, S extends Solution<S, I>, I extends Instanc
* @param <I> Instance type
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <M extends Move<S, I>, S extends Solution<S, I>, I extends Instance> RandomizableNeighborhood<M, S, I> random(boolean balanced, RandomizableNeighborhood<M, S, I>... neighborhoods) {
return new RandomFromNeighborhood<>(balanced, neighborhoods);
}
Expand Down Expand Up @@ -210,8 +213,7 @@ private Optional<M> balancedPick(S solution) {
private abstract static class DerivedNeighborhood<M extends Move<S, I>, S extends Solution<S, I>, I extends Instance> extends Neighborhood<M, S, I> {
final Neighborhood<M, S, I>[] neighborhoods;

@SafeVarargs
public DerivedNeighborhood(Neighborhood<M, S, I>... neighborhoods) {
public DerivedNeighborhood(Neighborhood<M, S, I>[] neighborhoods) {
this.neighborhoods = Objects.requireNonNull(neighborhoods);
}

Expand All @@ -230,6 +232,7 @@ public String toString() {
private static class ConcatNeighborhood<M extends Move<S, I>, S extends Solution<S, I>, I extends Instance> extends DerivedNeighborhood<M, S, I> {

@SafeVarargs
@SuppressWarnings("varargs")
private ConcatNeighborhood(Neighborhood<M, S, I>... neighborhoods) {
super(neighborhoods);
}
Expand Down Expand Up @@ -260,6 +263,7 @@ public ExploreResult<M, S, I> explore(S solution) {
private static class InterleavedNeighborhood<M extends Move<S, I>, S extends Solution<S, I>, I extends Instance> extends DerivedNeighborhood<M, S, I> {

@SafeVarargs
@SuppressWarnings("varargs")
private InterleavedNeighborhood(Neighborhood<M, S, I>... neighborhoods) {
super(neighborhoods);
}
Expand All @@ -286,7 +290,7 @@ public ExploreResult<M, S, I> explore(S solution) {
}

return new ExploreResult<>(StreamSupport.stream(new Spliterators.AbstractSpliterator<>(totalSize, ch) {
List<Spliterator<? extends M>> spliterators = sps;
final List<Spliterator<? extends M>> spliterators = sps;
int lastIndex = 0;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class StreamUtil {
* @return A single stream containing all the given streams
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> Stream<T> merge(Stream<T>... streams){
return Arrays.stream(streams).flatMap(s -> s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// Commented and Edited by Arup Guha on 3/6/2017 for COP 4516
// Code for Dinic's Network Flow Algorithm

import java.util.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;

/**
* Dinic's algorithm for the maxflow problem.
Expand All @@ -32,7 +35,7 @@ public class Dinic {
*
* @param N graph size
*/
@SuppressWarnings("unchecked") // due to required (HashSet<Edge>[]) cast
@SuppressWarnings({"unchecked", "rawtypes"}) // due to required (HashSet<Edge>[]) cast
public Dinic(int N) {

// s is the source, t is the sink, add these as last two nodes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.stream.Stream;

@SuppressWarnings("unchecked")
public class NeighborhoodTest {

TestSolution solution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void testCloneSet(){
for (int i = 0; i < size; i++) {
set1.add(r.nextInt(size));
}
var set2 = (BitSet) set1.clone();
var set2 = set1.clone();
assertEquals(set1, set2);
assertEquals(set1.hashCode(), set2.hashCode());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DotGenerator {
/**
* Common features for all files.
* Layout is set as "neato", other layout as "dot", "fdp", "sfdp" "twopi" or "circo" can be selected.
* More information: https://www.graphviz.org
* More information: <a href="https://www.graphviz.org">Graphviz website</a>
*/
private static final String header = """
digraph G {
Expand All @@ -35,9 +35,9 @@ public class DotGenerator {
*/
private static final String footer = "\n}";

private final AbstractEventStorage eventStorage;
private final AbstractEventStorage<?,?> eventStorage;

public DotGenerator(MemoryEventStorage eventStorage) {
public DotGenerator(MemoryEventStorage<?,?> eventStorage) {
this.eventStorage = eventStorage;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ private static double normalize(double value, double min, double max) {
@GetMapping("/api/generategraph/{eventId}")
public String getSolutionAsDotString(@PathVariable int eventId) throws IOException {
var event = eventStorage.getEvent(eventId);
if(event instanceof SolutionGeneratedEvent solutionGeneratedEvent && solutionGeneratedEvent.getSolution().isPresent()){
if(event instanceof SolutionGeneratedEvent<?,?> solutionGeneratedEvent && solutionGeneratedEvent.getSolution().isPresent()){
var solution = (TSPSolution) solutionGeneratedEvent.getSolution().get();
var viz = Graphviz.fromString(generateDotDiagram(solution));
BufferedImage image = viz.render(Format.PNG).toImage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SmokeTest {
private Executor<?, ?> executor;

@Autowired
private AbstractEventStorage abstractEventStorage;
private AbstractEventStorage<?,?> abstractEventStorage;

@Autowired
private ApplicationContext applicationContext;
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
<configuration>
<compilerArgs>
<arg>-parameters</arg>
<arg>-Xlint:all,-this-escape</arg>
</compilerArgs>
</configuration>
</plugin>
Expand Down

0 comments on commit 233b1ba

Please sign in to comment.