Skip to content

Method: hashCode()

1: package pruefskript.parser.nodes;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: import basic.PruefskriptConstants;
7: import pruefskript.CommunicationManager;
8: import pruefskript.parser.exceptions.CheckScriptException;
9: import pruefskript.parser.exceptions.WrongNodeException;
10: import pruefskript.parser.values.AbstractReturnValue;
11: import pruefskript.parser.values.ArrayValue;
12:
13: /**
14: * IdSetNode.
15: *
16: * @author Group B5
17: *
18: */
19: public class IdSetNode extends AbstractNode {
20:         /**
21:          * final String Attribut value.
22:          */
23:         private final String value;
24:
25:         /**
26:          * final value for index.
27:          */
28:         private AbstractNode index;
29:         /**
30:          * value if an index exists.
31:          */
32:         private Boolean indexExists;
33:
34:         /**
35:          * IdSetNode.
36:          *
37:          * @param value
38:          * String
39:          */
40:         public IdSetNode(final String value) {
41:                 super();
42:                 this.value = value;
43:                 this.index = null;
44:                 this.indexExists = false;
45:         }
46:
47:         @Override
48:         public String toString() {
49:                 return PruefskriptConstants.IDSETNODETITLE + PruefskriptConstants.BRACKET_OPEN
50:                                 + this.value + PruefskriptConstants.BRACKET_CLOSE;
51:         }
52:
53:         @Override
54:         public boolean equals(final Object obj) {
55:                 return obj instanceof IdSetNode && ((IdSetNode) obj).value.equals(this.value);
56:
57:         }
58:
59:         @Override
60:         public int hashCode() {
61:                 return this.value.hashCode();
62:         }
63:
64:         @Override
65:         public AbstractReturnValue interpret(final CommunicationManager mgr)
66:                         throws CheckScriptException {
67:                 AbstractReturnValue result = null;
68:                 if (this.indexExists) {
69:                         final AbstractReturnValue indexValue = this.index.interpret(mgr);
70:                         if (indexValue.isNumericValue()) {
71:                                 result = mgr.getVariableAssignmentsByDef(this.value)
72:                                                 .get(indexValue.toNumericValue().getValue() - 1);
73:                         } else if (indexValue.isArrayValue()) {
74:                                 final List<AbstractReturnValue> resultList = new ArrayList<AbstractReturnValue>();
75:                                 final List<AbstractReturnValue> list =
76:                                                 mgr.getVariableAssignmentsByDef(this.value);
77:                                 final List<AbstractReturnValue> indexes = indexValue.toArrayValue().getValues();
78:                                 for (int i = 0; i < indexes.size(); i++) {
79:                                         final AbstractReturnValue indexEntry = indexes.get(i);
80:                                         if (indexEntry.isNumericValue()) {
81:                                                 resultList.add(list.get(indexEntry.toNumericValue().getValue() - 1));
82:                                         } else {
83:                                                 throw new WrongNodeException(PruefskriptConstants.UNEXPECTED_NUMERIC);
84:                                         }
85:                                         result = new ArrayValue(resultList);
86:                                 }
87:                         } else {
88:                                 throw new WrongNodeException(PruefskriptConstants.UNEXPECTED_NUMERIC);
89:                         }
90:
91:                 } else {
92:                         result = new ArrayValue(mgr.getVariableAssignmentsByDef(this.value));
93:                 }
94:                 return result;
95:         }
96:
97:         /**
98:          *
99:          * @return value
100:          */
101:         public String getName() {
102:                 return this.value;
103:         }
104:
105:         @Override
106:         public Boolean isIDSetNode() {
107:                 return Boolean.TRUE;
108:         }
109:
110:         @Override
111:         public IdSetNode toIDSetNode() throws WrongNodeException {
112:                 return this;
113:         }
114:
115:         /**
116:          *
117:          * @param indexparam
118:          * Der Index.
119:          */
120:         public void addIndex(final AbstractNode indexparam) {
121:                 this.index = indexparam;
122:                 this.indexExists = true;
123:         }
124:
125:         /**
126:          * @return the index
127:          */
128:         public AbstractNode getIndex() {
129:                 return this.index;
130:         }
131:
132:         /**
133:          * @return the indexExists
134:          */
135:         public Boolean getIndexExists() {
136:                 return this.indexExists;
137:         }
138: }