Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
cbbcc354 | 2 | * Copyright (c) 2009, 2012 Ericsson |
8c8bf09f ASL |
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: | |
1f506a43 | 10 | * Francois Chouinard - Initial API and implementation |
cbbcc354 | 11 | * Francois Chouinard - Updated as per TMF Event Model 1.0 |
8c8bf09f ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.event; |
8c8bf09f | 15 | |
cbbcc354 | 16 | import java.util.Arrays; |
28b94d61 | 17 | import java.util.HashMap; |
cbbcc354 | 18 | import java.util.Map; |
28b94d61 | 19 | |
8c8bf09f ASL |
20 | /** |
21 | * <b><u>TmfEventType</u></b> | |
22 | * <p> | |
cbbcc354 | 23 | * A basic implementation of ITmfEventType. |
8c8bf09f | 24 | */ |
cbbcc354 | 25 | public class TmfEventType implements ITmfEventType { |
8c8bf09f | 26 | |
cbd4ad82 | 27 | // ------------------------------------------------------------------------ |
28b94d61 | 28 | // Constants |
cbd4ad82 | 29 | // ------------------------------------------------------------------------ |
28b94d61 | 30 | |
cbbcc354 | 31 | public static final String DEFAULT_CONTEXT_ID = "Context"; //$NON-NLS-1$ |
32 | public static final String DEFAULT_TYPE_ID = "Type"; //$NON-NLS-1$ | |
28b94d61 | 33 | |
cbd4ad82 | 34 | // ------------------------------------------------------------------------ |
8c8bf09f | 35 | // Attributes |
cbd4ad82 | 36 | // ------------------------------------------------------------------------ |
8c8bf09f | 37 | |
cbbcc354 | 38 | private String fContext; |
39 | private String fTypeId; | |
40 | private int fNbFields; | |
41 | private Map<String, Integer> fFieldMap; | |
1a971e96 | 42 | private String[] fFieldLabels; |
8c8bf09f | 43 | |
cbd4ad82 | 44 | // ------------------------------------------------------------------------ |
8c8bf09f | 45 | // Constructors |
cbd4ad82 | 46 | // ------------------------------------------------------------------------ |
8c8bf09f | 47 | |
28b94d61 | 48 | /** |
cbd4ad82 | 49 | * Default constructor |
28b94d61 FC |
50 | */ |
51 | public TmfEventType() { | |
cbbcc354 | 52 | this(DEFAULT_CONTEXT_ID, DEFAULT_TYPE_ID, null); |
28b94d61 FC |
53 | } |
54 | ||
8c8bf09f | 55 | /** |
cbbcc354 | 56 | * Full constructor |
57 | * | |
8c8bf09f ASL |
58 | * @param type |
59 | * @param format | |
60 | */ | |
cbbcc354 | 61 | public TmfEventType(String context, String typeId, String[] labels) { |
62 | if (context == null || typeId == null) | |
cbd4ad82 | 63 | throw new IllegalArgumentException(); |
cbbcc354 | 64 | fContext = context; |
65 | fTypeId = typeId; | |
66 | fFieldLabels = (labels != null) ? labels : new String[] { }; | |
67 | fNbFields = (fFieldLabels != null) ? fFieldLabels.length : 0; | |
68 | fFieldMap = new HashMap<String, Integer>(); | |
28b94d61 | 69 | for (int i = 0; i < fNbFields; i++) { |
cbbcc354 | 70 | String id = fFieldLabels[i]; |
71 | fFieldMap.put(id, i); | |
28b94d61 FC |
72 | } |
73 | } | |
74 | ||
75 | /** | |
cbd4ad82 | 76 | * Copy constructor |
cbbcc354 | 77 | * |
78 | * @param type the other type | |
28b94d61 | 79 | */ |
cbbcc354 | 80 | public TmfEventType(TmfEventType type) { |
81 | if (type == null) | |
cbd4ad82 | 82 | throw new IllegalArgumentException(); |
cbbcc354 | 83 | fContext = type.fContext; |
84 | fTypeId = type.fTypeId; | |
85 | fFieldLabels = type.fFieldLabels; | |
86 | fNbFields = type.fNbFields; | |
87 | fFieldMap = type.fFieldMap; | |
8c8bf09f ASL |
88 | } |
89 | ||
cbd4ad82 | 90 | // ------------------------------------------------------------------------ |
cbbcc354 | 91 | // ITmfEventType |
cbd4ad82 | 92 | // ------------------------------------------------------------------------ |
8c8bf09f | 93 | |
cbbcc354 | 94 | public String getContext() { |
95 | return fContext; | |
8c8bf09f ASL |
96 | } |
97 | ||
cbbcc354 | 98 | public String getId() { |
99 | return fTypeId; | |
100 | } | |
101 | ||
28b94d61 FC |
102 | public int getNbFields() { |
103 | return fNbFields; | |
104 | } | |
105 | ||
cbbcc354 | 106 | public String[] getFieldLabels() { |
107 | return fFieldLabels; | |
108 | } | |
28b94d61 | 109 | |
cbbcc354 | 110 | public String getFieldLabel(int i) throws TmfNoSuchFieldException { |
111 | if (i >= 0 && i < fNbFields) | |
112 | return fFieldLabels[i]; | |
113 | throw new TmfNoSuchFieldException("Invalid index (" + i + ")"); //$NON-NLS-1$//$NON-NLS-2$ | |
114 | } | |
28b94d61 | 115 | |
cbbcc354 | 116 | public int getFieldIndex(String fieldId) throws TmfNoSuchFieldException { |
117 | Integer index = fFieldMap.get(fieldId); | |
118 | if (index == null) | |
119 | throw (new TmfNoSuchFieldException("Invalid field (" + fieldId + ")")); //$NON-NLS-1$//$NON-NLS-2$ | |
120 | return index; | |
121 | } | |
122 | ||
123 | // ------------------------------------------------------------------------ | |
124 | // Cloneable | |
125 | // ------------------------------------------------------------------------ | |
126 | ||
127 | @Override | |
128 | public TmfEventType clone() { | |
129 | TmfEventType clone = null; | |
130 | try { | |
131 | clone = (TmfEventType) super.clone(); | |
132 | clone.fContext = fContext; | |
133 | clone.fTypeId = fTypeId; | |
134 | // Clone the fields | |
135 | clone.fNbFields = fNbFields; | |
136 | clone.fFieldLabels = new String[fNbFields]; | |
137 | clone.fFieldMap = new HashMap<String, Integer>(); | |
138 | for (int i = 0; i < fNbFields; i++) { | |
139 | clone.fFieldLabels[i] = fFieldLabels[i]; | |
140 | clone.fFieldMap.put(fFieldLabels[i], new Integer(i)); | |
141 | } | |
142 | } | |
143 | catch (CloneNotSupportedException e) { | |
144 | } | |
145 | return clone; | |
146 | } | |
8c8bf09f | 147 | |
cbd4ad82 FC |
148 | // ------------------------------------------------------------------------ |
149 | // Object | |
150 | // ------------------------------------------------------------------------ | |
1f506a43 | 151 | |
cbbcc354 | 152 | @Override |
cbd4ad82 | 153 | public int hashCode() { |
cbbcc354 | 154 | final int prime = 31; |
155 | int result = 1; | |
156 | result = prime * result + ((fContext == null) ? 0 : fContext.hashCode()); | |
157 | result = prime * result + Arrays.hashCode(fFieldLabels); | |
158 | result = prime * result + ((fFieldMap == null) ? 0 : fFieldMap.hashCode()); | |
159 | result = prime * result + fNbFields; | |
160 | result = prime * result + ((fTypeId == null) ? 0 : fTypeId.hashCode()); | |
161 | return result; | |
cbd4ad82 FC |
162 | } |
163 | ||
cbbcc354 | 164 | @Override |
165 | public boolean equals(Object obj) { | |
166 | if (this == obj) | |
167 | return true; | |
168 | if (obj == null) | |
169 | return false; | |
170 | if (getClass() != obj.getClass()) | |
171 | return false; | |
172 | TmfEventType other = (TmfEventType) obj; | |
173 | if (fContext == null) { | |
174 | if (other.fContext != null) | |
175 | return false; | |
176 | } else if (!fContext.equals(other.fContext)) | |
177 | return false; | |
178 | if (!Arrays.equals(fFieldLabels, other.fFieldLabels)) | |
179 | return false; | |
180 | if (fFieldMap == null) { | |
181 | if (other.fFieldMap != null) | |
182 | return false; | |
183 | } else if (!fFieldMap.equals(other.fFieldMap)) | |
184 | return false; | |
185 | if (fNbFields != other.fNbFields) | |
186 | return false; | |
187 | if (fTypeId == null) { | |
188 | if (other.fTypeId != null) | |
189 | return false; | |
190 | } else if (!fTypeId.equals(other.fTypeId)) | |
191 | return false; | |
192 | return true; | |
28b94d61 FC |
193 | } |
194 | ||
1f506a43 | 195 | @Override |
3b38ea61 | 196 | @SuppressWarnings("nls") |
1f506a43 | 197 | public String toString() { |
cbbcc354 | 198 | return "TmfEventType [fContext=" + fContext + ", fTypeId=" + fTypeId + ", fNbFields=" |
199 | + fNbFields + ", fFieldLabels=" + Arrays.toString(fFieldLabels) + "]"; | |
1f506a43 FC |
200 | } |
201 | ||
cbbcc354 | 202 | // @Override |
203 | // public int hashCode() { | |
204 | // return fTypeId.hashCode(); | |
205 | // } | |
206 | // | |
207 | // @Override | |
208 | // public boolean equals(Object other) { | |
209 | // if (!(other instanceof TmfEventType)) | |
210 | // return false; | |
211 | // TmfEventType o = (TmfEventType) other; | |
212 | // return fTypeId.equals(o.fTypeId); | |
213 | // } | |
214 | // | |
215 | // @Override | |
216 | // @SuppressWarnings("nls") | |
217 | // public String toString() { | |
218 | // return "[TmfEventType:" + fTypeId + "]"; | |
219 | // } | |
220 | ||
28b94d61 | 221 | } |