1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
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
10 * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation
11 * Patrick Tasse - Refactoring
12 * Vincent Perot - Add subfield filtering
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.List
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
25 * The base class for the Filter tree nodes
28 * @author Yuriy Vashchuk
29 * @author Patrick Tasse
31 public abstract class TmfFilterTreeNode
implements ITmfFilterTreeNode
, Cloneable
{
33 private static final String
[] VALID_CHILDREN
= {
34 TmfFilterTraceTypeNode
.NODE_NAME
,
35 TmfFilterAndNode
.NODE_NAME
,
36 TmfFilterOrNode
.NODE_NAME
,
37 TmfFilterContainsNode
.NODE_NAME
,
38 TmfFilterEqualsNode
.NODE_NAME
,
39 TmfFilterMatchesNode
.NODE_NAME
,
40 TmfFilterCompareNode
.NODE_NAME
43 private ITmfFilterTreeNode parent
= null;
44 private ArrayList
<ITmfFilterTreeNode
> children
= new ArrayList
<>();
50 public TmfFilterTreeNode(final ITmfFilterTreeNode parent
) {
52 parent
.addChild(this);
57 public ITmfFilterTreeNode
getParent() {
62 public abstract String
getNodeName();
65 public boolean hasChildren() {
66 return (children
.size() > 0);
70 public int getChildrenCount() {
71 return children
.size();
75 public @NonNull ITmfFilterTreeNode
[] getChildren() {
76 return children
.toArray(new @NonNull ITmfFilterTreeNode
[0]);
80 public ITmfFilterTreeNode
getChild(final int index
) throws IndexOutOfBoundsException
{
81 return children
.get(index
);
85 public ITmfFilterTreeNode
remove() {
86 if (getParent() != null) {
87 getParent().removeChild(this);
93 public ITmfFilterTreeNode
removeChild(ITmfFilterTreeNode node
) {
94 children
.remove(node
);
100 public int addChild(final ITmfFilterTreeNode node
) {
101 node
.setParent(this);
102 if (children
.add(node
)) {
103 return (children
.size() - 1);
109 public ITmfFilterTreeNode
replaceChild(final int index
, final ITmfFilterTreeNode node
) throws IndexOutOfBoundsException
{
110 node
.setParent(this);
111 return children
.set(index
, node
);
115 public void setParent(final ITmfFilterTreeNode parent
) {
116 this.parent
= parent
;
120 public abstract boolean matches(ITmfEvent event
);
123 public List
<String
> getValidChildren() {
124 return Arrays
.asList(VALID_CHILDREN
);
128 public ITmfFilterTreeNode
clone() {
130 TmfFilterTreeNode clone
= (TmfFilterTreeNode
) super.clone();
132 clone
.children
= new ArrayList
<>(children
.size());
133 for (ITmfFilterTreeNode child
: getChildren()) {
134 clone
.addChild(child
.clone());
137 } catch (CloneNotSupportedException e
) {
143 public String
toString() {
144 return toString(false);