com.geotab.model.serialization.UnitOfMeasureDeserializer Maven / Gradle / Ivy
/*
*
* 2020 Copyright (C) Geotab Inc. All rights reserved.
*/
package com.geotab.model.serialization;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasure.UNIT_OF_MEASURE_NONE_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureAmps.UNIT_OF_MEASURE_AMPS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureBytes.UNIT_OF_MEASURE_BYTES_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureCubicMeters.UNIT_OF_MEASURE_CUBIC_METERS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureCubicMetersPerSecond.UNIT_OF_MEASURE_CUBIC_METERS_PER_SECOND_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureDegreesCelsius.UNIT_OF_MEASURE_DEGREES_CELSIUS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureGrams.UNIT_OF_MEASURE_GRAMS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureGramsPerSecond.UNIT_OF_MEASURE_GRAMS_PER_SECOND_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureGramsPerSquareMeter.UNIT_OF_MEASURE_GRAMS_PER_SQUARE_METER_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureHertz.UNIT_OF_MEASURE_HERTZ_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureKiloWattHours.UNIT_OF_MEASURE_KILO_WATT_HOURS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilogramsPerKilometer.UNIT_OF_MEASURE_KILOGRAMS_PER_KILOMETER_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilometersPerHour.UNIT_OF_MEASURE_KILOMETERS_PER_HOUR_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilometersPerKilogram.UNIT_OF_MEASURE_KILOMETERS_PER_KILOGRAM_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilometersPerLiter.UNIT_OF_MEASURE_KILOMETERS_PER_LITER_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureLiters.UNIT_OF_MEASURE_LITERS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureLitersPerLaneKilometer.UNIT_OF_MEASURE_LITERS_PER_LANE_KILOMETER_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureLitersPerTonne.UNIT_OF_MEASURE_LITERS_PER_TONNE_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureMeters.UNIT_OF_MEASURE_METERS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureMetersPerSecondSquared.UNIT_OF_MEASURE_METERS_PER_SECOND_SQUARED_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureNewtonMeters.UNIT_OF_MEASURE_NEWTON_METERS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureNewtons.UNIT_OF_MEASURE_NEWTONS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureOhms.UNIT_OF_MEASURE_OHMS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureOhmsPerSecond.UNIT_OF_MEASURE_OHMS_PER_SECOND_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasurePartsPerMillion.UNIT_OF_MEASURE_PARTS_PER_MILLION_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasurePascals.UNIT_OF_MEASURE_PASCALS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasurePercentage.UNIT_OF_MEASURE_PERCENTAGE_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasurePulsesPerMeter.UNIT_OF_MEASURE_PULSES_PER_METER_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureRadians.UNIT_OF_MEASURE_RADIANS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureRevolutions.UNIT_OF_MEASURE_REVOLUTIONS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureRevolutionsPerMinute.UNIT_OF_MEASURE_REVOLUTIONS_PER_MINUTE_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureSeconds.UNIT_OF_MEASURE_SECONDS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureVolts.UNIT_OF_MEASURE_VOLTS_ID;
import static com.geotab.model.entity.unitofmeasure.UnitOfMeasureWatts.UNIT_OF_MEASURE_WATTS_ID;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.geotab.model.Id;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasure;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureAmps;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureBytes;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureCubicMeters;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureCubicMetersPerSecond;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureDegreesCelsius;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureGrams;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureGramsPerSecond;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureGramsPerSquareMeter;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureHertz;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureKiloWattHours;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilogramsPerKilometer;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilometersPerHour;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilometersPerKilogram;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureKilometersPerLiter;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureLiters;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureLitersPerLaneKilometer;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureLitersPerTonne;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureMeters;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureMetersPerSecondSquared;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureNewtonMeters;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureNewtons;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureNone;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureOhms;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureOhmsPerSecond;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasurePartsPerMillion;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasurePascals;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasurePercentage;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasurePulsesPerMeter;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureRadians;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureRevolutions;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureRevolutionsPerMinute;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureSeconds;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureVolts;
import com.geotab.model.entity.unitofmeasure.UnitOfMeasureWatts;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
public class UnitOfMeasureDeserializer extends JsonDeserializer {
@Override
public UnitOfMeasure deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException {
ObjectCodec parserCodec = jsonParser.getCodec();
JsonNode node = parserCodec.readTree(jsonParser);
if (node.isTextual()) {
return buildUnitOfMeasure(node.textValue());
} else if (node.isObject()) {
Id id = node.get("id") != null ? parserCodec.treeToValue(node.get("id"), Id.class) : null;
return buildUnitOfMeasure(id != null ? id.getId() : null);
}
return null;
}
private UnitOfMeasure buildUnitOfMeasure(String id) {
if (StringUtils.isEmpty(id)) {
return null;
}
switch (id) {
case UNIT_OF_MEASURE_AMPS_ID:
return UnitOfMeasureAmps.getInstance();
case UNIT_OF_MEASURE_BYTES_ID:
return UnitOfMeasureBytes.getInstance();
case UNIT_OF_MEASURE_CUBIC_METERS_ID:
return UnitOfMeasureCubicMeters.getInstance();
case UNIT_OF_MEASURE_CUBIC_METERS_PER_SECOND_ID:
return UnitOfMeasureCubicMetersPerSecond.getInstance();
case UNIT_OF_MEASURE_DEGREES_CELSIUS_ID:
return UnitOfMeasureDegreesCelsius.getInstance();
case UNIT_OF_MEASURE_GRAMS_ID:
return UnitOfMeasureGrams.getInstance();
case UNIT_OF_MEASURE_GRAMS_PER_SECOND_ID:
return UnitOfMeasureGramsPerSecond.getInstance();
case UNIT_OF_MEASURE_GRAMS_PER_SQUARE_METER_ID:
return UnitOfMeasureGramsPerSquareMeter.getInstance();
case UNIT_OF_MEASURE_HERTZ_ID:
return UnitOfMeasureHertz.getInstance();
case UNIT_OF_MEASURE_KILOGRAMS_PER_KILOMETER_ID:
return UnitOfMeasureKilogramsPerKilometer.getInstance();
case UNIT_OF_MEASURE_KILOMETERS_PER_HOUR_ID:
return UnitOfMeasureKilometersPerHour.getInstance();
case UNIT_OF_MEASURE_KILOMETERS_PER_KILOGRAM_ID:
return UnitOfMeasureKilometersPerKilogram.getInstance();
case UNIT_OF_MEASURE_KILOMETERS_PER_LITER_ID:
return UnitOfMeasureKilometersPerLiter.getInstance();
case UNIT_OF_MEASURE_KILO_WATT_HOURS_ID:
return UnitOfMeasureKiloWattHours.getInstance();
case UNIT_OF_MEASURE_LITERS_ID:
return UnitOfMeasureLiters.getInstance();
case UNIT_OF_MEASURE_LITERS_PER_LANE_KILOMETER_ID:
return UnitOfMeasureLitersPerLaneKilometer.getInstance();
case UNIT_OF_MEASURE_LITERS_PER_TONNE_ID:
return UnitOfMeasureLitersPerTonne.getInstance();
case UNIT_OF_MEASURE_METERS_ID:
return UnitOfMeasureMeters.getInstance();
case UNIT_OF_MEASURE_METERS_PER_SECOND_SQUARED_ID:
return UnitOfMeasureMetersPerSecondSquared.getInstance();
case UNIT_OF_MEASURE_NEWTON_METERS_ID:
return UnitOfMeasureNewtonMeters.getInstance();
case UNIT_OF_MEASURE_NEWTONS_ID:
return UnitOfMeasureNewtons.getInstance();
case UNIT_OF_MEASURE_NONE_ID:
return UnitOfMeasureNone.getInstance();
case UNIT_OF_MEASURE_OHMS_ID:
return UnitOfMeasureOhms.getInstance();
case UNIT_OF_MEASURE_OHMS_PER_SECOND_ID:
return UnitOfMeasureOhmsPerSecond.getInstance();
case UNIT_OF_MEASURE_PARTS_PER_MILLION_ID:
return UnitOfMeasurePartsPerMillion.getInstance();
case UNIT_OF_MEASURE_PASCALS_ID:
return UnitOfMeasurePascals.getInstance();
case UNIT_OF_MEASURE_PERCENTAGE_ID:
return UnitOfMeasurePercentage.getInstance();
case UNIT_OF_MEASURE_PULSES_PER_METER_ID:
return UnitOfMeasurePulsesPerMeter.getInstance();
case UNIT_OF_MEASURE_RADIANS_ID:
return UnitOfMeasureRadians.getInstance();
case UNIT_OF_MEASURE_REVOLUTIONS_ID:
return UnitOfMeasureRevolutions.getInstance();
case UNIT_OF_MEASURE_REVOLUTIONS_PER_MINUTE_ID:
return UnitOfMeasureRevolutionsPerMinute.getInstance();
case UNIT_OF_MEASURE_SECONDS_ID:
return UnitOfMeasureSeconds.getInstance();
case UNIT_OF_MEASURE_VOLTS_ID:
return UnitOfMeasureVolts.getInstance();
case UNIT_OF_MEASURE_WATTS_ID:
return UnitOfMeasureWatts.getInstance();
default:
return UnitOfMeasure.builder().id(id).name(id).build();
}
}
}