Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventType.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.event;
14
15 import java.util.HashMap;
16
17 /**
18 * <b><u>TmfEventType</u></b>
19 * <p>
20 * The event type.
21 */
22 public class TmfEventType implements Cloneable {
23
24 // ------------------------------------------------------------------------
25 // Constants
26 // ------------------------------------------------------------------------
27
28 public static final String DEFAULT_TYPE_ID = "TMF Default Type"; //$NON-NLS-1$
29 public static final String[] DEFAULT_LABELS = new String[] { "Content" }; //$NON-NLS-1$
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
35 private String fTypeId;
36 private String[] fFieldLabels;
37 private int fNbFields;
38 private HashMap<String, Integer> fFieldMap;
39
40 // ------------------------------------------------------------------------
41 // Constructors
42 // ------------------------------------------------------------------------
43
44 /**
45 * Default constructor
46 */
47 public TmfEventType() {
48 this(DEFAULT_TYPE_ID, DEFAULT_LABELS);
49 }
50
51 /**
52 * @param type
53 * @param format
54 */
55 public TmfEventType(String typeId, String[] labels) {
56 if (typeId == null || labels == null)
57 throw new IllegalArgumentException();
58 fTypeId = typeId;
59 fFieldLabels = labels;
60 fNbFields = fFieldLabels.length;
61 fFieldMap = new HashMap<String, Integer>();
62 for (int i = 0; i < fNbFields; i++) {
63 fFieldMap.put(fFieldLabels[i], i);
64 }
65 }
66
67 /**
68 * Copy constructor
69 * @param other
70 */
71 public TmfEventType(TmfEventType other) {
72 if (other == null)
73 throw new IllegalArgumentException();
74 fTypeId = other.fTypeId;
75 fFieldLabels = other.fFieldLabels;
76 fNbFields = other.fNbFields;
77 fFieldMap = other.fFieldMap;
78 }
79
80 // ------------------------------------------------------------------------
81 // Accessors
82 // ------------------------------------------------------------------------
83
84 /**
85 * @return
86 */
87 public String getTypeId() {
88 return fTypeId;
89 }
90
91 /**
92 * @return
93 */
94 public int getNbFields() {
95 return fNbFields;
96 }
97
98 /**
99 * @return
100 */
101 public int getFieldIndex(String id) throws TmfNoSuchFieldException {
102 Integer index = fFieldMap.get(id);
103 if (index == null)
104 throw(new TmfNoSuchFieldException(id));
105 return index;
106 }
107
108 /**
109 * @return
110 */
111 public String[] getLabels() {
112 return fFieldLabels;
113 }
114
115 /**
116 * @return
117 */
118 public String getLabel(int i) throws TmfNoSuchFieldException {
119 if (i >= 0 && i < fNbFields)
120 return fFieldLabels[i];
121 throw new TmfNoSuchFieldException("Bad index (" + i + ")"); //$NON-NLS-1$//$NON-NLS-2$
122 }
123
124 // ------------------------------------------------------------------------
125 // Object
126 // ------------------------------------------------------------------------
127
128 @Override
129 public int hashCode() {
130 return fTypeId.hashCode();
131 }
132
133 @Override
134 public boolean equals(Object other) {
135 if (!(other instanceof TmfEventType))
136 return false;
137 TmfEventType o = (TmfEventType) other;
138 return fTypeId.equals(o.fTypeId);
139 }
140
141 @Override
142 @SuppressWarnings("nls")
143 public String toString() {
144 return "[TmfEventType:" + fTypeId + "]";
145 }
146
147 @Override
148 public TmfEventType clone() {
149 TmfEventType clone = null;
150 try {
151 clone = (TmfEventType) super.clone();
152 clone.fTypeId = new String(fTypeId);
153 clone.fNbFields = fNbFields;
154 // Clone the field labels
155 clone.fFieldLabels = new String[fFieldLabels.length];
156 for (int i = 0; i < fFieldLabels.length; i++) {
157 clone.fFieldLabels[i] = new String(fFieldLabels[i]);
158 }
159 // Clone the fields
160 clone.fFieldMap = new HashMap<String, Integer>();
161 for (String key : fFieldMap.keySet()) {
162 clone.fFieldMap.put(new String(key), new Integer(fFieldMap.get(key)));
163 }
164 }
165 catch (CloneNotSupportedException e) {
166 e.printStackTrace();
167 }
168 return clone;
169 }
170 }
This page took 0.033912 seconds and 5 git commands to generate.