OSDN Git Service

テストセット整備
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd / model / ListUtil.java
1 /*
2  * List utilities
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd.model;
9
10 import java.lang.reflect.Constructor;
11 import java.lang.reflect.InvocationTargetException;
12 import java.util.List;
13
14 /**
15  * リスト構造の各種ユーティリティ。
16  */
17 public final class ListUtil {
18
19     /**
20      * 隠しコンストラクタ。
21      */
22     private ListUtil(){
23         assert false;
24         throw new AssertionError();
25     }
26
27     /**
28      * リストの出現順にシリアルナンバーを割り振る。
29      * シリアルナンバー先頭は0。
30      * @param list リスト。
31      */
32     public static void assignIndexedSerial(
33             List<? extends SerialNumbered> list){
34         int serial = 0;
35         for(SerialNumbered numbered : list){
36             numbered.setSerialNumber(serial);
37             serial++;
38         }
39
40         return;
41     }
42
43     /**
44      * リストの要素数を拡張する。
45      * 追加された要素にはnullが収められる。
46      * リストがすでに指定サイズ以上の要素数を持つ場合、何もしない。
47      * @param <E> 型
48      * @param list リスト
49      * @param newSize 新サイズ
50      */
51     public static <E> void extendList(List<E> list, int newSize){
52         int remain = newSize - list.size();
53         if(remain <= 0) return;
54
55         for(int ct = 1; ct <= remain; ct++){
56             list.add(null);
57         }
58
59         assert list.size() == newSize;
60
61         return;
62     }
63
64     /**
65      * リストのnull要素をデフォルトコンストラクタによるインスタンスで埋める。
66      * null要素がなければなにもしない。
67      * @param <E> 型
68      * @param list リスト
69      * @param cons コンストラクタ
70      * @return 埋めた数
71      */
72     public static <E> int fillDefCons(List<E> list,
73                                       Constructor<? extends E> cons){
74         int result = 0;
75
76         int size = list.size();
77         for(int pt = 0; pt < size; pt++){
78             E elem = list.get(pt);
79             if(elem != null) continue;
80
81             try{
82                 elem = cons.newInstance();
83             }catch(InstantiationException e){
84                 assert false;
85                 throw new AssertionError(e);
86             }catch(IllegalAccessException e){
87                 assert false;
88                 throw new AssertionError(e);
89             }catch(InvocationTargetException e){
90                 Throwable cause = e.getTargetException();
91                 if(cause instanceof RuntimeException){
92                     throw (RuntimeException) cause;
93                 }
94                 if(cause instanceof Error){
95                     throw (Error) cause;
96                 }
97                 assert false;
98                 throw new AssertionError(e);
99             }
100
101             list.set(pt, elem);
102             result++;
103         }
104
105         return result;
106     }
107
108     /**
109      * リストのnull要素をデフォルトコンストラクタによるインスタンスで埋める。
110      * @param <E> 型
111      * @param list リスト
112      * @param klass デフォルトコンストラクタの属するクラス
113      * @return 埋めた数
114      */
115     public static <E> int fillDefCons(List<E> list, Class<? extends E> klass){
116         Constructor<? extends E> cons;
117         try{
118             cons = klass.getConstructor();
119         }catch(NoSuchMethodException e){
120             assert false;
121             throw new AssertionError(e);
122         }
123
124         return fillDefCons(list, cons);
125     }
126
127     /**
128      * リスト要素数の拡張とデフォルトコンストラクタによる要素埋めを行う。
129      * 追加された要素および既存のnull要素には
130      * デフォルトコンストラクタによるインスタンスが収められる。
131      * @param <E> 型
132      * @param list リスト
133      * @param klass デフォルトコンストラクタの属するクラス
134      * @param newSize 新サイズ
135      * @return 埋めた数。
136      */
137     public static <E> int prepareDefConsList(List<E> list,
138                                              Class<? extends E> klass,
139                                              int newSize ){
140         extendList(list, newSize);
141         int result = fillDefCons(list, klass);
142         return result;
143     }
144
145 }