1/*******************************************************************************
2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.report.check;
13
14import java.util.ArrayList;
15import java.util.List;
16
17import org.jacoco.core.analysis.ICoverageNode.ElementType;
18import org.jacoco.core.runtime.WildcardMatcher;
19
20/**
21 * A rule applies for a certain element type and can define any number of limits
22 * for all elements of this type.
23 */
24public final class Rule {
25
26	private ElementType element;
27	private String includes;
28	private String excludes;
29	private List<Limit> limits;
30
31	private WildcardMatcher includesMatcher;
32	private WildcardMatcher excludesMatcher;
33
34	/**
35	 * Creates a new Rule without limits.
36	 */
37	public Rule() {
38		this.element = ElementType.BUNDLE;
39		this.limits = new ArrayList<Limit>();
40		this.setIncludes("*");
41		this.setExcludes("");
42	}
43
44	/**
45	 * @return element type this rule applies to
46	 */
47	public ElementType getElement() {
48		return element;
49	}
50
51	/**
52	 * @param elementType
53	 *            element type this rule applies to
54	 */
55	public void setElement(final ElementType elementType) {
56		this.element = elementType;
57	}
58
59	/**
60	 * @return includes pattern
61	 */
62	public String getIncludes() {
63		return includes;
64	}
65
66	/**
67	 * @param includes
68	 *            includes pattern
69	 */
70	public void setIncludes(final String includes) {
71		this.includes = includes;
72		this.includesMatcher = new WildcardMatcher(includes);
73	}
74
75	/**
76	 * @return excludes pattern
77	 */
78	public String getExcludes() {
79		return excludes;
80	}
81
82	/**
83	 *
84	 * @param excludes
85	 *            excludes patterns
86	 */
87	public void setExcludes(final String excludes) {
88		this.excludes = excludes;
89		this.excludesMatcher = new WildcardMatcher(excludes);
90	}
91
92	/**
93	 * @return list of {@link Limit}s configured for this rule
94	 */
95	public List<Limit> getLimits() {
96		return limits;
97	}
98
99	/**
100	 * @param limits
101	 *            list of {@link Limit}s configured for this rule
102	 */
103	public void setLimits(final List<Limit> limits) {
104		this.limits = limits;
105	}
106
107	/**
108	 * Creates and adds a new {@link Limit}.
109	 *
110	 * @return creates {@link Limit}
111	 */
112	public Limit createLimit() {
113		final Limit limit = new Limit();
114		this.limits.add(limit);
115		return limit;
116	}
117
118	boolean matches(final String name) {
119		return includesMatcher.matches(name) && !excludesMatcher.matches(name);
120	}
121
122}
123