|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CollectionUtils.java | 85% | 91.2% | 100% | 90.5% |
|
1 |
/*
|
|
2 |
* Joey and its relative products are published under the terms
|
|
3 |
* of the Apache Software License.
|
|
4 |
*/
|
|
5 |
package org.asyrinx.brownie.core.collection;
|
|
6 |
|
|
7 |
import java.util.ArrayList;
|
|
8 |
import java.util.Iterator;
|
|
9 |
import java.util.List;
|
|
10 |
import java.util.Map;
|
|
11 |
|
|
12 |
/**
|
|
13 |
* コレクションに関するユーティリティクラスです。
|
|
14 |
*
|
|
15 |
* @author Akima
|
|
16 |
*/
|
|
17 |
public final class CollectionUtils extends |
|
18 |
org.apache.commons.collections.CollectionUtils { |
|
19 |
|
|
20 |
/**
|
|
21 |
* MapされているValue群をListとして取得します。
|
|
22 |
*/
|
|
23 | 1 |
public static List toList(Map source) { |
24 | 1 |
final List result = new ArrayList();
|
25 | 1 |
toList(source, result); |
26 | 1 |
return result;
|
27 |
} |
|
28 |
|
|
29 |
/**
|
|
30 |
* MapされているValue群をListとして取得します。
|
|
31 |
*/
|
|
32 | 2 |
public static void toList(Map source, List dest) { |
33 | 2 |
final Iterator iter = source.keySet().iterator(); |
34 | 2 |
while (iter.hasNext())
|
35 | 6 |
dest.add(source.get(iter.next())); |
36 |
} |
|
37 |
|
|
38 |
/**
|
|
39 |
* 引数sourceの要素(群)をListとして取得します。
|
|
40 |
*/
|
|
41 | 10 |
public static List toList(Object[] source) { |
42 | 10 |
final List result = new ArrayList();
|
43 | 10 |
toList(source, result); |
44 | 10 |
return result;
|
45 |
} |
|
46 |
|
|
47 |
/**
|
|
48 |
* 引数sourceの要素(群)をListとして取得します。
|
|
49 |
*/
|
|
50 | 11 |
public static void toList(Object[] source, List dest) { |
51 | 11 |
for (int i = 0; i < source.length; i++) { |
52 | 52 |
dest.add(source[i]); |
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
/**
|
|
57 |
* 引数hashCodeと合致するハッシュコードを保持するオブジェクトを 引数sourceから検索します。
|
|
58 |
*/
|
|
59 | 3 |
public static Object find(List source, int hashCode) { |
60 | 6 |
for (int i = 0; i < source.size(); i++) { |
61 | 6 |
if (source.get(i).hashCode() == hashCode) {
|
62 | 3 |
return source.get(i);
|
63 |
} |
|
64 |
} |
|
65 | 0 |
return null; |
66 |
} |
|
67 |
|
|
68 |
/**
|
|
69 |
* 引数source中に、重複する要素があるかどうかを調べます。
|
|
70 |
*/
|
|
71 | 3 |
public static boolean hasDuplicates(List source) { |
72 | 3 |
final List checked = new ArrayList();
|
73 | 3 |
for (int i = source.size() - 1; i > -1; i--) { |
74 | 10 |
if (checked.contains(source.get(i))) {
|
75 | 2 |
return true; |
76 |
} else {
|
|
77 | 8 |
checked.add(source.get(i)); |
78 |
} |
|
79 |
} |
|
80 | 1 |
return false; |
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* 指定されたListから指定された個数の要素を先頭から要素を削除します。
|
|
85 |
*
|
|
86 |
* @param target
|
|
87 |
* 対象となるList
|
|
88 |
* @param count
|
|
89 |
* 削除する個数
|
|
90 |
*/
|
|
91 | 1 |
public static void removeFromFirst(List target, int count) { |
92 | 1 |
removeFromIndex(target, 0, Math.abs(count)); |
93 |
} |
|
94 |
|
|
95 |
/**
|
|
96 |
* 指定されたListから指定された個数の要素を末尾から要素を削除します。
|
|
97 |
*
|
|
98 |
* @param target
|
|
99 |
* 対象となるList
|
|
100 |
* @param count
|
|
101 |
* 削除する個数
|
|
102 |
*/
|
|
103 | 1 |
public static void removeFromLast(List target, int count) { |
104 | 1 |
if (target == null) |
105 | 0 |
return;
|
106 | 1 |
removeFromIndex(target, target.size() - 1, Math.abs(count) * -1); |
107 |
} |
|
108 |
|
|
109 |
/**
|
|
110 |
* 指定されたListから指定された個数の要素を指定されたインデックスから順に要素を削除します。
|
|
111 |
*
|
|
112 |
* @param target
|
|
113 |
* 対象となるList
|
|
114 |
* @param index
|
|
115 |
* 削除を始めるインデックス
|
|
116 |
* @param count
|
|
117 |
* 削除する個数。マイナス値を指定した場合は逆方向に削除していきます。
|
|
118 |
*/
|
|
119 | 4 |
public static void removeFromIndex(List target, int index, int count) { |
120 | 4 |
if (target == null) |
121 | 0 |
return;
|
122 | 4 |
final boolean backward = count < 0;
|
123 | 4 |
count = Math.abs(count); |
124 | 4 |
while (!target.isEmpty() && count > 0 && index > -1
|
125 |
&& index < target.size()) { |
|
126 | 13 |
target.remove(index); |
127 | 13 |
count--; |
128 | 13 |
if (backward)
|
129 | 6 |
index--; |
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
} |
|