1 package org.tinyjee.maven.dim.utils;
2
3 import org.apache.maven.doxia.logging.Log;
4 import org.tinyjee.maven.dim.spi.Globals;
5
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collection;
9 import java.util.List;
10 import java.util.regex.Pattern;
11
12
13
14
15
16
17 public class SelectableArrayList<E> extends ArrayList<E> {
18
19 private static final long serialVersionUID = 7547555631109691023L;
20
21
22
23
24
25
26 public interface Selector<E> {
27 boolean accept(E element);
28 }
29
30 public SelectableArrayList() {
31 }
32
33 public SelectableArrayList(int initialCapacity) {
34 super(initialCapacity);
35 }
36
37 public SelectableArrayList(Collection<? extends E> collection) {
38 super(collection);
39 }
40
41 @Override
42 @SuppressWarnings("unchecked")
43 public SelectableArrayList<E> clone() {
44 return (SelectableArrayList) super.clone();
45 }
46
47
48
49
50
51
52
53 public SelectableArrayList<E> select(Selector<E> selector) {
54
55 SelectableArrayList<E> clone = clone();
56
57
58 clone.clear();
59 for (E e : this) {
60 if (selector.accept(e)) clone.add(e);
61 }
62
63 if (clone.isEmpty() && !isEmpty()) {
64 Log log = Globals.getLog();
65 log.debug("No elements were matched when selecting from " + size() + " elements by selector: " + selector);
66 }
67
68 return clone;
69 }
70
71
72
73
74
75
76
77 public SelectableArrayList<E> selectMatching(String regularExpression) {
78 return select(createRegularExpressionSelector(regularExpression));
79 }
80
81
82
83
84
85
86
87 @SuppressWarnings("unchecked")
88 public SelectableArrayList<E> selectMatching(String... regularExpressions) {
89 List<Selector<E>> selectors = new ArrayList<Selector<E>>(regularExpressions.length);
90 for (String regularExpression : regularExpressions)
91 selectors.add(createRegularExpressionSelector(regularExpression));
92 return select(createOrSelector(selectors.toArray(new Selector[selectors.size()])));
93 }
94
95
96
97
98
99
100
101 public SelectableArrayList<E> selectNonMatching(String regularExpression) {
102 return select(createInvertSelector(createRegularExpressionSelector(regularExpression)));
103 }
104
105
106
107
108
109
110
111 public Selector<E> createOrSelector(final Selector<E>... selectors) {
112 return new Selector<E>() {
113 public boolean accept(E element) {
114 for (Selector<E> selector : selectors)
115 if (selector.accept(element)) return true;
116 return false;
117 }
118
119 @Override
120 public String toString() {
121 return "OrSelector{" +
122 "selectors=" + Arrays.toString(selectors) +
123 '}';
124 }
125 };
126 }
127
128
129
130
131
132
133
134 public Selector<E> createInvertSelector(final Selector<E> other) {
135 return new Selector<E>() {
136 public boolean accept(E element) {
137 return !other.accept(element);
138 }
139
140 @Override
141 public String toString() {
142 return "InvertSelector{" +
143 "selector=" + other +
144 '}';
145 }
146 };
147 }
148
149
150
151
152
153
154
155 public Selector<E> createRegularExpressionSelector(final String regularExpression) {
156 return new Selector<E>() {
157 final Pattern pattern = Pattern.compile(regularExpression);
158
159 public boolean accept(E element) {
160 return pattern.matcher(String.valueOf(element)).matches();
161 }
162
163 @Override
164 public String toString() {
165 return "RegularExpressionSelector{" +
166 "pattern=" + pattern +
167 '}';
168 }
169 };
170 }
171 }