c16223a96e40156af6fb2a360dc1bbb3b232247f
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / filter / model / TmfFilterTreeNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
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
12 * Vincent Perot - Add subfield filtering
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.core.filter.model;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22
23 /**
24 * The base class for the Filter tree nodes
25 *
26 * @version 1.0
27 * @author Yuriy Vashchuk
28 * @author Patrick Tasse
29 */
30 public abstract class TmfFilterTreeNode implements ITmfFilterTreeNode, Cloneable {
31
32 private static final String[] VALID_CHILDREN = {
33 TmfFilterTraceTypeNode.NODE_NAME,
34 TmfFilterAndNode.NODE_NAME,
35 TmfFilterOrNode.NODE_NAME,
36 TmfFilterContainsNode.NODE_NAME,
37 TmfFilterEqualsNode.NODE_NAME,
38 TmfFilterMatchesNode.NODE_NAME,
39 TmfFilterCompareNode.NODE_NAME
40 };
41
42 private ITmfFilterTreeNode parent = null;
43 private ArrayList<ITmfFilterTreeNode> children = new ArrayList<>();
44
45 /**
46 * @param parent
47 * the parent node
48 */
49 public TmfFilterTreeNode(final ITmfFilterTreeNode parent) {
50 if (parent != null) {
51 parent.addChild(this);
52 }
53 }
54
55 @Override
56 public ITmfFilterTreeNode getParent() {
57 return parent;
58 }
59
60 @Override
61 public abstract String getNodeName();
62
63 @Override
64 public boolean hasChildren() {
65 return (children.size() > 0);
66 }
67
68 @Override
69 public int getChildrenCount() {
70 return children.size();
71 }
72
73 @Override
74 public ITmfFilterTreeNode[] getChildren() {
75 return children.toArray(new ITmfFilterTreeNode[0]);
76 }
77
78 @Override
79 public ITmfFilterTreeNode getChild(final int index) throws IndexOutOfBoundsException {
80 return children.get(index);
81 }
82
83 @Override
84 public ITmfFilterTreeNode remove() {
85 if (getParent() != null) {
86 getParent().removeChild(this);
87 }
88 return this;
89 }
90
91 @Override
92 public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) {
93 children.remove(node);
94 node.setParent(null);
95 return node;
96 }
97
98 @Override
99 public int addChild(final ITmfFilterTreeNode node) {
100 node.setParent(this);
101 if (children.add(node)) {
102 return (children.size() - 1);
103 }
104 return -1;
105 }
106
107 @Override
108 public ITmfFilterTreeNode replaceChild(final int index, final ITmfFilterTreeNode node) throws IndexOutOfBoundsException {
109 node.setParent(this);
110 return children.set(index, node);
111 }
112
113 @Override
114 public void setParent(final ITmfFilterTreeNode parent) {
115 this.parent = parent;
116 }
117
118 @Override
119 public abstract boolean matches(ITmfEvent event);
120
121 @Override
122 public List<String> getValidChildren() {
123 return Arrays.asList(VALID_CHILDREN);
124 }
125
126 @Override
127 public ITmfFilterTreeNode clone() {
128 try {
129 TmfFilterTreeNode clone = (TmfFilterTreeNode) super.clone();
130 clone.parent = null;
131 clone.children = new ArrayList<>(children.size());
132 for (ITmfFilterTreeNode child : getChildren()) {
133 clone.addChild(child.clone());
134 }
135 return clone;
136 } catch (CloneNotSupportedException e) {
137 return null;
138 }
139 }
140
141 @Override
142 public String toString() {
143 return toString(false);
144 }
145 }
This page took 0.034941 seconds and 4 git commands to generate.