~ubuntu-branches/ubuntu/trusty/ivy/trusty

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/core/module/id/ModuleRules.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-06 22:04:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090306220456-5v37luqiuqda8ewp
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 *  contributor license agreements.  See the NOTICE file distributed with
 
4
 *  this work for additional information regarding copyright ownership.
 
5
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 *  (the "License"); you may not use this file except in compliance with
 
7
 *  the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *  Unless required by applicable law or agreed to in writing, software
 
12
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *  See the License for the specific language governing permissions and
 
15
 *  limitations under the License.
 
16
 *
 
17
 */
 
18
package org.apache.ivy.core.module.id;
 
19
 
 
20
import java.util.ArrayList;
 
21
import java.util.Collections;
 
22
import java.util.Iterator;
 
23
import java.util.LinkedHashMap;
 
24
import java.util.List;
 
25
import java.util.Map;
 
26
import java.util.Map.Entry;
 
27
 
 
28
import org.apache.ivy.plugins.matcher.MapMatcher;
 
29
import org.apache.ivy.util.Checks;
 
30
import org.apache.ivy.util.Message;
 
31
import org.apache.ivy.util.filter.Filter;
 
32
import org.apache.ivy.util.filter.NoFilter;
 
33
 
 
34
/**
 
35
 * A list of module specific rules.
 
36
 * <p>
 
37
 * This class defines a list of module specific rules. For each module only one rule apply,
 
38
 * sometimes none.
 
39
 * </p>
 
40
 * <p>
 
41
 * To know which rule to apply, they are configured using matchers. So you can define a rule
 
42
 * applying to all module from one particular organization, or to all modules with a revisions
 
43
 * matching a pattern, and so on.
 
44
 * </p>
 
45
 * <p>
 
46
 * Rules condition are evaluated in order, so the first matching rule is returned.
 
47
 * </p>
 
48
 * <p>
 
49
 * Rules themselves can be represented by any object, depending on the purpose of the rule (define
 
50
 * which resolver to use, which TTL in cache, ...)
 
51
 * </p>
 
52
 */
 
