Fix some null warnings
[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.jdt.annotation.NonNull;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
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
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
41 };
42
43 private ITmfFilterTreeNode parent = null;
44 private ArrayList<ITmfFilterTreeNode> children = new ArrayList<>();
45
46 /**
47 * @param parent
48 * the parent node
49 */
50 public TmfFilterTreeNode(final ITmfFilterTreeNode parent) {
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);
67 }
68
69 @Override
70 public int getChildrenCount() {
71 return children.size();
72 }
73
74 @Override
75 public @NonNull ITmfFilterTreeNode[] getChildren() {
76 return children.toArray(new @NonNull ITmfFilterTreeNode[0]);
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);
121
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;
132 clone.children = new ArrayList<>(children.size());
133 for (ITmfFilterTreeNode child : getChildren()) {
134 clone.addChild(child.clone());
135 }
136 return clone;
137 } catch (CloneNotSupportedException e) {
138 return null;
139 }
140 }
141
142 @Override
143 public String toString() {
144 return toString(false);
145 }
146 }
This page took 0.040445 seconds and 5 git commands to generate.