Skip to content

Commit c4032a9

Browse files
committed
Improve DRYness.
1 parent 866e0be commit c4032a9

1 file changed

Lines changed: 36 additions & 84 deletions

File tree

src/main/java/fiji/plugin/trackmate/helper/model/ParameterSweepModelIO.java

Lines changed: 36 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -179,89 +179,19 @@ public static ParameterSweepModel fromJson( final String str )
179179
return model;
180180
}
181181

182-
private static class DetectorSweepModelAdapter implements JsonSerializer< DetectorSweepModel >, JsonDeserializer< DetectorSweepModel >
183-
{
184-
185-
@Override
186-
public DetectorSweepModel deserialize( final JsonElement json, final Type typeOfT, final JsonDeserializationContext context ) throws JsonParseException
187-
{
188-
final JsonObject jsonObject = json.getAsJsonObject();
189-
final String type = jsonObject.get( "type" ).getAsString();
190-
final JsonElement element = jsonObject.get( "properties" );
191-
192-
/*
193-
* Is the module of the model we are trying to deserialize
194-
* available?
195-
*/
196-
try
197-
{
198-
final Object obj = Class.forName( "fiji.plugin.trackmate.helper.model.detector." + type ).getConstructor().newInstance();
199-
final DetectorSweepModel m = ( DetectorSweepModel ) obj;
200-
if ( m.factory == null )
201-
{
202-
/*
203-
* Non de-serialized version (will correctly show a message
204-
* about a missing module, even if the JSon files referred
205-
* to an installed module.
206-
*/
207-
return m;
208-
}
209-
}
210-
catch ( InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e )
211-
{
212-
e.printStackTrace();
213-
}
214182

215-
/*
216-
* The module is available. But does the JSon file refers to a
217-
* missing module?
218-
*/
219-
try
220-
{
221-
final JsonElement properiesElement = json.getAsJsonObject().get( "properties" );
222-
final JsonElement factoryElement = properiesElement.getAsJsonObject().get( "factory" );
223-
if ( factoryElement == null )
224-
{
225-
/*
226-
* The JSon says that when it was saved it could not find
227-
* the module. But now we can. Substitute a default version
228-
* properly initialized.
229-
*/
230-
try
231-
{
232-
final Object obj2 = Class.forName( "fiji.plugin.trackmate.helper.model.detector." + type ).getConstructor().newInstance();
233-
return ( DetectorSweepModel ) obj2;
234-
}
235-
catch ( InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e )
236-
{
237-
e.printStackTrace();
238-
}
239-
}
183+
private static abstract class AbstractModuleSweepModelAdapter< G extends AbstractSweepModel< ? > > implements JsonSerializer< G >, JsonDeserializer< G >
184+
{
240185

241-
// Normal case.
242-
return context.deserialize( element, Class.forName( "fiji.plugin.trackmate.helper.model.detector." + type ) );
243-
}
244-
catch ( final ClassNotFoundException cnfe )
245-
{
246-
throw new JsonParseException( "Unknown element type: " + type, cnfe );
247-
}
248-
}
186+
private final String packagePrefix;
249187

250-
@Override
251-
public JsonElement serialize( final DetectorSweepModel src, final Type typeOfSrc, final JsonSerializationContext context )
188+
public AbstractModuleSweepModelAdapter( final String packagePrefix )
252189
{
253-
final JsonObject result = new JsonObject();
254-
result.add( "type", new JsonPrimitive( src.getClass().getSimpleName() ) );
255-
result.add( "properties", context.serialize( src, src.getClass() ) );
256-
return result;
190+
this.packagePrefix = packagePrefix;
257191
}
258-
}
259-
260-
private static class TrackerSweepModelAdapter implements JsonSerializer< TrackerSweepModel >, JsonDeserializer< TrackerSweepModel >
261-
{
262192

263193
@Override
264-
public TrackerSweepModel deserialize( final JsonElement json, final Type typeOfT, final JsonDeserializationContext context ) throws JsonParseException
194+
public G deserialize( final JsonElement json, final Type typeOfT, final JsonDeserializationContext context ) throws JsonParseException
265195
{
266196
final JsonObject jsonObject = json.getAsJsonObject();
267197
final String type = jsonObject.get( "type" ).getAsString();
@@ -273,8 +203,9 @@ public TrackerSweepModel deserialize( final JsonElement json, final Type typeOfT
273203
*/
274204
try
275205
{
276-
final Object obj = Class.forName( "fiji.plugin.trackmate.helper.model.tracker." + type ).getConstructor().newInstance();
277-
final TrackerSweepModel m = ( TrackerSweepModel ) obj;
206+
final Object obj = Class.forName( packagePrefix + type ).getConstructor().newInstance();
207+
@SuppressWarnings( "unchecked" )
208+
final G m = ( G ) obj;
278209
if ( m.factory == null )
279210
{
280211
/*
@@ -307,8 +238,9 @@ public TrackerSweepModel deserialize( final JsonElement json, final Type typeOfT
307238
*/
308239
try
309240
{
310-
final Object obj2 = Class.forName( "fiji.plugin.trackmate.helper.model.tracker." + type ).getConstructor().newInstance();
311-
return ( TrackerSweepModel ) obj2;
241+
@SuppressWarnings( "unchecked" )
242+
final G obj2 = ( G ) Class.forName( packagePrefix + type ).getConstructor().newInstance();
243+
return obj2;
312244
}
313245
catch ( InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e )
314246
{
@@ -317,7 +249,8 @@ public TrackerSweepModel deserialize( final JsonElement json, final Type typeOfT
317249
}
318250

319251
// Deserialize from Json
320-
final TrackerSweepModel ds = ( TrackerSweepModel ) context.deserialize( element, Class.forName( "fiji.plugin.trackmate.helper.model.tracker." + type ) );
252+
@SuppressWarnings( "unchecked" )
253+
final G ds = ( G ) context.deserialize( element, Class.forName( packagePrefix + type ) );
321254

322255
/*
323256
* We may have serialized that the module was not available or
@@ -335,8 +268,9 @@ public TrackerSweepModel deserialize( final JsonElement json, final Type typeOfT
335268
{
336269
try
337270
{
338-
final Object obj2 = Class.forName( "fiji.plugin.trackmate.helper.model.tracker." + type ).getConstructor().newInstance();
339-
return ( TrackerSweepModel ) obj2;
271+
@SuppressWarnings( "unchecked" )
272+
final G obj2 = ( G ) Class.forName( packagePrefix + type ).getConstructor().newInstance();
273+
return obj2;
340274
}
341275
catch ( InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e )
342276
{
@@ -354,7 +288,7 @@ public TrackerSweepModel deserialize( final JsonElement json, final Type typeOfT
354288
}
355289

356290
@Override
357-
public JsonElement serialize( final TrackerSweepModel src, final Type typeOfSrc, final JsonSerializationContext context )
291+
public JsonElement serialize( final G src, final Type typeOfSrc, final JsonSerializationContext context )
358292
{
359293
final JsonObject result = new JsonObject();
360294
result.add( "type", new JsonPrimitive( src.getClass().getSimpleName() ) );
@@ -363,6 +297,24 @@ public JsonElement serialize( final TrackerSweepModel src, final Type typeOfSrc,
363297
}
364298
}
365299

300+
private static class DetectorSweepModelAdapter extends AbstractModuleSweepModelAdapter< DetectorSweepModel >
301+
{
302+
public DetectorSweepModelAdapter()
303+
{
304+
super( "fiji.plugin.trackmate.helper.model.detector." );
305+
}
306+
}
307+
308+
private static class TrackerSweepModelAdapter extends AbstractModuleSweepModelAdapter< TrackerSweepModel >
309+
{
310+
311+
public TrackerSweepModelAdapter()
312+
{
313+
super( "fiji.plugin.trackmate.helper.model.tracker." );
314+
}
315+
316+
}
317+
366318
public static class ClassTypeAdapter implements JsonSerializer< Class< ? > >, JsonDeserializer< Class< ? > >
367319
{
368320

0 commit comments

Comments
 (0)