public void register(Class<? extends Measurement> measurementType, Measurement.Listener listener) { try { // A bit of a hack to cache this measurement's ID field so we // can deserialize incoming measurements of this type. Why don't we // have a getId() in the Measurement interface? Ah, because it would // have to be static and you can't have static methods in an // interface. It would work if we were passed an instance of the // measurement in this function, but we don't really have that when // adding a listener. BaseMeasurement.getKeyForMeasurement(measurementType); } catch(UnrecognizedMeasurementTypeException e) { } mMeasurementTypeListeners.put(measurementType, listener); }
@Test(expected=UnrecognizedMeasurementTypeException.class) public void getKeyForMeasurementMissingIdField() throws UnrecognizedMeasurementTypeException { BaseMeasurement.getKeyForMeasurement(InvalidMeasurementType.class); }
@Test(expected=UnrecognizedMeasurementTypeException.class) public void getKeyForMeasurementPrivateID() throws UnrecognizedMeasurementTypeException { BaseMeasurement.getKeyForMeasurement(PrivateIdFieldMeasurement.class); }
/** * Retrieve the most current value of a measurement. * * @param measurementType The class of the requested Measurement * (e.g. VehicleSpeed.class) * @return An instance of the requested Measurement which may or may * not have a value. * @throws UnrecognizedMeasurementTypeException if passed a measurementType * that does not extend Measurement * @throws NoValueException if no value has yet been received for this * measurementType * @see BaseMeasurement */ public Measurement get( Class<? extends Measurement> measurementType) throws UnrecognizedMeasurementTypeException, NoValueException { return BaseMeasurement.getMeasurementFromMessage(measurementType, get(BaseMeasurement.getKeyForMeasurement(measurementType)).asSimpleMessage()); }
@Test public void getKeyForUnrecognizedMeasurementWithProperId() throws UnrecognizedMeasurementTypeException { assertThat(BaseMeasurement.getKeyForMeasurement(NewMeasurement.class), equalTo(new NamedVehicleMessage(NewMeasurement.ID).getKey())); }