53
public class ModuleRules {
 
54
    private Map/*<MapMatcher,Object>*/ rules = new LinkedHashMap();
 
55
    
 
56
    /**
 
57
     * Constructs an empty ModuleRules.
 
58
     */
 
59
    public ModuleRules() {
 
60
    }
 
61
 
 
62
    private ModuleRules(Map/*<MapMatcher,Object>*/ rules) {
 
63
        this.rules = new LinkedHashMap(rules);
 
64
    }
 
65
    
 
66
    /**
 
67
     * Defines a new rule for the given condition.
 
68
     * 
 
69
     * @param condition
 
70
     *            the condition for which the rule should be applied. Must not be <code>null</code>.
 
71
     * @param rule
 
72
     *            the rule to apply. Must not be <code>null</code>.
 
73
     */
 
74
    public void defineRule(MapMatcher condition, Object rule) {
 
75
        Checks.checkNotNull(condition, "condition");
 
76
        Checks.checkNotNull(rule, "rule");
 
77
        
 
78
        rules.put(condition, rule);
 
79
    }
 
80
 
 
81
    /**
 
82
     * Returns the rule object matching the given {@link ModuleId}, or <code>null</code>
 
83
     * if no rule applies.
 
84
     * 
 
85
     * @param mid
 
86
     *            the {@link ModuleId} to search the rule for. 
 
87
     *            Must not be <code>null</code>.
 
88
     * @return the rule object matching the given {@link ModuleId}, or <code>null</code>
 
89
     *         if no rule applies.
 
90
     * @see #getRule(ModuleId, Filter)
 
91
     */
 
92
    public Object getRule(ModuleId mid) {
 
93
        return getRule(mid, NoFilter.INSTANCE);
 
94
    }
 
95
 
 
96
    /**
 
97
     * Returns the rules objects matching the given {@link ModuleId}, or an empty array
 
98
     * if no rule applies.
 
99
     * 
 
100
     * @param mid
 
101
     *            the {@link ModuleId} to search the rule for. 
 
102
     *            Must not be <code>null</code>.
 
103
     * @return an array of rule objects matching the given {@link ModuleId}.
 
104
     */
 
105
    public Object[] getRules(ModuleId mid) {
 
106
        return getRules(mid.getAttributes(), NoFilter.INSTANCE);
 
107
    }
 
108
 
 
109
    /**
 
110
     * Returns the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
 
111
     * if no rule applies.
 
112
     * 
 
113
     * @param mrid
 
114
     *            the {@link ModuleRevisionId} to search the rule for. 
 
115
     *            Must not be <code>null</code>.
 
116
     * @return the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
 
117
     *         if no rule applies.
 
118
     * @see #getRule(ModuleRevisionId, Filter)
 
119
     */
 
120
    public Object getRule(ModuleRevisionId mrid) {
 
121
        return getRule(mrid, NoFilter.INSTANCE);
 
122
    }
 
123
    
 
124
    /**
 
125
     * Returns the rule object matching the given {@link ModuleId} and accepted by the given
 
126
     * {@link Filter}, or <code>null</code> if no rule applies.
 
127
     * 
 
128
     * @param mrid
 
129
     *            the {@link ModuleRevisionId} to search the rule for. 
 
130
     *            Must not be <code>null</code>.
 
131
     * @param filter
 
132
     *            the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
 
133
     *            method will be called only with rule objects matching the given
 
134
     *            {@link ModuleId}, and the first rule object accepted by the filter will
 
135
     *            be returned. Must not be <code>null</code>.
 
136
     * @return the rule object matching the given {@link ModuleId}, or <code>null</code>
 
137
     *         if no rule applies.
 
138
     * @see #getRule(ModuleRevisionId, Filter)
 
139
     */
 
140
    public Object getRule(ModuleId mid, Filter filter) {
 
141
        Checks.checkNotNull(mid, "mid");
 
142
        return getRule(mid.getAttributes(), filter);
 
143
    }
 
144
    
 
145
    /**
 
146
     * Returns the rule object matching the given {@link ModuleRevisionId} and accepted by the given
 
147
     * {@link Filter}, or <code>null</code> if no rule applies.
 
148
     * 
 
149
     * @param mrid
 
150
     *            the {@link ModuleRevisionId} to search the rule for. 
 
151
     *            Must not be <code>null</code>.
 
152
     * @param filter
 
153
     *            the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
 
154
     *            method will be called only with rule objects matching the given
 
155
     *            {@link ModuleRevisionId}, and the first rule object accepted by the filter will
 
156
     *            be returned. Must not be <code>null</code>.
 
157
     * @return the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
 
158
     *         if no rule applies.
 
159
     * @see #getRule(ModuleRevisionId)
 
160
     */
 
161
    public Object getRule(ModuleRevisionId mrid, Filter filter) {
 
162
        Checks.checkNotNull(mrid, "mrid");
 
163
        Checks.checkNotNull(filter, "filter");        
 
164
        Map moduleAttributes = mrid.getAttributes();       
 
165
        return getRule(moduleAttributes, filter);
 
166
    }
 
167
 
 
168
    private Object getRule(Map moduleAttributes, Filter filter) {
 
169
        for (Iterator iter = rules.entrySet().iterator(); iter.hasNext();) {
 
170
            Map.Entry ruleEntry = (Entry) iter.next();
 
171
            MapMatcher midm = (MapMatcher) ruleEntry.getKey();            
 
172
            if (midm.matches(moduleAttributes)) {
 
173
                Object rule = ruleEntry.getValue();
 
174
                if (filter.accept(rule)) {
 
175
                    return rule;
 
176
                }
 
177
            }
 
178
        }
 
179
        return null;
 
180
    }
 
181
 
 
182
    
 
183
    
 
184
    
 
185
    /**
 
186
     * Returns the rules object matching the given {@link ModuleRevisionId} and accepted by the
 
187
     * given {@link Filter}, or an empty array if no rule applies.
 
188
     * 
 
189
     * @param mrid
 
190
     *            the {@link ModuleRevisionId} to search the rule for. 
 
191
     *            Must not be <code>null</code>.
 
192
     * @param filter
 
193
     *            the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
 
194
     *            method will be called only with rule objects matching the given
 
195
     *            {@link ModuleRevisionId}. Must not be <code>null</code>.
 
196
     * @return an array of rule objects matching the given {@link ModuleRevisionId}.
 
197
     */
 
198
    public Object[] getRules(ModuleRevisionId mrid, Filter filter) {
 
199
        Checks.checkNotNull(mrid, "mrid");
 
200
        Checks.checkNotNull(filter, "filter");      
 
201
        Map moduleAttributes = mrid.getAttributes();
 
202
        return getRules(moduleAttributes, filter);
 
203
    }
 
204
 
 
205
    
 
206
    private Object[] getRules(Map moduleAttributes, Filter filter) {
 
207
        List matchingRules = new ArrayList();
 
208
        for (Iterator iter = rules.entrySet().iterator(); iter.hasNext();) {
 
209
            Map.Entry ruleEntry = (Entry) iter.next();
 
210
            MapMatcher midm = (MapMatcher) ruleEntry.getKey();            
 
211
            if (midm.matches(moduleAttributes)) {
 
212
                Object rule = ruleEntry.getValue();
 
213
                if (filter.accept(rule)) {
 
214
                    matchingRules.add(rule);
 
215
                }
 
216
            }
 
217
        }
 
218
        return matchingRules.toArray();
 
219
    }
 
220
 
 
221
    /**
 
222
     * Dump the list of rules to {@link Message#debug(String)}
 
223
     * 
 
224
     * @param prefix
 
225
     *            the prefix to use for each line dumped
 
226
     */
 
227
    public void dump(String prefix) {
 
228
        if (rules.isEmpty()) {
 
229
            Message.debug(prefix + "NONE");
 
230
        } else {
 
231
            for (Iterator iter = rules.keySet().iterator(); iter.hasNext();) {
 
232
                MapMatcher midm = (MapMatcher) iter.next();
 
233
                Object rule = rules.get(midm);
 
234
                Message.debug(prefix + midm + " -> " + rule);
 
235
            }
 
236
        }
 
237
    }
 
238
    
 
239
    /**
 
240
     * Returns an unmodifiable view of all the rules defined on this ModuleRules.
 
241
     * <p>
 
242
     * The rules are returned in a Map where they keys are the MapMatchers matching the rules
 
243
     * object, and the values are the rules object themselves.
 
244
     * </p>
 
245
     * 
 
246
     * @return an unmodifiable view of all the rules defined on this ModuleRules.
 
247
     */
 
248
    public Map/*<MapMatcher,Object>*/ getAllRules() {
 
249
        return Collections.unmodifiableMap(rules);
 
250
    }
 
251
 
 
252
    public Object clone() {
 
253
        return new ModuleRules(rules);
 
254
    }
 
255
}