Skip to content

Commit 5f1204e

Browse files
IntegerUtils class
Original PR: LabKey/platform#6933
1 parent 5af24e5 commit 5f1204e

5 files changed

Lines changed: 24 additions & 20 deletions

File tree

studydesign/src/org/labkey/studydesign/model/AssaySpecimenConfigImpl.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Map;
3030
import java.util.Objects;
3131

32+
import static org.labkey.api.util.IntegerUtils.asIntegerElseNull;
33+
3234
/**
3335
* Represents an assay/specimen configuration for a study
3436
*/
@@ -251,26 +253,26 @@ public static AssaySpecimenConfigImpl fromJSON(@NotNull JSONObject o, Container
251253

252254
if (o.has("RowId"))
253255
assay.setRowId(o.getInt("RowId"));
254-
if (o.has("DataSet") && (o.get("DataSet") instanceof Integer || o.get("DataSet") instanceof Long) && o.getInt("DataSet") > 0)
255-
assay.setDataset(o.getInt("DataSet"));
256+
if (o.has("DataSet") && asIntegerElseNull(o.get("DataSet")) instanceof Integer dataset && dataset > 0)
257+
assay.setDataset(dataset);
256258
if (o.has("Source") && !StringUtils.isEmpty(o.getString("Source")))
257259
assay.setSource(o.getString("Source"));
258-
if (o.has("LocationId") && (o.get("LocationId") instanceof Integer || o.get("LocationId") instanceof Long) && o.getInt("LocationId") > 0)
260+
if (o.has("LocationId") && asIntegerElseNull(o.get("LocationId")) instanceof Integer locationId && locationId > 0)
259261
assay.setLocationId(o.getInt("LocationId"));
260262
if (o.has("TubeType") && !StringUtils.isEmpty(o.getString("TubeType")))
261263
assay.setTubeType(o.getString("TubeType"));
262264
if (o.has("Lab") && !StringUtils.isEmpty(o.getString("Lab")))
263265
assay.setLab(o.getString("Lab"));
264266
if (o.has("SampleType") && !StringUtils.isEmpty(o.getString("SampleType")))
265267
assay.setSampleType(o.getString("SampleType"));
266-
if (o.has("SampleQuantity") && (o.get("SampleQuantity") instanceof Integer || o.get("SampleQuantity") instanceof Double) && o.getDouble("SampleQuantity") > 0)
267-
assay.setSampleQuantity(o.getDouble("SampleQuantity"));
268+
if (o.has("SampleQuantity") && o.get("SampleQuantity") instanceof Number sampleQuantity && sampleQuantity.doubleValue() > 0)
269+
assay.setSampleQuantity(sampleQuantity.doubleValue());
268270
if (o.has("SampleUnits") && !StringUtils.isEmpty(o.getString("SampleUnits")))
269271
assay.setSampleUnits(o.getString("SampleUnits"));
270-
if (o.has("PrimaryTypeId") && (o.get("PrimaryTypeId") instanceof Integer || o.get("PrimaryTypeId") instanceof Long))
271-
assay.setPrimaryTypeId(o.getInt("PrimaryTypeId"));
272-
if (o.has("DerivativeTypeId") && (o.get("DerivativeTypeId") instanceof Integer || o.get("DerivativeTypeId") instanceof Long))
273-
assay.setDerivativeTypeId(o.getInt("DerivativeTypeId"));
272+
if (o.has("PrimaryTypeId") && asIntegerElseNull(o.get("PrimaryTypeId")) instanceof Integer primaryTypeId)
273+
assay.setPrimaryTypeId(primaryTypeId);
274+
if (o.has("DerivativeTypeId") && asIntegerElseNull(o.get("DerivativeTypeId")) instanceof Integer derivativeTypeId)
275+
assay.setDerivativeTypeId(derivativeTypeId);
274276

275277
JSONArray visitMapJSON = o.optJSONArray("VisitMap");
276278
if (visitMapJSON != null)

studydesign/src/org/labkey/studydesign/model/TreatmentImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131

32-
import static org.labkey.api.exp.api.ExperimentService.asInteger;
32+
import static org.labkey.api.util.IntegerUtils.asIntegerElseNull;
3333

3434
/**
3535
* User: cnathe
@@ -170,8 +170,8 @@ public static TreatmentImpl fromJSON(@NotNull JSONObject o, Container container)
170170

171171
if (o.has("RowId"))
172172
{
173-
if (o.get("RowId") instanceof Integer || o.get("RowId") instanceof Integer)
174-
treatment.setRowId(asInteger("RowId"));
173+
if (asIntegerElseNull(o.get("RowId")) instanceof Integer rowId)
174+
treatment.setRowId(rowId);
175175
else
176176
treatment.setTempRowId(o.getString("RowId"));
177177
}

studydesign/src/org/labkey/studydesign/model/TreatmentManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
import java.util.Map;
6666
import java.util.Set;
6767

68-
import static org.labkey.api.exp.api.ExperimentService.asInteger;
68+
import static org.labkey.api.util.IntegerUtils.asInteger;
6969

7070
/**
7171
* Created by cnathe on 1/24/14.

studydesign/src/org/labkey/studydesign/model/TreatmentProductImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.HashMap;
2525
import java.util.Map;
2626

27-
import static org.labkey.api.exp.api.ExperimentService.asInteger;
27+
import static org.labkey.api.util.IntegerUtils.asIntegerElseNull;
2828

2929
/**
3030
* User: cnathe
@@ -192,10 +192,10 @@ public static TreatmentProductImpl fromJSON(@NotNull JSONObject o, Container con
192192
//treatmentProduct.setDose(o.getString("Dose"));
193193
//treatmentProduct.setRoute(o.getString("Route"));
194194
treatmentProduct.setContainer(container);
195-
if (o.has("ProductId") && (o.get("ProductId") instanceof Integer || o.get("ProductId") instanceof Long))
196-
treatmentProduct.setProductId(asInteger(o.get("ProductId")));
197-
if (o.has("TreatmentId") && (o.get("TreatmentId") instanceof Integer || o.get("TreatmentId") instanceof Long))
198-
treatmentProduct.setTreatmentId(asInteger(o.get("TreatmentId")));
195+
if (o.has("ProductId") && asIntegerElseNull(o.get("ProductId")) instanceof Integer productId)
196+
treatmentProduct.setProductId(productId);
197+
if (o.has("TreatmentId") && asIntegerElseNull(o.get("TreatmentId")) instanceof Integer treatmentId)
198+
treatmentProduct.setTreatmentId(treatmentId);
199199
if (o.has("RowId"))
200200
treatmentProduct.setRowId(o.getInt("RowId"));
201201
if (o.has("DoseAndRoute"))

studydesign/src/org/labkey/studydesign/model/TreatmentVisitMapImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.json.JSONObject;
2020
import org.labkey.api.data.Container;
2121

22+
import static org.labkey.api.util.IntegerUtils.asIntegerElseNull;
23+
2224
/**
2325
* User: cnathe
2426
* Date: 12/30/13
@@ -110,8 +112,8 @@ public static TreatmentVisitMapImpl fromJSON(@NotNull JSONObject o)
110112
visitMap.setCohortId(o.getInt("CohortId"));
111113
if (o.has("TreatmentId"))
112114
{
113-
if (o.get("TreatmentId") instanceof Integer || o.get("TreatmentId") instanceof Long)
114-
visitMap.setTreatmentId(o.getInt("TreatmentId"));
115+
if (asIntegerElseNull(o.get("TreatmentId")) instanceof Integer treatmentId)
116+
visitMap.setTreatmentId(treatmentId);
115117
else
116118
visitMap.setTempTreatmentId(o.getString("TreatmentId"));
117119
}

0 commit comments

Comments
 (0)