Skip to content

Commit

Permalink
handle SpotBugs warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Nov 11, 2024
1 parent 9e59191 commit 0078a99
Show file tree
Hide file tree
Showing 20 changed files with 986 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package uk.ac.ox.poseidon.agents.behaviours;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Data;
import sim.engine.SimState;
import sim.engine.Steppable;
Expand All @@ -37,6 +38,7 @@ public abstract class Action implements Steppable {
private final Duration duration;
private final Vessel vessel;

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Action(
final LocalDateTime start,
final Duration duration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package uk.ac.ox.poseidon.agents.behaviours.destination;

import com.google.common.collect.ImmutableList;
import ec.util.MersenneTwisterFast;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.behaviours.travel.TravelBehaviour;
Expand All @@ -30,7 +31,7 @@

public class ChooseRandomDestinationBehaviour extends DestinationBehaviour {

private final List<Int2D> possibleDestinations;
private final ImmutableList<Int2D> possibleDestinations;
private final MersenneTwisterFast rng;

public ChooseRandomDestinationBehaviour(
Expand All @@ -39,7 +40,7 @@ public ChooseRandomDestinationBehaviour(
final MersenneTwisterFast rng
) {
super(travelBehaviour);
this.possibleDestinations = possibleDestinations;
this.possibleDestinations = ImmutableList.copyOf(possibleDestinations);
this.rng = rng;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package uk.ac.ox.poseidon.agents.vessels;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
Expand All @@ -40,9 +41,10 @@ public class Vessel implements Oriented2D {
private double cruisingSpeed;
private double heading;

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Vessel(
final String id,
@NonNull final Port homePort,
final Port homePort,
final double cruisingSpeed,
final VesselField vesselField
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package uk.ac.ox.poseidon.biology.biomass;

import ec.util.MersenneTwisterFast;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Double2D;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class BiomassDiffuser implements Steppable {

private final Map<Int2D, List<Int2D>> habitableNeighbours;

@SuppressFBWarnings("EI_EXPOSE_REP2")
public BiomassDiffuser(
final BiomassGrid biomassGrid,
final CarryingCapacityGrid carryingCapacityGrid,
Expand Down
33 changes: 30 additions & 3 deletions core/src/main/java/uk/ac/ox/poseidon/core/PathFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package uk.ac.ox.poseidon.core;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import lombok.*;

Expand All @@ -27,14 +28,36 @@
import java.util.List;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PathFactory extends GlobalScopeFactory<Path> {

private Factory<? extends Path> parent;
@NonNull private List<String> pathElements;
@NonNull private ImmutableList<String> pathElements;

public static PathFactory from(
final String path
) {
return from(null, Path.of(path));
}

public static PathFactory from(
final Path path
) {
return from(null, path);
}

public static PathFactory from(
final PathFactory parent,
final String path
) {
return from(parent, Path.of(path));
}

public static PathFactory from(
final PathFactory parent,
Expand All @@ -48,13 +71,17 @@ public static PathFactory from(
Streams.stream(path)
)
.map(Path::toString)
.toList()
.collect(toImmutableList())
);
}

@Override
protected Path newInstance(final Simulation simulation) {
final Path path = Paths.get(pathElements.getFirst(), pathElements.removeFirst());
checkState(!pathElements.isEmpty());
final Path path = Path.of(
pathElements.getFirst(),
pathElements.subList(1, pathElements.size()).toArray(String[]::new)
);
return parent == null ? path : parent.get(null).resolve(path);
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/uk/ac/ox/poseidon/core/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package uk.ac.ox.poseidon.core;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Getter;
import sim.engine.SimState;
import uk.ac.ox.poseidon.core.schedule.TemporalSchedule;
Expand Down Expand Up @@ -64,6 +65,7 @@ public Simulation(
);
}

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Simulation(
final long seed,
final TemporalSchedule schedule,
Expand Down
58 changes: 58 additions & 0 deletions core/src/test/java/uk/ac/ox/poseidon/core/PathFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package uk.ac.ox.poseidon.core;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;

import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.*;

class PathFactoryTest {

private static final String FOLDERS = "/a/b/c/";
private static final String FILENAME = "file.txt";

@Test
void newInstanceSimpleFile() {
final PathFactory pathFactory = PathFactory.from(FILENAME);
assertNull(pathFactory.getParent());
assertEquals(ImmutableList.of(FILENAME), pathFactory.getPathElements());
assertEquals(Path.of(FILENAME), pathFactory.newInstance(null));
}

@Test
void newInstanceFileInFolder() {
final PathFactory pathFactory = PathFactory.from(FOLDERS + FILENAME);
assertNull(pathFactory.getParent());
assertEquals(ImmutableList.of("/", "a", "b", "c", FILENAME), pathFactory.getPathElements());
assertEquals(Path.of(FOLDERS + FILENAME), pathFactory.newInstance(null));
}

@Test
void newInstanceWithParent() {
final PathFactory pathFactory = PathFactory.from(PathFactory.from(FOLDERS), FILENAME);
assertEquals(Path.of(FOLDERS), pathFactory.getParent().get(null));
assertEquals(ImmutableList.of(FILENAME), pathFactory.getPathElements());
assertEquals(Path.of(FOLDERS + FILENAME), pathFactory.newInstance(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@

class PaletteColorMapTest {

private final double max = 5;
private final double min = -max;
private final PaletteColorMap colorMap = new PaletteColorMap(OLERON, min, max);
private static final double MAX = 5;
private static final double MIN = -MAX;
private final PaletteColorMap colorMap = new PaletteColorMap(OLERON, MIN, MAX);

@Test
void getColor() {
final Color[] palette = colorMap.getColors();
Map.of(
min - 1, 0,
min, 0,
MIN - 1, 0,
MIN, 0,
-1E-10, 127,
0.0, 128,
max, palette.length - 1,
max + 1, palette.length - 1
MAX, palette.length - 1,
MAX + 1, palette.length - 1
).forEach((value, index) -> {
final Color expected = palette[index];
final Color actual = colorMap.getColor(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@

import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;

public interface BathymetricGrid extends NumberGrid<Double, DoubleGrid2D> {

default List<Int2D> getWaterCells() {
return getGridExtent().getAllCells().stream().filter(this::isWater).toList();
return getGridExtent()
.getAllCells()
.stream()
.filter(this::isWater)
.collect(toImmutableList());
}

default List<Int2D> getLandCells() {
return getGridExtent().getAllCells().stream().filter(this::isLand).toList();
return getGridExtent()
.getAllCells()
.stream()
.filter(this::isLand)
.collect(toImmutableList());
}

default List<Int2D> getAllCells() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
package uk.ac.ox.poseidon.geography.paths;

import com.google.common.collect.ImmutableList;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.Optional;

public class CachingPathFinder<P> implements PathFinder<P> {
private final PathFinder<P> pathFinder;
private final PathCache<P> cache;

@SuppressFBWarnings("EI_EXPOSE_REP2")
public CachingPathFinder(
final PathFinder<P> pathFinder,
final PathCache<P> cache
Expand Down
72 changes: 72 additions & 0 deletions gui/out/production/resources/images/port.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0078a99

Please sign in to comment.