blob: ac5d906abe9fc89665a2f8b9dcecedd7fc860ec2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/*******************************************************************************
* Copyright (c) 2011 protos software gmbh (http://www.protos.de).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* CONTRIBUTORS:
* Henrik Rentz-Reichert (initial contribution)
*
*******************************************************************************/
package org.eclipse.etrice.core.converter;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.conversion.impl.AbstractValueConverter;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.util.Strings;
/**
* @author Henrik Rentz-Reichert
*
*/
public class MultiplicityConverter extends AbstractValueConverter<Integer> {
public Integer toValue(String string, INode node) {
if (Strings.isEmpty(string))
throw new ValueConverterException("Couldn't convert empty string to integer.", node, null);
if (string.indexOf("*") >= 0)
return -1;
// we have to find the substring containing the digits
int first = 0;
for (; first < string.length(); ++first)
if (Character.isDigit(string.charAt(first)))
break;
int last = first;
for (; last < string.length(); ++last)
if (!Character.isDigit(string.charAt(last)))
break;
String val = string.substring(first, last);
try {
int intValue = Integer.parseInt(val);
return Integer.valueOf(intValue);
}
catch (NumberFormatException e) {
throw new ValueConverterException("Couldn't convert '" + val + "' to integer.", node, e);
}
}
@Override
public String toString(Integer value) throws ValueConverterException {
if (value == -1)
return "[*]";
return "[" + value.toString() + "]";
}
}
|