Mappings.jrag 9.56 KB
Newer Older
1
2
aspect DefaultMappings {

3
  private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinitionFromBytes(String typeName) {
4
5
6
7
8
9
10
11
    DefaultMappingDefinition result = new DefaultMappingDefinition();
    result.setID("_DefaultBytesTo" + Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1) + "Mapping");
    result.setFromType(new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse("byte")));
    result.setFromVariableName("bytes");
    result.setToType(new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName)));
    return result;
  }

12
  private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinitionToBytes(String typeName) {
13
14
15
16
17
18
19
20
    DefaultMappingDefinition result = new DefaultMappingDefinition();
    result.setID("_Default" + Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1) + "ToBytesMapping");
    result.setFromType(new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName)));
    result.setFromVariableName("input");
    result.setToType(new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse("byte")));
    return result;
  }

21
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() {
22
23
24
25
26
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("int");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getInt();");
    return result;
  }

27
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToShortMapping() {
28
29
30
31
32
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("short");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getShort();");
    return result;
  }

33
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToLongMapping() {
34
35
36
37
38
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("long");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getLong();");
    return result;
  }

39
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToFloatMapping() {
40
41
42
43
44
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("float");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getFloat();");
    return result;
  }

45
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToDoubleMapping() {
46
47
48
49
50
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("double");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getDouble();");
    return result;
  }

51
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToCharMapping() {
52
53
54
55
56
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("char");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getChar();");
    return result;
  }

57
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToStringMapping() {
58
59
60
61
62
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("String");
    result.setContent("return new String(bytes);");
    return result;
  }

63
  syn nta DefaultMappingDefinition RagConnect.defaultIntToBytesMapping() {
64
65
66
67
68
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("int");
    result.setContent("return java.nio.ByteBuffer.allocate(4).putInt(input).array();");
    return result;
  }

69
  syn nta DefaultMappingDefinition RagConnect.defaultShortToBytesMapping() {
70
71
72
73
74
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("short");
    result.setContent("return java.nio.ByteBuffer.allocate(2).putShort(input).array();");
    return result;
  }

75
  syn nta DefaultMappingDefinition RagConnect.defaultLongToBytesMapping() {
76
77
78
79
80
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("long");
    result.setContent("return java.nio.ByteBuffer.allocate(8).putLong(input).array();");
    return result;
  }

81
  syn nta DefaultMappingDefinition RagConnect.defaultFloatToBytesMapping() {
82
83
84
85
86
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("float");
    result.setContent("return java.nio.ByteBuffer.allocate(4).putFloat(input).array();");
    return result;
  }

87
  syn nta DefaultMappingDefinition RagConnect.defaultDoubleToBytesMapping() {
88
89
90
91
92
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("double");
    result.setContent("return java.nio.ByteBuffer.allocate(8).putDouble(input).array();");
    return result;
  }

93
  syn nta DefaultMappingDefinition RagConnect.defaultCharToBytesMapping() {
94
95
96
97
98
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("char");
    result.setContent("return java.nio.ByteBuffer.allocate(2).putChar(input).array();");
    return result;
  }

99
  syn nta DefaultMappingDefinition RagConnect.defaultStringToBytesMapping() {
100
101
102
103
104
105
106
107
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("String");
    result.setContent("return input.getBytes();");
    return result;
  }
}

