2010-10-15 Francois Chouinard <fchouinard@gmail.com> Fix for Bug327910
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / event / TmfEventType.java
CommitLineData
8c8bf09f 1/*******************************************************************************
cbd4ad82 2 * Copyright (c) 2009, 2010 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
8c8bf09f
ASL
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.event;
14
28b94d61 15import java.util.HashMap;
28b94d61 16
8c8bf09f
ASL
17/**
18 * <b><u>TmfEventType</u></b>
19 * <p>
20 * The event type.
21 */
1a971e96 22public class TmfEventType implements Cloneable {
8c8bf09f 23
cbd4ad82 24 // ------------------------------------------------------------------------
28b94d61 25 // Constants
cbd4ad82 26 // ------------------------------------------------------------------------
28b94d61
FC
27
28 public static final String DEFAULT_TYPE_ID = "TMF Default Type";
29 public static final String[] DEFAULT_LABELS = new String[] { "Content" };
30
cbd4ad82 31 // ------------------------------------------------------------------------
8c8bf09f 32 // Attributes
cbd4ad82 33 // ------------------------------------------------------------------------
8c8bf09f 34
1a971e96
FC
35 private String fTypeId;
36 private String[] fFieldLabels;
37 private int fNbFields;
38 private HashMap<String, Integer> fFieldMap;
8c8bf09f 39
cbd4ad82 40 // ------------------------------------------------------------------------
8c8bf09f 41 // Constructors
cbd4ad82 42 // ------------------------------------------------------------------------
8c8bf09f 43
28b94d61 44 /**
cbd4ad82 45 * Default constructor
28b94d61
FC
46 */
47 public TmfEventType() {
48 this(DEFAULT_TYPE_ID, DEFAULT_LABELS);
49 }
50
8c8bf09f
ASL
51 /**
52 * @param type
53 * @param format
54 */
28b94d61 55 public TmfEventType(String typeId, String[] labels) {
cbd4ad82
FC
56 if (typeId == null || labels == null)
57 throw new IllegalArgumentException();
28b94d61
FC
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 /**
cbd4ad82 68 * Copy constructor
28b94d61
FC
69 * @param other
70 */
71 public TmfEventType(TmfEventType other) {
cbd4ad82
FC
72 if (other == null)
73 throw new IllegalArgumentException();
28b94d61
FC
74 fTypeId = other.fTypeId;
75 fFieldLabels = other.fFieldLabels;
76 fNbFields = other.fNbFields;
77 fFieldMap = other.fFieldMap;
8c8bf09f
ASL
78 }
79
cbd4ad82 80 // ------------------------------------------------------------------------
8c8bf09f 81 // Accessors
cbd4ad82 82 // ------------------------------------------------------------------------
8c8bf09f
ASL
83
84 /**
85 * @return
86 */
87 public String getTypeId() {
88 return fTypeId;
89 }
90
28b94d61
FC
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 */
cbd4ad82 118 public String getLabel(int i) throws TmfNoSuchFieldException {
28b94d61
FC
119 if (i >= 0 && i < fNbFields)
120 return fFieldLabels[i];
cbd4ad82 121 throw new TmfNoSuchFieldException("Bad index (" + i + ")");
28b94d61 122 }
8c8bf09f 123
cbd4ad82
FC
124 // ------------------------------------------------------------------------
125 // Object
126 // ------------------------------------------------------------------------
1f506a43 127
cbd4ad82
FC
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);
28b94d61
FC
139 }
140
1f506a43
FC
141 @Override
142 public String toString() {
28b94d61 143 return "[TmfEventType:" + fTypeId + "]";
1f506a43
FC
144 }
145
1a971e96
FC
146 @Override
147 public TmfEventType clone() {
148 TmfEventType clone = null;
149 try {
150 clone = (TmfEventType) super.clone();
151 clone.fTypeId = new String(fTypeId);
152 clone.fNbFields = fNbFields;
153 // Clone the field labels
154 clone.fFieldLabels = new String[fFieldLabels.length];
155 for (int i = 0; i < fFieldLabels.length; i++) {
156 clone.fFieldLabels[i] = new String(fFieldLabels[i]);
157 }
158 // Clone the fields
159 clone.fFieldMap = new HashMap<String, Integer>();
160 for (String key : fFieldMap.keySet()) {
161 clone.fFieldMap.put(new String(key), new Integer(fFieldMap.get(key)));
162 }
163 }
164 catch (CloneNotSupportedException e) {
165 e.printStackTrace();
166 }
167 return clone;
168 }
28b94d61 169}
This page took 0.033591 seconds and 5 git commands to generate.