Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.tmf.event; | |
14 | ||
28b94d61 | 15 | import java.util.HashMap; |
28b94d61 | 16 | |
8c8bf09f ASL |
17 | /** |
18 | * <b><u>TmfEventType</u></b> | |
19 | * <p> | |
20 | * The event type. | |
21 | */ | |
1a971e96 | 22 | public class TmfEventType implements Cloneable { |
8c8bf09f | 23 | |
cbd4ad82 | 24 | // ------------------------------------------------------------------------ |
28b94d61 | 25 | // Constants |
cbd4ad82 | 26 | // ------------------------------------------------------------------------ |
28b94d61 | 27 | |
3b38ea61 FC |
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$ | |
28b94d61 | 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]; | |
3b38ea61 | 121 | throw new TmfNoSuchFieldException("Bad index (" + i + ")"); //$NON-NLS-1$//$NON-NLS-2$ |
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 | 141 | @Override |
3b38ea61 | 142 | @SuppressWarnings("nls") |
1f506a43 | 143 | public String toString() { |
28b94d61 | 144 | return "[TmfEventType:" + fTypeId + "]"; |
1f506a43 FC |
145 | } |
146 | ||
1a971e96 FC |
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 | } | |
28b94d61 | 170 | } |