1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.event
;
15 import java
.util
.HashMap
;
18 * <b><u>TmfEventType</u></b>
22 public class TmfEventType
implements Cloneable
{
24 // ------------------------------------------------------------------------
26 // ------------------------------------------------------------------------
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$
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 private String fTypeId
;
36 private String
[] fFieldLabels
;
37 private int fNbFields
;
38 private HashMap
<String
, Integer
> fFieldMap
;
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
47 public TmfEventType() {
48 this(DEFAULT_TYPE_ID
, DEFAULT_LABELS
);
55 public TmfEventType(String typeId
, String
[] labels
) {
56 if (typeId
== null || labels
== null)
57 throw new IllegalArgumentException();
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
);
71 public TmfEventType(TmfEventType other
) {
73 throw new IllegalArgumentException();
74 fTypeId
= other
.fTypeId
;
75 fFieldLabels
= other
.fFieldLabels
;
76 fNbFields
= other
.fNbFields
;
77 fFieldMap
= other
.fFieldMap
;
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
87 public String
getTypeId() {
94 public int getNbFields() {
101 public int getFieldIndex(String id
) throws TmfNoSuchFieldException
{
102 Integer index
= fFieldMap
.get(id
);
104 throw(new TmfNoSuchFieldException(id
));
111 public String
[] getLabels() {
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$
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
129 public int hashCode() {
130 return fTypeId
.hashCode();
134 public boolean equals(Object other
) {
135 if (!(other
instanceof TmfEventType
))
137 TmfEventType o
= (TmfEventType
) other
;
138 return fTypeId
.equals(o
.fTypeId
);
142 @SuppressWarnings("nls")
143 public String
toString() {
144 return "[TmfEventType:" + fTypeId
+ "]";
148 public TmfEventType
clone() {
149 TmfEventType clone
= null;
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
]);
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
)));
165 catch (CloneNotSupportedException e
) {