summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblemCategory.java
blob: 015621143ecf90952490d5ce29055e48ef4639a2 (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Alena Laskavaia 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Alena Laskavaia  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.core.model;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemCategory;
import org.eclipse.cdt.codan.core.model.IProblemElement;

public class CodanProblemCategory implements IProblemCategory, Cloneable {
	private String id;
	private String name;
	private ArrayList<IProblemElement> list = new ArrayList<IProblemElement>();

	public CodanProblemCategory(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String getId() {
		return id;
	}

	@Override
	public String toString() {
		return name;
	}

	public IProblemElement[] getChildren() {
		return list.toArray(new IProblemElement[list.size()]);
	}

	public void addChild(IProblemElement p) {
		list.add(p);
	}

	public static IProblem findProblem(IProblemCategory c, String id) {
		Object[] children = c.getChildren();
		for (Object object : children) {
			if (object instanceof IProblemCategory) {
				IProblemCategory cat = (IProblemCategory) object;
				IProblem found = findProblem(cat, id);
				if (found != null) return found;
			} else if (object instanceof IProblem) {
				IProblem p = (IProblem) object;
				if (p.getId().equals(id)) return p;
			}
		}
		return null;
	}

	public static IProblemCategory findCategory(IProblemCategory cat, String id) {
		if (cat.getId().equals(id)) return cat;
		Object[] children = cat.getChildren();
		for (Object object : children) {
			if (object instanceof IProblemCategory) {
				IProblemCategory cat2 = (IProblemCategory) object;
				IProblemCategory found = findCategory(cat2, id);
				if (found != null) return found;
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#clone()
	 */
	@Override
	public Object clone() {
		try {
			CodanProblemCategory clone = (CodanProblemCategory) super.clone();
			clone.list = new ArrayList<IProblemElement>();
			for (Iterator<IProblemElement> iterator = this.list.iterator(); iterator.hasNext();) {
				IProblemElement child = iterator.next();
				clone.list.add((IProblemElement) child.clone());
			}
			return clone;
		} catch (CloneNotSupportedException e) {
			return this;
		}
	}
}