I implemented a class in Java that internally stores a list. I want the class to be immutable. However, I need to perform operations on internal data that does not make sense in the context of the class. Therefore, I have another class that defines a set of algorithms. Here is a simplified example:
Wrapper.java
import java.util.List;
import java.util.Iterator;
public class Wrapper implements Iterable<Double>
{
private final List<Double> list;
public Wrapper(List<Double> list)
{
this.list = list;
}
public Iterator<Double> iterator()
{
return getList().iterator();
}
public List<Double> data() { return getList(); }
}
Algorithm.java
import java.util.Iterator;
import java.util.Collection;
public class Algorithm
{
public static double sum(Collection<Double> collection)
{
double sum = 0.0;
Iterator<Double> iterator = collection.iterator();
do
{
sum += iterator.next();
}
while(iterator.hasNext());
return sum;
}
}
, , , , ? data() , - , clear() remove(). , . , , . -, , , , .
, , , . Java , const ++. !
! , . . .
Scott