Skip to content

Commit

Permalink
ModelUtils: declare variable and parameters final if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
maarzt committed Mar 26, 2024
1 parent f66133d commit 53c469b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 76 deletions.
47 changes: 23 additions & 24 deletions src/main/java/org/mastodon/mamut/model/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static final String dump( final Model model, final long maxLines )
*/
public static final String dump( final Model model, final long maxLines, final DumpFlags... options )
{
Set< DumpFlags > optionsSet = EnumSet.copyOf( Arrays.asList( options ) );
final Set< DumpFlags > optionsSet = EnumSet.copyOf( Arrays.asList( options ) );
final FeatureModel featureModel = model.getFeatureModel();
final List< FeatureSpec< ?, ? > > featureSpecs = new ArrayList<>( featureModel.getFeatureSpecs() );
featureSpecs.sort( Comparator.comparing( FeatureSpec::getKey ) );
Expand All @@ -145,12 +145,12 @@ public static final String dump( final Model model, final long maxLines, final D
return str.toString();
}

private static void addSpotsTable( Model model, long maxLines, Set< DumpFlags > optionsSet, List< FeatureSpec< ?, ? > > featureSpecs, StringBuilder str )
private static void addSpotsTable( final Model model, final long maxLines, final Set< DumpFlags > optionsSet, final List< FeatureSpec< ?, ? > > featureSpecs, final StringBuilder str )
{
final String spaceUnits = Optional.ofNullable( model.getSpaceUnits() ).orElse( "" );
final ModelGraph graph = model.getGraph();
final FeatureModel featureModel = model.getFeatureModel();
TablePrinter< Spot > spotsTable = new TablePrinter<>();
final TablePrinter< Spot > spotsTable = new TablePrinter<>();
spotsTable.defineColumn( 9, "Id", "", spot -> Integer.toString( spot.getInternalPoolIndex() ) );
spotsTable.defineColumn( 9, "Label", "", Spot::getLabel );
spotsTable.defineColumn( 6, "Frame", "", spot -> Integer.toString( spot.getTimepoint() ) );
Expand All @@ -162,15 +162,15 @@ private static void addSpotsTable( Model model, long maxLines, Set< DumpFlags >
if ( optionsSet.contains( DumpFlags.PRINT_FEATURES ) )
addFeatureColumns( featureSpecs, featureModel, spotsTable, Spot.class );

List< Spot > spots = getSortedSpots( graph, featureModel );
final List< Spot > spots = getSortedSpots( graph, featureModel );
spotsTable.print( str, spots, maxLines );
}

private static void addLinksTable( Model model, long maxLines, Set< DumpFlags > optionsSet, List< FeatureSpec< ?, ? > > featureSpecs, StringBuilder str )
private static void addLinksTable( final Model model, final long maxLines, final Set< DumpFlags > optionsSet, final List< FeatureSpec< ?, ? > > featureSpecs, final StringBuilder str )
{
final ModelGraph graph = model.getGraph();
final FeatureModel featureModel = model.getFeatureModel();
TablePrinter< Link > linkTable = new TablePrinter<>();
final TablePrinter< Link > linkTable = new TablePrinter<>();
linkTable.defineColumn( 9, "Id", "", link -> Integer.toString( link.getInternalPoolIndex() ) );
final Spot ref = graph.vertexRef();
linkTable.defineColumn( 9, "Source Id", "", link -> Integer.toString( link.getSource( ref ).getInternalPoolIndex() ) );
Expand All @@ -183,21 +183,20 @@ private static void addLinksTable( Model model, long maxLines, Set< DumpFlags >
linkTable.print( str, graph.edges(), maxLines );
}


private static < T > void addTagColumns( TablePrinter< T > table, TagSetStructure tagSetStructure, ObjTags< T > tags )
private static < T > void addTagColumns( final TablePrinter< T > table, final TagSetStructure tagSetStructure, final ObjTags< T > tags )
{
for ( TagSetStructure.TagSet tagSet : tagSetStructure.getTagSets() )
for ( final TagSetStructure.TagSet tagSet : tagSetStructure.getTagSets() )
{
String header = tagSet.getName();
ObjTagMap< T, TagSetStructure.Tag > tagSetTags = tags.tags( tagSet );
final String header = tagSet.getName();
final ObjTagMap< T, TagSetStructure.Tag > tagSetTags = tags.tags( tagSet );
table.defineColumn( header.length(), header, "", spotOrLink -> {
TagSetStructure.Tag tag = tagSetTags.get( spotOrLink );
final TagSetStructure.Tag tag = tagSetTags.get( spotOrLink );
return tag == null ? "" : tag.label();
} );
}
}

private static < T > void addFeatureColumns( List< FeatureSpec< ?, ? > > featureSpecs, FeatureModel featureModel, TablePrinter< T > table, Class< T > targetClass )
private static < T > void addFeatureColumns( final List< FeatureSpec< ?, ? > > featureSpecs, final FeatureModel featureModel, final TablePrinter< T > table, final Class< T > targetClass )
{
for ( final FeatureSpec< ?, ? > featureSpec : featureSpecs )
{
Expand All @@ -218,15 +217,15 @@ private static < T > void addFeatureColumns( List< FeatureSpec< ?, ? > > feature
}
}

private static RefArrayList< Spot > getSortedSpots( ModelGraph graph, FeatureModel featureModel )
private static RefArrayList< Spot > getSortedSpots( final ModelGraph graph, final FeatureModel featureModel )
{
final RefArrayList< Spot > spots = new RefArrayList<>( graph.vertices().getRefPool(), graph.vertices().size() );
spots.addAll( graph.vertices() );
spots.sort( getSpotComparator( featureModel ) );
return spots;
}

private static Comparator< Spot > getSpotComparator( FeatureModel featureModel )
private static Comparator< Spot > getSpotComparator( final FeatureModel featureModel )
{
if ( featureModel.getFeatureSpecs().contains( SpotTrackIDFeature.SPEC ) )
{
Expand All @@ -239,7 +238,7 @@ private static Comparator< Spot > getSpotComparator( FeatureModel featureModel )
return Comparator.comparingInt( Spot::getTimepoint ).thenComparing( Spot::getInternalPoolIndex );
}

private static < T > String valueAsString( FeatureProjection< T > projection, T t, int width )
private static < T > String valueAsString( final FeatureProjection< T > projection, final T t, final int width )
{
if ( !projection.isSet( t ) )
return String.format( Locale.US, "%" + width + "s", "unset" );
Expand All @@ -255,27 +254,27 @@ private static class TablePrinter< T >

private final List< Column< T > > columns = new ArrayList<>();

public void defineColumn( int width, String title, String unit, Function< T, String > toString )
public void defineColumn( final int width, final String title, final String unit, final Function< T, String > toString )
{
columns.add( new Column<>( columns.isEmpty(), width, title, unit, toString ) );
}

public void print( StringBuilder str, Iterable< T > rows, long maxLines )
public void print( final StringBuilder str, final Iterable< T > rows, final long maxLines )
{
for ( Column< T > column : columns )
for ( final Column< T > column : columns )
str.append( String.format( Locale.US, column.template, column.header ) );
str.append( '\n' );
for ( Column< T > column : columns )
for ( final Column< T > column : columns )
str.append( String.format( Locale.US, column.template, column.unit ) );
str.append( '\n' );
int totalWidth = columns.stream().mapToInt( c -> c.width + 2 ).sum() - 2;
final int totalWidth = columns.stream().mapToInt( c -> c.width + 2 ).sum() - 2;
for ( int i = 0; i < totalWidth; i++ )
str.append( '-' );
str.append( '\n' );
long i = 0;
for ( T row : rows )
for ( final T row : rows )
{
for ( Column< T > column : columns )
for ( final Column< T > column : columns )
str.append( String.format( Locale.US, column.template, column.valueToString.apply( row ) ) );
str.append( '\n' );
i++;
Expand All @@ -298,7 +297,7 @@ private static class Column< T >

private final Function< T, String > valueToString;

private Column( boolean isFirst, int width, String header, String unit, Function< T, String > valueToString )
private Column( final boolean isFirst, final int width, final String header, final String unit, final Function< T, String > valueToString )
{
this.header = header;
this.unit = unit;
Expand Down
74 changes: 37 additions & 37 deletions src/test/java/org/mastodon/mamut/model/ModelUndoRedoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public class ModelUndoRedoTest
@Test
public void testUndoRedo()
{
Model model = new Model();
ModelGraph graph = model.getGraph();
final Model model = new Model();
final ModelGraph graph = model.getGraph();
model.setUndoPoint();
Spot a = graph.addVertex().init( 0, new double[ 3 ], 1 );
final Spot a = graph.addVertex().init( 0, new double[ 3 ], 1 );
a.setLabel( "A" );
model.setUndoPoint();
assertEquals( "A", graphAsString( graph ) );
Spot b = graph.addVertex().init( 0, new double[ 3 ], 1 );
final Spot b = graph.addVertex().init( 0, new double[ 3 ], 1 );
b.setLabel( "B" );
model.setUndoPoint();
assertEquals( "A, B", graphAsString( graph ) );
Expand All @@ -86,17 +86,17 @@ public void testUndoRedo()
/**
* Returns a readable sting representation of the graph.
*/
private String graphAsString( ModelGraph graph )
private String graphAsString( final ModelGraph graph )
{
List< String > spots = new ArrayList<>();
for ( Spot spot : graph.vertices() )
final List< String > spots = new ArrayList<>();
for ( final Spot spot : graph.vertices() )
spots.add( spot.getLabel() );
Collections.sort( spots );
List< String > links = new ArrayList<>();
for ( Link link : graph.edges() )
final List< String > links = new ArrayList<>();
for ( final Link link : graph.edges() )
links.add( link.getSource().getLabel() + "->" + link.getTarget().getLabel() );
Collections.sort( links );
StringJoiner joiner = new StringJoiner( ", " );
final StringJoiner joiner = new StringJoiner( ", " );
spots.forEach( joiner::add );
links.forEach( joiner::add );
return joiner.toString();
Expand All @@ -109,7 +109,7 @@ private String graphAsString( ModelGraph graph )
@Test
public void testUndoRedoOnEmptyModel()
{
Model model = new Model();
final Model model = new Model();
testUndoRedo( model );
}

Expand All @@ -119,37 +119,37 @@ public void testUndoRedoOnEmptyModel()
* set. Then, the undo and redo methods are called to test if the model is
* restored to the intended state.
*/
private static void testUndoRedo( Model model )
private static void testUndoRedo( final Model model )
{
final List< String > states = new ArrayList<>();
makeVariousChangesAndRecordUndoPoints( model, states );
undoAllRecordedPointsAndVerifyCorrectness( model, states );
redoAllRecordedPointsAndVerifyCorrectness( model, states );
}

private static void makeVariousChangesAndRecordUndoPoints( Model model, List< String > states )
private static void makeVariousChangesAndRecordUndoPoints( final Model model, final List< String > states )
{
ModelGraph graph = model.getGraph();
final ModelGraph graph = model.getGraph();
// add spot A
Spot a = graph.addVertex().init( 2, new double[] { 1, 2, 3 }, 1.5 );
final Spot a = graph.addVertex().init( 2, new double[] { 1, 2, 3 }, 1.5 );
a.setLabel( "spot A" );
setUndoPointAndRecordState( model, states );

// add spot B
Spot b = graph.addVertex().init( 3, new double[] { 1, 2, 4 }, 1.7 );
final Spot b = graph.addVertex().init( 3, new double[] { 1, 2, 4 }, 1.7 );
b.setLabel( "spot B" );
setUndoPointAndRecordState( model, states );

// add edge
Link edge = graph.addEdge( a, b ).init();
final Link edge = graph.addEdge( a, b ).init();
setUndoPointAndRecordState( model, states );

// add tag set
TagSetStructure.TagSet tagset = TagSetUtils.addNewTagSetToModel( model, "tag set 1", Arrays.asList(
final TagSetStructure.TagSet tagset = TagSetUtils.addNewTagSetToModel( model, "tag set 1", Arrays.asList(
Pair.of( "tag1", Color.red.getRGB() ),
Pair.of( "tag2", Color.green.getRGB() ) ) );
TagSetStructure.Tag tag1 = tagset.getTags().get( 0 );
TagSetStructure.Tag tag2 = tagset.getTags().get( 1 );
final TagSetStructure.Tag tag1 = tagset.getTags().get( 0 );
final TagSetStructure.Tag tag2 = tagset.getTags().get( 1 );
TagSetUtils.tagSpot( model, tagset, tag1, a );
TagSetUtils.tagSpot( model, tagset, tag2, b );
TagSetUtils.tagLinks( model, tagset, tag1, Collections.singletonList( edge ) );
Expand All @@ -165,7 +165,7 @@ private static void makeVariousChangesAndRecordUndoPoints( Model model, List< St
setUndoPointAndRecordState( model, states );

// change covariance
double[][] cov = { { 2, 0.1, 0 }, { 0.1, 2.5, 0 }, { 0, 0, 2.1 } };
final double[][] cov = { { 2, 0.1, 0 }, { 0.1, 2.5, 0 }, { 0, 0, 2.1 } };
a.setCovariance( cov );
setUndoPointAndRecordState( model, states );

Expand All @@ -178,13 +178,13 @@ private static void makeVariousChangesAndRecordUndoPoints( Model model, List< St
setUndoPointAndRecordState( model, states );
}

public static void setUndoPointAndRecordState( Model model, List< String > states )
public static void setUndoPointAndRecordState( final Model model, final List< String > states )
{
model.setUndoPoint();
states.add( modelAsString( model ) );
}

public static void undoAllRecordedPointsAndVerifyCorrectness( Model model, List< String > states )
public static void undoAllRecordedPointsAndVerifyCorrectness( final Model model, final List< String > states )
{
for ( int i = states.size() - 1; i > 0; i-- )
{
Expand All @@ -194,7 +194,7 @@ public static void undoAllRecordedPointsAndVerifyCorrectness( Model model, List<
assertEquals( states.get( 0 ), modelAsString( model ) );
}

public static void redoAllRecordedPointsAndVerifyCorrectness( Model model, List< String > states )
public static void redoAllRecordedPointsAndVerifyCorrectness( final Model model, final List< String > states )
{
assertEquals( states.get( 0 ), modelAsString( model ) );
for ( int i = 1; i < states.size(); i++ )
Expand All @@ -204,7 +204,7 @@ public static void redoAllRecordedPointsAndVerifyCorrectness( Model model, List<
}
}

private static String modelAsString( Model model )
private static String modelAsString( final Model model )
{
return ModelUtils.dump( model, ModelUtils.DumpFlags.PRINT_TAGS );
}
Expand All @@ -222,7 +222,7 @@ private static String modelAsString( Model model )
@Test
public void testUndoRedoAfterModelImporter()
{
Model model = new Model();
final Model model = new Model();
// Fill the undo-redo history with many different entries:
makeVariousChangesAndRecordUndoPoints( model, new ArrayList<>() );
addFourSpotGraph( model );
Expand All @@ -240,14 +240,14 @@ public void testUndoRedoAfterModelImporter()
/**
* Adds three spots and two edges to the model.
*/
private void addThreeSpotGraph( Model model )
private void addThreeSpotGraph( final Model model )
{
ModelGraph graph = model.getGraph();
Spot a = graph.addVertex().init( 0, new double[] { 1, 0, 0 }, 1 );
final ModelGraph graph = model.getGraph();
final Spot a = graph.addVertex().init( 0, new double[] { 1, 0, 0 }, 1 );
a.setLabel( "A" );
Spot b = graph.addVertex().init( 1, new double[] { 0, 2, 0 }, 1 );
final Spot b = graph.addVertex().init( 1, new double[] { 0, 2, 0 }, 1 );
b.setLabel( "B" );
Spot c = graph.addVertex().init( 1, new double[] { 0, 0, 3 }, 1 );
final Spot c = graph.addVertex().init( 1, new double[] { 0, 0, 3 }, 1 );
c.setLabel( "C" );
graph.addEdge( a, b ).init();
graph.addEdge( a, c ).init();
Expand All @@ -257,16 +257,16 @@ private void addThreeSpotGraph( Model model )
/**
* Adds four spots and three edges to the model.
*/
private void addFourSpotGraph( Model model )
private void addFourSpotGraph( final Model model )
{
ModelGraph graph = model.getGraph();
Spot a = graph.addVertex().init( 0, new double[] { 1, 2, 3 }, 1 );
final ModelGraph graph = model.getGraph();
final Spot a = graph.addVertex().init( 0, new double[] { 1, 2, 3 }, 1 );
a.setLabel( "A" );
Spot b = graph.addVertex().init( 1, new double[] { 2, 2, 3 }, 1 );
final Spot b = graph.addVertex().init( 1, new double[] { 2, 2, 3 }, 1 );
b.setLabel( "B" );
Spot c = graph.addVertex().init( 2, new double[] { 3, 2, 3 }, 1 );
final Spot c = graph.addVertex().init( 2, new double[] { 3, 2, 3 }, 1 );
c.setLabel( "C" );
Spot d = graph.addVertex().init( 3, new double[] { 4, 2, 3 }, 1 );
final Spot d = graph.addVertex().init( 3, new double[] { 4, 2, 3 }, 1 );
d.setLabel( "D" );
graph.addEdge( a, b ).init();
graph.addEdge( b, c ).init();
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/org/mastodon/mamut/model/ModelUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public class ModelUtilsTest
@Test
public void testDump()
{
Model model = new Model();
ModelGraph graph = model.getGraph();
Spot a = graph.addVertex().init( 0, new double[] { 1, 2, 3 }, 1 );
Spot b = graph.addVertex().init( 1, new double[] { 1, 2, 3.2 }, 1 );
final Model model = new Model();
final ModelGraph graph = model.getGraph();
final Spot a = graph.addVertex().init( 0, new double[] { 1, 2, 3 }, 1 );
final Spot b = graph.addVertex().init( 1, new double[] { 1, 2, 3.2 }, 1 );
a.setLabel( "A" );
b.setLabel( "B" );
graph.addEdge( a, b ).init();
String actual = ModelUtils.dump( model );
String expexted = "Model " + model + "\n"
final String actual = ModelUtils.dump( model );
final String expexted = "Model " + model + "\n"
+ "Spots:\n"
+ " Id Label Frame X Y Z N incoming links N outgoing links Spot N links Spot frame X Y Z Spot radius\n"
+ " (pixel) (pixel) (pixel) (pixel) (pixel) (pixel) (pixel)\n"
Expand All @@ -72,24 +72,24 @@ public void testDump()
@Test
public void testDumpWithTagSets()
{
Model model = new Model();
ModelGraph graph = model.getGraph();
Spot a = graph.addVertex().init( 0, new double[] { 1, 2, 3 }, 1 );
Spot b = graph.addVertex().init( 1, new double[] { 1, 2, 3.2 }, 1 );
final Model model = new Model();
final ModelGraph graph = model.getGraph();
final Spot a = graph.addVertex().init( 0, new double[] { 1, 2, 3 }, 1 );
final Spot b = graph.addVertex().init( 1, new double[] { 1, 2, 3.2 }, 1 );
a.setLabel( "A" );
b.setLabel( "B" );
Link edge = graph.addEdge( a, b ).init();
final Link edge = graph.addEdge( a, b ).init();
TagSetUtils.addNewTagSetToModel( model, "my tag set", Arrays.asList(
Pair.of( "tag1", Color.YELLOW.getRGB() ),
Pair.of( "tag2", Color.BLUE.getRGB() )
) );
TagHelper tag1 = new TagHelper( model, "my tag set", "tag1" );
final TagHelper tag1 = new TagHelper( model, "my tag set", "tag1" );
tag1.tagSpot( a );
TagHelper tag2 = new TagHelper( model, "my tag set", "tag2" );
final TagHelper tag2 = new TagHelper( model, "my tag set", "tag2" );
tag2.tagSpot( b );
tag2.tagLink( edge );
String actual = ModelUtils.dump( model, ModelUtils.DumpFlags.PRINT_TAGS );
String expected = "Spots:\n"
final String actual = ModelUtils.dump( model, ModelUtils.DumpFlags.PRINT_TAGS );
final String expected = "Spots:\n"
+ " Id Label Frame X Y Z my tag set\n"
+ " (pixel) (pixel) (pixel) \n"
+ "-------------------------------------------------------------------------\n"
Expand Down

0 comments on commit 53c469b

Please sign in to comment.