ctf: Fix some Sonar warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
4bd7f2db 2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
866e5b51
FC
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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.List;
0594c61c 18import java.util.Map;
866e5b51
FC
19
20/**
d37aaa7f 21 * A CTF structure declaration.
77fdc5df 22 *
d37aaa7f
FC
23 * A structure is similar to a C structure, it is a compound data type that
24 * contains other datatypes in fields. they are stored in an hashmap and indexed
25 * by names which are strings.
26 *
27 * @version 1.0
28 * @author Matthew Khouzam
29 * @author Simon Marchi
866e5b51
FC
30 */
31public class StructDeclaration implements IDeclaration {
32
33 // ------------------------------------------------------------------------
34 // Attributes
35 // ------------------------------------------------------------------------
36
0594c61c 37 private final Map<String, IDeclaration> fields = new HashMap<String, IDeclaration>();
866e5b51 38 private final List<String> fieldsList = new LinkedList<String>();
2b7f6f09 39 private long maxAlign;
866e5b51
FC
40
41 // ------------------------------------------------------------------------
42 // Constructors
43 // ------------------------------------------------------------------------
44
9ac2eb62
MK
45 /**
46 * The struct declaration, add fields later
47 *
48 * @param align
49 * the minimum alignment of the struct. (if a struct is 8bit
50 * aligned and has a 32 bit aligned field, the struct becomes 32
51 * bit aligned.
52 */
2b7f6f09
MK
53 public StructDeclaration(long align) {
54 this.maxAlign = Math.max(align, 1);
866e5b51
FC
55 }
56
57 // ------------------------------------------------------------------------
58 // Getters/Setters/Predicates
59 // ------------------------------------------------------------------------
60
9ac2eb62
MK
61 /**
62 * Get current alignment
63 * @return the alignment of the struct and all its fields
64 */
2b7f6f09
MK
65 public long getMaxAlign() {
66 return maxAlign;
866e5b51
FC
67 }
68
9ac2eb62
MK
69 /**
70 * Query if the struct has a given field
71 * @param name the name of the field, scopeless please
72 * @return does the field exist?
73 */
866e5b51
FC
74 public boolean hasField(String name) {
75 return this.fields.containsKey(name);
76 }
77
9ac2eb62
MK
78 /**
79 * get the fields of the struct in a map. Faster access time than a list.
80 * @return a HashMap of the fields (key is the name)
0594c61c 81 * @since 2.0
9ac2eb62 82 */
0594c61c 83 public Map<String, IDeclaration> getFields() {
866e5b51
FC
84 return this.fields;
85 }
86
9ac2eb62
MK
87 /**
88 * Gets the field list. Very important since the map of fields does not retain the order of the fields.
89 * @return the field list.
90 */
866e5b51
FC
91 public List<String> getFieldsList() {
92 return this.fieldsList;
93 }
94
fd74e6c1
MK
95 @Override
96 public long getAlignment() {
2b7f6f09 97 return this.maxAlign;
fd74e6c1 98 }
9ac2eb62 99
866e5b51
FC
100 // ------------------------------------------------------------------------
101 // Operations
102 // ------------------------------------------------------------------------
103
104 @Override
105 public StructDefinition createDefinition(IDefinitionScope definitionScope,
106 String fieldName) {
107 return new StructDefinition(this, definitionScope, fieldName);
108 }
109
9ac2eb62
MK
110 /**
111 * Add a field to the struct
112 * @param name the name of the field, scopeless
113 * @param declaration the declaration of the field
114 */
866e5b51 115 public void addField(String name, IDeclaration declaration) {
866e5b51
FC
116 this.fields.put(name, declaration);
117 this.fieldsList.add(name);
2b7f6f09 118 maxAlign = Math.max(maxAlign, declaration.getAlignment());
9ac2eb62
MK
119 if (maxAlign == 1) {
120 maxAlign = 1;
2b7f6f09 121 }
866e5b51
FC
122 }
123
124 @Override
125 public String toString() {
126 /* Only used for debugging */
127 return "[declaration] struct[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
128 }
129
4dd0eaed
MK
130 @Override
131 public int hashCode() {
132 final int prime = 31;
133 int result = 1;
77fdc5df 134 result = (prime * result) + fieldsList.hashCode();
4dd0eaed
MK
135 result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32));
136 return result;
137 }
138
4dd0eaed
MK
139 @Override
140 public boolean equals(Object obj) {
141 if (this == obj) {
142 return true;
143 }
144 if (obj == null) {
145 return false;
146 }
147 if (!(obj instanceof StructDeclaration)) {
148 return false;
149 }
150 StructDeclaration other = (StructDeclaration) obj;
77fdc5df 151 if (!fieldsList.equals(other.fieldsList)) {
4dd0eaed
MK
152 return false;
153 }
154 if (maxAlign != other.maxAlign) {
155 return false;
156 }
157 return true;
158 }
159
866e5b51 160}
This page took 0.03809 seconds and 5 git commands to generate.