String comparison may return any integer (not only 0, 1 and -1)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDeclaration.java
CommitLineData
866e5b51
FC
1/*******************************************************************************
2 * Copyright (c) 2011-2012 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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.List;
18
19/**
20 * <b><u>StructDeclaration</u></b>
21 */
22public class StructDeclaration implements IDeclaration {
23
24 // ------------------------------------------------------------------------
25 // Attributes
26 // ------------------------------------------------------------------------
27
28 private final HashMap<String, IDeclaration> fields = new HashMap<String, IDeclaration>();
29 private final List<String> fieldsList = new LinkedList<String>();
2b7f6f09 30 private long maxAlign;
866e5b51
FC
31
32 // ------------------------------------------------------------------------
33 // Constructors
34 // ------------------------------------------------------------------------
35
2b7f6f09
MK
36 public StructDeclaration(long align) {
37 this.maxAlign = Math.max(align, 1);
866e5b51
FC
38 }
39
40 // ------------------------------------------------------------------------
41 // Getters/Setters/Predicates
42 // ------------------------------------------------------------------------
43
2b7f6f09
MK
44 public long getMaxAlign() {
45 return maxAlign;
866e5b51
FC
46 }
47
48 public boolean hasField(String name) {
49 return this.fields.containsKey(name);
50 }
51
52 public HashMap<String, IDeclaration> getFields() {
53 return this.fields;
54 }
55
56 public List<String> getFieldsList() {
57 return this.fieldsList;
58 }
59
fd74e6c1
MK
60 @Override
61 public long getAlignment() {
2b7f6f09 62 return this.maxAlign;
fd74e6c1 63 }
866e5b51
FC
64 // ------------------------------------------------------------------------
65 // Operations
66 // ------------------------------------------------------------------------
67
68 @Override
69 public StructDefinition createDefinition(IDefinitionScope definitionScope,
70 String fieldName) {
71 return new StructDefinition(this, definitionScope, fieldName);
72 }
73
74 public void addField(String name, IDeclaration declaration) {
866e5b51
FC
75 this.fields.put(name, declaration);
76 this.fieldsList.add(name);
2b7f6f09
MK
77 maxAlign = Math.max(maxAlign, declaration.getAlignment());
78 if( maxAlign == 1 )
79 {
80 maxAlign =1;
81 }
866e5b51
FC
82 }
83
84 @Override
85 public String toString() {
86 /* Only used for debugging */
87 return "[declaration] struct[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
88 }
89
4dd0eaed
MK
90 /* (non-Javadoc)
91 * @see java.lang.Object#hashCode()
92 */
93 @Override
94 public int hashCode() {
95 final int prime = 31;
96 int result = 1;
97 result = (prime * result)
98 + ((fieldsList == null) ? 0 : fieldsList.hashCode());
99 result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32));
100 return result;
101 }
102
103 /* (non-Javadoc)
104 * @see java.lang.Object#equals(java.lang.Object)
105 */
106 @Override
107 public boolean equals(Object obj) {
108 if (this == obj) {
109 return true;
110 }
111 if (obj == null) {
112 return false;
113 }
114 if (!(obj instanceof StructDeclaration)) {
115 return false;
116 }
117 StructDeclaration other = (StructDeclaration) obj;
118 if (fieldsList == null) {
119 if (other.fieldsList != null) {
120 return false;
121 }
122 } else if (!fieldsList.equals(other.fieldsList)) {
123 return false;
124 }
125 if (maxAlign != other.maxAlign) {
126 return false;
127 }
128 return true;
129 }
130
866e5b51 131}
This page took 0.034743 seconds and 5 git commands to generate.