private synchronized void propagateMeasurementFromMessage( SimpleVehicleMessage message) { try { Measurement measurement = BaseMeasurement.getMeasurementFromMessage(message); if(mMeasurementTypeListeners.containsKey(measurement.getClass())) { for(Measurement.Listener listener : mMeasurementTypeListeners.get(measurement.getClass())) { listener.receive(measurement); } } } catch(UnrecognizedMeasurementTypeException e) { // The message is not a recognized Measurement, we don't propagate // it as a Measurement (only as a Message, which is handled // earlier). } catch(NoValueException e) { Log.w(TAG, "Received notification for a blank measurement", e); } } }
/** * 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()); }
@Override public boolean equals(Object obj) { if(this == obj) { return true; } if(obj == null) { return false; } if(getClass() != obj.getClass()) { return false; } @SuppressWarnings("unchecked") final BaseMeasurement<TheUnit> other = (BaseMeasurement<TheUnit>) obj; return Objects.equal(getValue(), other.getValue()) && Objects.equal(other.getRange(), getRange()); }
public static Measurement getMeasurementFromMessage( SimpleVehicleMessage message) throws UnrecognizedMeasurementTypeException, NoValueException { Class<? extends Measurement> measurementClass = BaseMeasurement.getClassForId(message.getName()); return BaseMeasurement.getMeasurementFromMessage(measurementClass, message); }
@Override public SimpleVehicleMessage toVehicleMessage() { return new SimpleVehicleMessage(mValue.getTimestamp(), getGenericName(), getSerializedValue()); }
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); }
@Override public String toString() { return getValue().toString(); } }
@Test public void emptyRange() { assertThat(measurement.getRange(), nullValue()); }
/** * Construct a new Measurement with the given value. * * @param value the TheUnit this measurement represents. */ public BaseMeasurement(TheUnit value) { if(!sMeasurementIdToClass.inverse().containsKey(this.getClass())) { try { cacheMeasurementId(this.getClass()); } catch(UnrecognizedMeasurementTypeException e) { Log.w(TAG, "Incomplete BaseMeasurement subclass", e); } } mValue = new AgingData<>(value); }
public String getName(Context context) { String name = getGenericName(); if(!sCachedPrettyNames.containsKey(getClass())) { // Make sure to not use the package name here, we have to find the // resource using the package name of the app using the library instead. int identifier = context.getResources().getIdentifier( getGenericName() + "_label", "string", context.getPackageName()); if(identifier != 0) { name = context.getString(identifier); sCachedPrettyNames.put(getClass(), name); } } else if(sCachedPrettyNames.get(getClass()) != null) { name = sCachedPrettyNames.get(getClass()); } return name; }
@Override public boolean equals(Object obj) { if(this == obj) { return true; } if(!super.equals(obj) || getClass() != obj.getClass()) { return false; } @SuppressWarnings("unchecked") final EventedMeasurement<TheUnit> other = (EventedMeasurement<TheUnit>) obj; return Objects.equal(getEvent(), other.getEvent()); } }
@Test(expected=UnrecognizedMeasurementTypeException.class) public void getKeyForMeasurementPrivateID() throws UnrecognizedMeasurementTypeException { BaseMeasurement.getKeyForMeasurement(PrivateIdFieldMeasurement.class); }
@Override public Object getSerializedValue() { return getValue().getSerializedValue(); }
public static MessageKey getKeyForMeasurement( Class<? extends Measurement> measurementType) throws UnrecognizedMeasurementTypeException { if(!sMeasurementIdToClass.inverse().containsKey(measurementType)) { cacheMeasurementId(measurementType); } return new NamedVehicleMessage( sMeasurementIdToClass.inverse().get(measurementType)).getKey(); }
@Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = LayoutInflater.from(mContext) .inflate(R.layout.dashboard_list_item, parent, false); } SimpleVehicleMessage message = getItem(position); TextView nameView = (TextView) convertView.findViewById(R.id.name); TextView valueView = (TextView) convertView.findViewById(R.id.value); try { Measurement measurement = BaseMeasurement.getMeasurementFromMessage(message); nameView.setText("" + measurement.getName(mContext)); valueView.setText("" + measurement.toString()); } catch(UnrecognizedMeasurementTypeException e) { nameView.setText("" + message.getName()); valueView.setText("" + message.getValue()); } catch(NoValueException e) { } return convertView; }
@Test(expected=UnrecognizedMeasurementTypeException.class) public void getKeyForMeasurementMissingIdField() throws UnrecognizedMeasurementTypeException { BaseMeasurement.getKeyForMeasurement(InvalidMeasurementType.class); }
@Test public void get() { assertThat(measurement.getValue().doubleValue(), equalTo(10.0)); } }
private void checkDeserializeSerialize(Measurement measurement) throws UnrecognizedMeasurementTypeException, NoValueException { SimpleVehicleMessage message = measurement.toVehicleMessage(); Measurement deserializedMeasurement = BaseMeasurement.getMeasurementFromMessage(message); assertThat(deserializedMeasurement, instanceOf(measurement.getClass())); assertThat(deserializedMeasurement, equalTo(measurement)); }
@Test public void getKeyForUnrecognizedMeasurementWithProperId() throws UnrecognizedMeasurementTypeException { assertThat(BaseMeasurement.getKeyForMeasurement(NewMeasurement.class), equalTo(new NamedVehicleMessage(NewMeasurement.ID).getKey())); }
@Test(expected=NoValueException.class) public void buildFromNull() throws NoValueException, UnrecognizedMeasurementTypeException { BaseMeasurement.getMeasurementFromMessage(VehicleSpeed.class, null); }