package gcom.util; import java.util.AbstractList; import java.util.List; /** * Classe com métodos úteis para manipular uma Collection * * * @author Hugo Amorim * @data 14/10/2010 */ public class CollectionUtil { /* * Utilizar como exemplos: * * - atualizarParmsOS no RepositorioCobrancaHBM * * - pesquisarServicoTipoPorRA no RepositorioOrdemServicoHBM */ public static List> particao(List list, int size) { if (list == null) throw new NullPointerException("Lista precisa esta preenchida"); if (!(size > 0)) throw new IllegalArgumentException("Tamanho retorno precisa ser maior que 0"); return new Particao(list, size); } private static class Particao extends AbstractList> { final List list; final int size; Particao(List list, int size) { this.list = list; this.size = size; } @Override public List get(int index) { int listSize = size(); if (listSize < 0) throw new IllegalArgumentException("Tamanho negativo: " + listSize); if (index < 0) throw new IndexOutOfBoundsException("Index " + index + " precisa ser não negativo"); if (index >= listSize) throw new IndexOutOfBoundsException("Index " + index + " precisa ser menor que " + listSize); int start = index * size; int end = Math.min(start + size, list.size()); return list.subList(start, end); } @Override public int size() { return (list.size() + size - 1) / size; } @Override public boolean isEmpty() { return list.isEmpty(); } } }