aspect Mappings {
  // --- effectiveMappings ---
108
  syn java.util.List<MappingDefinition> EndpointDefinition.effectiveMappings();
109
110
111
112
113
  eq ReadFromMqttDefinition.effectiveMappings() {
    // if there is a first mapping, check if its input type is byte[].
    //  or if no mappings are specified.
    // then prepend the suitable default mapping
    java.util.List<MappingDefinition> result;
114
    if (getMappingList().isEmpty() || !getMappingList().get(0).getFromType().isByteArray()) {
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      result = new java.util.ArrayList();
      result.add(suitableDefaultMapping());
      result.addAll(getMappingList());
    } else {
      result = getMappingList();
    }
    return result;
  }
  eq WriteToMqttDefinition.effectiveMappings() {
    // if there is a mapping, check if the output type of the last mapping is byte[].
    //  or if no mappings are specified.
    // then append the suitable default mapping
    java.util.List<MappingDefinition> result;
    int numMappings = getMappingList().size();
    if (numMappings == 0 || !getMappingList().get(numMappings - 1).getToType().isByteArray()) {
      result = new java.util.ArrayList();
      result.addAll(getMappingList());
      result.add(suitableDefaultMapping());
    } else {
      result = getMappingList();
    }
    return result;
  }

René Schöne's avatar
René Schöne committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  // --- isPrimitiveType ---
  syn boolean TokenComponent.isPrimitiveType() = getJavaTypeUse().isPrimitiveType();
  syn boolean JavaTypeUse.isPrimitiveType() = false;
  eq SimpleJavaTypeUse.isPrimitiveType() {
    switch(getName()) {
      case "int":
      case "short":
      case "long":
      case "float":
      case "double":
      case "char":
      case "byte": return true;
      default: return false;
    }
  }
René Schöne's avatar
René Schöne committed
154
155
156
157
158
159
  syn boolean MappingDefinitionType.isPrimitiveType() = false;
  eq JavaMappingDefinitionType.isPrimitiveType() = getType().isPrimitiveType();

  // --- isArray ---
  syn boolean MappingDefinitionType.isArray() = false;
  eq JavaArrayMappingDefinitionType.isArray() = true;
René Schöne's avatar
René Schöne committed
160

161
  // --- suitableDefaultMapping ---
162
  syn DefaultMappingDefinition EndpointDefinition.suitableDefaultMapping();
163
  eq ReadFromMqttDefinition.suitableDefaultMapping() {
164
165
166
    String typeName = getMappingList().isEmpty() ?
        getToken().getJavaTypeUse().getName() :
        getMappingList().get(0).getFromType().prettyPrint();
167
168
    switch(typeName) {
      case "int":
169
      case "Integer": return ragconnect().defaultBytesToIntMapping();
170
      case "short":
171
      case "Short": return ragconnect().defaultBytesToShortMapping();
172
      case "long":
173
      case "Long": return ragconnect().defaultBytesToLongMapping();
174
      case "float":
175
      case "Float": return ragconnect().defaultBytesToFloatMapping();
176
      case "double":
177
      case "Double": return ragconnect().defaultBytesToDoubleMapping();
178
      case "char":
179
180
      case "Character": return ragconnect().defaultBytesToCharMapping();
      case "String": return ragconnect().defaultBytesToStringMapping();
181
182
183
184
      default: return null;
    }
  }
  eq WriteToMqttDefinition.suitableDefaultMapping() {
185
186
187
    String typeName = getMappingList().isEmpty() ?
        getToken().getJavaTypeUse().getName() :
        getMappingList().get(getMappingList().size() - 1).getFromType().prettyPrint();
188
189
    switch(typeName) {
      case "int":
190
      case "Integer": return ragconnect().defaultIntToBytesMapping();
191
      case "short":
192
      case "Short": return ragconnect().defaultShortToBytesMapping();
193
      case "long":
194
      case "Long": return ragconnect().defaultLongToBytesMapping();
195
      case "float":
196
      case "Float": return ragconnect().defaultFloatToBytesMapping();
197
      case "double":
198
      case "Double": return ragconnect().defaultDoubleToBytesMapping();
199
      case "char":
200
201
      case "Character": return ragconnect().defaultCharToBytesMapping();
      case "String": return ragconnect().defaultStringToBytesMapping();
202
203
204
205
206
207
208
209
210
      default: return null;
    }
  }

  // --- isByteArray ---
  syn boolean MappingDefinitionType.isByteArray() = false;
  eq JavaArrayMappingDefinitionType.isByteArray() = getType().getName().equals("byte");

  // --- allMappingDefinitions ---
211
  syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() {
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
    getMappingDefinitionList().iterator().forEachRemaining(result::add);
    result.add(defaultBytesToIntMapping());
    result.add(defaultBytesToShortMapping());
    result.add(defaultBytesToLongMapping());
    result.add(defaultBytesToFloatMapping());
    result.add(defaultBytesToDoubleMapping());
    result.add(defaultBytesToCharMapping());
    result.add(defaultBytesToStringMapping());
    result.add(defaultIntToBytesMapping());
    result.add(defaultShortToBytesMapping());
    result.add(defaultLongToBytesMapping());
    result.add(defaultFloatToBytesMapping());
    result.add(defaultDoubleToBytesMapping());
    result.add(defaultCharToBytesMapping());
    result.add(defaultStringToBytesMapping());
    return result;
  }
}