001/*
002 * This file is part of the Tuning Fork Visualization Platform
003 *  (http://sourceforge.net/projects/tuningforkvp)
004 *
005 * Copyright (c) 2005 - 2008 IBM Corporation.
006 * All rights reserved. This program and the accompanying materials
007 * are made available under the terms of the Eclipse Public License v1.0
008 * which accompanies this distribution, and is available at
009 * http://www.eclipse.org/legal/epl-v10.html
010 *
011 * Contributors:
012 *     IBM Corporation - initial API and implementation
013 */
014
015package com.ibm.tuningfork.tracegen.types;
016
017import org.vmmagic.pragma.Interruptible;
018import org.vmmagic.pragma.Uninterruptible;
019
020/**
021 * An EventType describes the types and attributes of the data values associated
022 * with a particular event index.
023 */
024@Uninterruptible
025public final class EventType {
026
027    private final int index;
028    private final String name;
029    private final String description;
030    private final EventAttribute[] attributes;
031    private final int numberOfInts;
032    private final int numberOfLongs;
033    private final int numberOfDoubles;
034    private final int numberOfStrings;
035
036    private static int currentIndex = 0;
037
038    /**
039     * Does this event type accept the supplied number of attributes of each
040     * type.
041     *
042     * @param numInt
043     *                The number of int attributes.
044     * @param numLong
045     *                The number of long attributes.
046     * @param numDouble
047     *                The number of double attributes.
048     * @param numString
049     *                The number of String attributes.
050     * @return True if the event will accept attributes of the specified types.
051     */
052    public boolean admits(int numInt, int numLong, int numDouble, int numString) {
053        return (numInt == numberOfInts) && (numLong == numberOfLongs)
054                && (numDouble == numberOfDoubles)
055                && (numString == numberOfStrings);
056    }
057
058    /**
059     * Create a new event type with no attributes.
060     *
061     * @param name
062     *                The name of the event type.
063     * @param description
064     *                A description of the event type.
065     */
066    public EventType(String name, String description) {
067        this(name, description, new EventAttribute[] {});
068    }
069
070    /**
071     * Create a new event type with a single attribute.
072     *
073     * @param name
074     *                The name of the event type.
075     * @param description
076     *                A description of the event type.
077     * @param attribute
078     *                The event attribute for this event type.
079     */
080    public EventType(String name, String description, EventAttribute attribute) {
081        this(name, description, new EventAttribute[] { attribute });
082    }
083
084    @Interruptible
085    private static synchronized int getNextIndex() {
086        return currentIndex++;
087    }
088
089    /**
090     * Create a new event type with the supplied attributes.
091     *
092     * @param name
093     *                The name of the event type.
094     * @param description
095     *                A description of the event type.
096     * @param attributes
097     *                The event attributes for this event type.
098     */
099    public EventType(String name, String description,
100            EventAttribute[] attributes) {
101        this.index = getNextIndex();
102        this.name = name;
103        this.description = description;
104        this.attributes = attributes;
105        int ic = 0;
106        int lc = 0;
107        int dc = 0;
108        int sc = 0;
109        for (int i = 0; i < attributes.length; i++) {
110            ScalarType at = attributes[i].getType();
111            if (at.equals(ScalarType.INT)) {
112                ic++;
113                checkOrder(lc + dc + sc);
114            } else if (at.equals(ScalarType.LONG)) {
115                lc++;
116                checkOrder(dc + sc);
117            } else if (at.equals(ScalarType.DOUBLE)) {
118                dc++;
119                checkOrder(sc);
120            } else if (at.equals(ScalarType.STRING)) {
121                sc++;
122            } else {
123                throw new IllegalArgumentException("EventType constructor: Unsupported event attribute type");
124            }
125        }
126        numberOfInts = ic;
127        numberOfLongs = lc;
128        numberOfDoubles = dc;
129        numberOfStrings = sc;
130    }
131
132    /**
133     * Check that attributes are declared in the proper order
134     *
135     * @param others
136     *                the sum of the attribute declarations that should come
137     *                later but have already occurred
138     */
139    @Interruptible
140    private void checkOrder(int others) {
141        if (others > 0) {
142            throw new IllegalArgumentException("EventType constructor: attributes not declared in the required order int/long/double/String");
143        }
144    }
145
146    public final int getIndex() {
147        return index;
148    }
149
150    /**
151     * Return the name of this event type.
152     *
153     * @return The name of this event type.
154     */
155    public final String getName() {
156        return name;
157    }
158
159    /**
160     * Return the description of this event type.
161     *
162     * @return The description of this event type.
163     */
164    public final String getDescription() {
165        return description;
166    }
167
168    /**
169     * Return the number of attributes this event type accepts.
170     *
171     * @return The number of attributes.
172     */
173    public final int getNumberOfAttributes() {
174        return attributes.length;
175    }
176
177    /**
178     * Return the number of int attributes this event type accepts.
179     *
180     * @return The number of int attributes.
181     */
182    public final int getNumberOfInts() {
183        return numberOfInts;
184    }
185
186    /**
187     * Return the number of long attributes this event type accepts.
188     *
189     * @return The number of long attributes.
190     */
191    public final int getNumberOfLongs() {
192        return numberOfLongs;
193    }
194
195    /**
196     * Return the number of double attributes this event type accepts.
197     *
198     * @return The number of double attributes.
199     */
200    public final int getNumberOfDoubles() {
201        return numberOfDoubles;
202    }
203
204    /**
205     * Return the number of string attributes this event type accepts.
206     *
207     * @return The number of string attributes.
208     */
209    public final int getNumberOfStrings() {
210        return numberOfStrings;
211    }
212
213    /**
214     * Return the ith attribute of this event type.
215     *
216     * @param i The index of the attribute to return.
217     * @return The attribute.
218     */
219    public final EventAttribute getAttribute(final int i) {
220        return attributes[i];
221    }
222
223}