package max.obj; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; public class LongList extends ArrayList { private static final long serialVersionUID = 1L; private Long sum = Long.valueOf(0); private void addSum(Long o) { this.sum += o; } private void removeSum(Long o) { this.sum -= o; } public void recomputeSum() { this.sum = Long.valueOf(0); Iterator it = this.iterator(); for(;it.hasNext();addSum(it.next())); } public Long getSum() { return this.sum; } /** * CONSTRUCTOR */ public LongList() { super(); } public LongList(List l) { this(); this.addAll(l); } /** * ADD */ @Override public boolean add(Long e) { addSum(e); return super.add(e); } @Override public boolean addAll(Collection c) { Iterator it = c.iterator(); for(;it.hasNext();add(it.next())); return true; } /** * REMOVE */ @Override public boolean remove(Object arg0) { if(arg0 instanceof Long) { removeSum((Long) arg0); return true; } return false; } @Override public boolean removeAll(Collection arg0) { Iterator it = arg0.iterator(); for(;it.hasNext();remove(it.next())); return true; } /** * SUBLIST */ @Override public LongList subList(int fromIndex, int toIndex) { return new LongList(super.subList(fromIndex, toIndex)); } }