| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are made |
| 5 | * 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: Matthew Khouzam - Initial API and implementation |
| 10 | * Contributors: Simon Marchi - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.ctf.core.event.types; |
| 14 | |
| 15 | import org.eclipse.jdt.annotation.NonNull; |
| 16 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; |
| 17 | |
| 18 | /** |
| 19 | * A CTF enum definition. |
| 20 | * |
| 21 | * The definition of a enum point basic data type. It will take the data from a |
| 22 | * trace and store it (and make it fit) as an integer and a string. |
| 23 | * |
| 24 | * @version 1.0 |
| 25 | * @author Matthew Khouzam |
| 26 | * @author Simon Marchi |
| 27 | */ |
| 28 | public final class EnumDefinition extends SimpleDatatypeDefinition { |
| 29 | |
| 30 | // ------------------------------------------------------------------------ |
| 31 | // Attributes |
| 32 | // ------------------------------------------------------------------------ |
| 33 | |
| 34 | private final long fIntegerValue; |
| 35 | |
| 36 | private final String fValue; |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | // Constructors |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | /** |
| 43 | * Constructor |
| 44 | * |
| 45 | * @param declaration |
| 46 | * the parent declaration |
| 47 | * @param definitionScope |
| 48 | * the parent scope |
| 49 | * @param fieldName |
| 50 | * the field name |
| 51 | * @param intValue |
| 52 | * the value of the enum |
| 53 | */ |
| 54 | public EnumDefinition(@NonNull EnumDeclaration declaration, |
| 55 | IDefinitionScope definitionScope, @NonNull String fieldName, IntegerDefinition intValue) { |
| 56 | super(declaration, definitionScope, fieldName); |
| 57 | |
| 58 | fIntegerValue = intValue.getValue(); |
| 59 | fValue = declaration.query(fIntegerValue); |
| 60 | } |
| 61 | |
| 62 | // ------------------------------------------------------------------------ |
| 63 | // Getters/Setters/Predicates |
| 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 66 | /** |
| 67 | * Gets the value of the enum in string format so |
| 68 | * "Enum a{DAY="0", NIGHT="1"}; will return "DAY" |
| 69 | * |
| 70 | * @return the value of the enum. |
| 71 | */ |
| 72 | public String getValue() { |
| 73 | return fValue; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String getStringValue() { |
| 78 | return getValue(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets the value of the enum in string format so |
| 83 | * "Enum a{DAY="0", NIGHT="1"}; will return 0 |
| 84 | * |
| 85 | * @return the value of the enum. |
| 86 | */ |
| 87 | @Override |
| 88 | public Long getIntegerValue() { |
| 89 | return fIntegerValue; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public EnumDeclaration getDeclaration() { |
| 94 | return (EnumDeclaration) super.getDeclaration(); |
| 95 | } |
| 96 | |
| 97 | // ------------------------------------------------------------------------ |
| 98 | // Operations |
| 99 | // ------------------------------------------------------------------------ |
| 100 | |
| 101 | @Override |
| 102 | public String toString() { |
| 103 | return "{ value = " + getValue() + //$NON-NLS-1$ |
| 104 | ", container = " + fIntegerValue + //$NON-NLS-1$ |
| 105 | " }"; //$NON-NLS-1$ |
| 106 | } |
| 107 | } |