I have a filter class in which the user must declare a type (eg Filter<Double>, Filter<Float>etc.). Then the class implements a moving average filter, so objects in the class must be added. My question is: how to do this? Sorry if the answer is simple, but I got confused thinking about it too much, I think: p.
public abstract class FilterData<T>
{
private final List<T> mFilter;
private T mFilteredValue;
protected Integer mSize = 10;
private T mUnfilteredValue;
public FilterData()
{
mFilter = new ArrayList<T>();
}
public FilterData(int size)
{
mSize = size;
mFilter = new ArrayList<T>(mSize);
}
public abstract T add(final T pFirstValue, final T pSecondValue);
@SuppressWarnings("unchecked")
public T filter(T currentVal)
{
T filteredVal;
mUnfilteredValue = currentVal;
push(currentVal);
T totalVal = (T) (new Integer(0));
int numNonZeros = 1;
for (int i = 0; i < mFilter.size(); ++i)
{
if (mFilter.get(i) != (T) (new Integer(0)))
{
++numNonZeros;
T totalValDouble = add(mFilter.get(i), totalVal);
totalVal = totalValDouble;
}
}
Double filteredValDouble = (Double) totalVal / new Double(numNonZeros);
filteredVal = (T) filteredValDouble;
mFilteredValue = filteredVal;
return filteredVal;
}
public T getFilteredValue()
{
return mFilteredValue;
}
public List<T> getFilterStream()
{
return mFilter;
}
public T getUnfilteredValue()
{
return mUnfilteredValue;
}
public void push(T currentVal)
{
mFilter.add(0, currentVal);
if (mFilter.size() > mSize)
mFilter.remove(mFilter.size() - 1);
}
public void resizeFilter(int newSize)
{
if (mSize > newSize)
{
int numItemsToRemove = mSize - newSize;
for (int i = 0; i < numItemsToRemove; ++i)
{
mFilter.remove(mFilter.size() - 1);
}
}
}
}
I have the right to include the abstract Add method, and if so, how should I properly extend the class to cover primitive types (e.g. Float, Double, Integer, etc.), thanks to Chris
EDIT:
. , , . Java, ++ ( ). "push". , , , ( , !). , "" , . FilterData, , ( , List ). , , .
. ( , ). ? ( , , ).