Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
2b0005f0 | 2 | * Copyright (c) 2010, 2015 Ericsson |
be222f56 PT |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation | |
11 | * Patrick Tasse - Refactoring | |
a1bc6c45 | 12 | * Vincent Perot - Add subfield filtering |
be222f56 PT |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.core.filter.model; |
be222f56 PT |
16 | |
17 | import java.util.ArrayList; | |
18 | import java.util.Arrays; | |
19 | import java.util.List; | |
20 | ||
aa353506 | 21 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 | 22 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
be222f56 PT |
23 | |
24 | /** | |
25 | * The base class for the Filter tree nodes | |
26 | * | |
27 | * @version 1.0 | |
28 | * @author Yuriy Vashchuk | |
29 | * @author Patrick Tasse | |
30 | */ | |
31 | public abstract class TmfFilterTreeNode implements ITmfFilterTreeNode, Cloneable { | |
32 | ||
11252342 | 33 | private static final String[] VALID_CHILDREN = { |
2b0005f0 | 34 | TmfFilterTraceTypeNode.NODE_NAME, |
11252342 AM |
35 | TmfFilterAndNode.NODE_NAME, |
36 | TmfFilterOrNode.NODE_NAME, | |
37 | TmfFilterContainsNode.NODE_NAME, | |
38 | TmfFilterEqualsNode.NODE_NAME, | |
ec34bf48 | 39 | TmfFilterMatchesNode.NODE_NAME, |
11252342 AM |
40 | TmfFilterCompareNode.NODE_NAME |
41 | }; | |
be222f56 | 42 | |
11252342 | 43 | private ITmfFilterTreeNode parent = null; |
a4524c1b | 44 | private ArrayList<ITmfFilterTreeNode> children = new ArrayList<>(); |
be222f56 PT |
45 | |
46 | /** | |
11252342 AM |
47 | * @param parent |
48 | * the parent node | |
be222f56 PT |
49 | */ |
50 | public TmfFilterTreeNode(final ITmfFilterTreeNode parent) { | |
11252342 AM |
51 | if (parent != null) { |
52 | parent.addChild(this); | |
53 | } | |
54 | } | |
55 | ||
56 | @Override | |
57 | public ITmfFilterTreeNode getParent() { | |
58 | return parent; | |
59 | } | |
60 | ||
61 | @Override | |
62 | public abstract String getNodeName(); | |
63 | ||
64 | @Override | |
65 | public boolean hasChildren() { | |
66 | return (children.size() > 0); | |
be222f56 PT |
67 | } |
68 | ||
11252342 AM |
69 | @Override |
70 | public int getChildrenCount() { | |
71 | return children.size(); | |
72 | } | |
73 | ||
74 | @Override | |
aa353506 AM |
75 | public @NonNull ITmfFilterTreeNode[] getChildren() { |
76 | return children.toArray(new @NonNull ITmfFilterTreeNode[0]); | |
11252342 AM |
77 | } |
78 | ||
79 | @Override | |
80 | public ITmfFilterTreeNode getChild(final int index) throws IndexOutOfBoundsException { | |
81 | return children.get(index); | |
82 | } | |
83 | ||
84 | @Override | |
85 | public ITmfFilterTreeNode remove() { | |
86 | if (getParent() != null) { | |
87 | getParent().removeChild(this); | |
88 | } | |
89 | return this; | |
90 | } | |
91 | ||
92 | @Override | |
93 | public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) { | |
94 | children.remove(node); | |
95 | node.setParent(null); | |
96 | return node; | |
97 | } | |
98 | ||
99 | @Override | |
100 | public int addChild(final ITmfFilterTreeNode node) { | |
101 | node.setParent(this); | |
102 | if (children.add(node)) { | |
103 | return (children.size() - 1); | |
104 | } | |
105 | return -1; | |
106 | } | |
107 | ||
108 | @Override | |
109 | public ITmfFilterTreeNode replaceChild(final int index, final ITmfFilterTreeNode node) throws IndexOutOfBoundsException { | |
110 | node.setParent(this); | |
111 | return children.set(index, node); | |
112 | } | |
113 | ||
114 | @Override | |
115 | public void setParent(final ITmfFilterTreeNode parent) { | |
116 | this.parent = parent; | |
117 | } | |
118 | ||
119 | @Override | |
120 | public abstract boolean matches(ITmfEvent event); | |
be222f56 | 121 | |
11252342 AM |
122 | @Override |
123 | public List<String> getValidChildren() { | |
124 | return Arrays.asList(VALID_CHILDREN); | |
125 | } | |
126 | ||
127 | @Override | |
128 | public ITmfFilterTreeNode clone() { | |
129 | try { | |
130 | TmfFilterTreeNode clone = (TmfFilterTreeNode) super.clone(); | |
131 | clone.parent = null; | |
a4524c1b | 132 | clone.children = new ArrayList<>(children.size()); |
11252342 AM |
133 | for (ITmfFilterTreeNode child : getChildren()) { |
134 | clone.addChild(child.clone()); | |
135 | } | |
136 | return clone; | |
137 | } catch (CloneNotSupportedException e) { | |
138 | return null; | |
139 | } | |
140 | } | |
e883975e PT |
141 | |
142 | @Override | |
143 | public String toString() { | |
144 | return toString(false); | |
145 | } | |
be222f56 | 146 | } |