summaryrefslogtreecommitdiff
path: root/core/java12/com/vladium/emma/filter/IInclExclFilter.java
blob: e366c235bed9fe9e431712c2848395848a791b78 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: IInclExclFilter.java,v 1.1.1.1 2004/05/09 16:57:33 vlad_r Exp $
 */
package com.vladium.emma.filter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import com.vladium.util.WCMatcher;

// ----------------------------------------------------------------------------
/**
 * @author Vlad Roubtsov, 2003
 */
public
interface IInclExclFilter
{
    // public: ................................................................
    
    // TODO: move this into util pkg
    
    char INCLUSION_PREFIX = '+';
    String INCLUSION_PREFIX_STRING = "+";
    char EXCLUSION_PREFIX = '-';
    String EXCLUSION_PREFIX_STRING = "-";

    boolean included (final String s);

    abstract class Factory
    {
        public static IInclExclFilter create (final String specs1, final String separators, final String [] specs2)
        {
            if ((specs1 == null) || (specs1.trim ().length () == 0))
                return create (specs2);
            else
            {
                final List /* String */ _specs = new ArrayList ();
                
                if (specs2 != null)
                {
                    for (int s = 0; s < specs2.length; ++ s)
                    {
                        _specs.add (specs2 [s]);
                    }
                }
                
                for (StringTokenizer tokenizer = new StringTokenizer (specs1, separators);
                     tokenizer.hasMoreTokens (); )
                {
                    _specs.add (tokenizer.nextToken ());
                }
                
                final String [] specs = new String [_specs.size ()];
                _specs.toArray (specs);
                
                return create (specs);
            }
        }
        
        public static IInclExclFilter create (final String [] specs)
        {
            if ((specs == null) || (specs.length == 0))
                return new WCInclExclFilter ((String []) null, (String []) null);
            
            final List inclusions = new ArrayList ();
            final List exclusions = new ArrayList ();
            
            for (int i = 0, iLimit = specs.length; i < iLimit; ++ i)
            {
                final String spec = specs [i];
                
                if (spec.length () > 0)
                {
                    if (spec.charAt (0) == EXCLUSION_PREFIX)
                        exclusions.add (spec.substring (1));
                    else
                    {
                        // [inclusion prefix is optional]
                        
                        if (spec.charAt (0) == INCLUSION_PREFIX)
                            inclusions.add (spec.substring (1));
                        else
                            inclusions.add (spec);
                    }
                } 
            }
            
            return new WCInclExclFilter (inclusions, exclusions);
        }
        
        public static IInclExclFilter create (final String [] inclusions,
                                              final String [] exclusions)
        {
            return new WCInclExclFilter (inclusions, exclusions);
        }
        
        public static IInclExclFilter create (final List /* String */ inclusions,
                                              final List /* String */ exclusions)
        {
            return new WCInclExclFilter (inclusions, exclusions);
        }

        private static final class WCInclExclFilter implements IInclExclFilter
        {
            public boolean included (final String s)
            {
                if (s == null) return false;
                
                final char [] chars = s.toCharArray ();
                
                // included set is (inclusions - exclusions), where null inclusions
                // mean 'everything' and null exclusions mean 'nothing':
                
                final WCMatcher [] inclusions = m_inclusions;
                final WCMatcher [] exclusions = m_exclusions;
                
                if (inclusions != null)
                {
                    boolean included = false;
                    
                    for (int i = 0, iLimit = inclusions.length; i < iLimit; ++ i)
                    {
                        if (inclusions [i].matches (chars))
                        {
                            included = true;
                            break;
                        }
                    }
                    
                    if (! included) return false;
                }
    
                if (exclusions != null)
                {
                    for (int x = 0, xLimit = exclusions.length; x < xLimit; ++ x)
                    {
                        if (exclusions [x].matches (chars)) return false;
                    }
                }
                
                return true;
            }
            
            
            WCInclExclFilter (final String [] inclusions,
                              final String [] exclusions)
            {
                if ((inclusions == null) || (inclusions.length == 0))
                    m_inclusions = null;
                else
                {
                    m_inclusions = new WCMatcher [inclusions.length];
                    
                    for (int i = 0; i < inclusions.length; ++ i)
                    {
                        m_inclusions [i] = WCMatcher.compile (inclusions [i]);
                    }
                }
                
                if ((exclusions == null) || (exclusions.length == 0))
                    m_exclusions = null;
                else
                {
                    m_exclusions = new WCMatcher [exclusions.length];
                    
                    for (int i = 0; i < exclusions.length; ++ i)
                    {
                        m_exclusions [i] = WCMatcher.compile (exclusions [i]);
                    }
                }
            }
            
            WCInclExclFilter (final List /* String */ inclusions,
                              final List /* String */ exclusions)
            {
                if ((inclusions == null) || inclusions.isEmpty ())
                    m_inclusions = null;
                else
                {
                    m_inclusions = new WCMatcher [inclusions.size ()];
                    
                    int ii = 0;
                    for (Iterator i = inclusions.iterator (); i.hasNext (); ++ ii)
                    {
                        final String pattern = (String) i.next ();
                        
                        m_inclusions [ii] = WCMatcher.compile (pattern);
                    }
                }
                
                if ((exclusions == null) || exclusions.isEmpty ())
                    m_exclusions = null;
                else
                {
                    m_exclusions = new WCMatcher [exclusions.size ()];
                    
                    int ii = 0;
                    for (Iterator i = exclusions.iterator (); i.hasNext (); ++ ii)
                    {
                        final String pattern = (String) i.next ();
                        
                        m_exclusions [ii] = WCMatcher.compile (pattern);
                    }
                }
            }
            
            
            private final WCMatcher [] m_inclusions, m_exclusions;
            
        } // end of nested class
        
    } // end of nested class

} // end of interface
// ----------------------------------------------------------------------------