Copyright (c) 2008-2011 Vrije Universiteit, The Netherlands
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the follo...
If you think the Android project interdroid-swan listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package interdroid.swan.sensors;
//fromwww.java2s.comimport interdroid.swan.swansong.TimestampedValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Abstract class that implements basic functionality for sensors. Descendants
* only have to implement requestReading() and onEntityServiceLevelChange(). The
* rest can be overridden optionally.
*/publicabstractclass AbstractMemorySensor extends AbstractSensorBase {
/**
* The map of values for this sensor.
*/privatefinal Map<String, List<TimestampedValue>> values = new HashMap<String, List<TimestampedValue>>();
privatelong mReadings = 0;
privatelong mLastReadingTimestamp = 0;
/**
* @return the values
*/publicfinal Map<String, List<TimestampedValue>> getValues() {
return values;
}
@Override
publicfinalvoid init() {
for (String valuePath : VALUE_PATHS) {
expressionIdsPerValuePath.put(valuePath, new ArrayList<String>());
getValues()
.put(valuePath,
Collections
.synchronizedList(new ArrayList<TimestampedValue>()));
}
}
/**
* Trims the values to the given length.
*
* @param history
* the number of items to keep
*/privatefinalvoid trimValues(finalint history) {
for (String path : VALUE_PATHS) {
if (getValues().get(path).size() >= history) {
getValues().get(path).remove(getValues().get(path).size() - 1);
}
}
}
/**
* Adds a value for the given value path to the history.
*
* @param valuePath
* the value path
* @param now
* the current time
* @param value
* the value
* @param historySize
* the history size
*/protectedfinalvoid putValueTrimSize(final String valuePath,
final String id, finallong now, final Object value,
finalint historySize) {
updateReadings(now);
getValues().get(valuePath).add(0, new TimestampedValue(value, now));
trimValues(historySize);
if (id != null) {
notifyDataChangedForId(id);
} else {
notifyDataChanged(valuePath);
}
}
/**
* Adds a value for the given value path to the history.
*
* @param valuePath
* the value path
* @param now
* the current time
* @param value
* the value
* @param historyLength
* the history length
*/protectedfinalvoid putValueTrimTime(final String valuePath,
final String id, finallong now, final Object value,
finallong historyLength) {
updateReadings(now);
getValues().get(valuePath).add(0, new TimestampedValue(value, now));
trimValueByTime(now - historyLength);
if (id != null) {
notifyDataChangedForId(id);
} else {
notifyDataChanged(valuePath);
}
}
privatevoid updateReadings(long now) {
if (now != mLastReadingTimestamp) {
mReadings++;
mLastReadingTimestamp = now;
}
}
/**
* Trims values past the given expire time.
*
* @param expire
* the time to trim after
*/privatefinalvoid trimValueByTime(finallong expire) {
for (String valuePath : VALUE_PATHS) {
List<TimestampedValue> values = getValues().get(valuePath);
while ((values.size() > 0 && values.get(values.size() - 1)
.getTimestamp() < expire)) {
values.remove(values.size() - 1);
}
}
}
@Override
publicfinal List<TimestampedValue> getValues(final String id,
finallong now, finallong timespan) {
return getValuesForTimeSpan(values.get(registeredValuePaths.get(id)),
now, timespan);
}
@Override
publiclong getReadings() {
return mReadings;
}
}