(no commit message)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / 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.event;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 /**
19 * <b><u>TmfEventType</u></b>
20 * <p>
21 * The event type.
22 */
23 public class TmfEventType {
24
25 // ------------------------------------------------------------------------
26 // Constants
27 // ------------------------------------------------------------------------
28
29 public static final String DEFAULT_TYPE_ID = "TMF Default Type";
30 public static final String[] DEFAULT_LABELS = new String[] { "Content" };
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
36 private final String fTypeId;
37 private final String[] fFieldLabels;
38 private final int fNbFields;
39 private final Map<String, Integer> fFieldMap;
40
41 // ------------------------------------------------------------------------
42 // Constructors
43 // ------------------------------------------------------------------------
44
45 /**
46 * Default constructor
47 */
48 public TmfEventType() {
49 this(DEFAULT_TYPE_ID, DEFAULT_LABELS);
50 }
51
52 /**
53 * @param type
54 * @param format
55 */
56 public TmfEventType(String typeId, String[] labels) {
57 if (typeId == null || labels == null)
58 throw new IllegalArgumentException();
59 fTypeId = typeId;
60 fFieldLabels = labels;
61 fNbFields = fFieldLabels.length;
62 fFieldMap = new HashMap<String, Integer>();
63 for (int i = 0; i < fNbFields; i++) {
64 fFieldMap.put(fFieldLabels[i], i);
65 }
66 }
67
68 /**
69 * Copy constructor
70 * @param other
71 */
72 public TmfEventType(TmfEventType other) {
73 if (other == null)
74 throw new IllegalArgumentException();
75 fTypeId = other.fTypeId;
76 fFieldLabels = other.fFieldLabels;
77 fNbFields = other.fNbFields;
78 fFieldMap = other.fFieldMap;
79 }
80
81 // ------------------------------------------------------------------------
82 // Accessors
83 // ------------------------------------------------------------------------
84
85 /**
86 * @return
87 */
88 public String getTypeId() {
89 return fTypeId;
90 }
91
92 /**
93 * @return
94 */
95 public int getNbFields() {
96 return fNbFields;
97 }
98
99 /**
100 * @return
101 */
102 public int getFieldIndex(String id) throws TmfNoSuchFieldException {
103 Integer index = fFieldMap.get(id);
104 if (index == null)
105 throw(new TmfNoSuchFieldException(id));
106 return index;
107 }
108
109 /**
110 * @return
111 */
112 public String[] getLabels() {
113 return fFieldLabels;
114 }
115
116 /**
117 * @return
118 */
119 public String getLabel(int i) throws TmfNoSuchFieldException {
120 if (i >= 0 && i < fNbFields)
121 return fFieldLabels[i];
122 throw new TmfNoSuchFieldException("Bad index (" + i + ")");
123 }
124
125 // ------------------------------------------------------------------------
126 // Object
127 // ------------------------------------------------------------------------
128
129 @Override
130 public int hashCode() {
131 return fTypeId.hashCode();
132 }
133
134 @Override
135 public boolean equals(Object other) {
136 if (!(other instanceof TmfEventType))
137 return false;
138 TmfEventType o = (TmfEventType) other;
139 return fTypeId.equals(o.fTypeId);
140 }
141
142 @Override
143 public String toString() {
144 return "[TmfEventType:" + fTypeId + "]";
145 }
146
147 }
This page took 0.032785 seconds and 5 git commands to generate.