CollectionUtil.java
1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 <T> List<List<T>> particao(List<T> 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<T>(list, size);
}
private static class Particao<T> extends AbstractList<List<T>> {
final List<T> list;
final int size;
Particao(List<T> list, int size) {
this.list = list;
this.size = size;
}
@Override
public List<T> 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();
}
}
}