Skip to main content
summaryrefslogtreecommitdiffstats
blob: 143770b6e3436497bea09e3b8eb5ecee51208609 (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
/**
 * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *         Florian Pirchner - Initial implementation
 */
package org.eclipse.osbp.runtime.web.vaadin.components.converter;

import java.util.Locale;

import com.vaadin.data.util.converter.AbstractStringToNumberConverter;

@SuppressWarnings("serial")
public class StringToNumberConverter extends
		AbstractStringToNumberConverter<Number> {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
	 * java.util.Locale)
	 */
	@Override
	public Number convertToModel(String value,
			Class<? extends Number> targetType, Locale locale)
			throws ConversionException {
		Number n = convertToNumber(value, targetType, locale);
		return n == null ? null : toResult(n, targetType);
	}

	protected Number toResult(Number n, Class<? extends Number> targetType) {
		if (targetType == Byte.class) {
			return (Byte) n.byteValue();
		} else if (targetType == Short.class) {
			return (Short) n.shortValue();
		} else if (targetType == Integer.class) {
			return (Integer) n.intValue();
		} else if (targetType == Long.class) {
			return (Long) n.longValue();
		} else if (targetType == Float.class) {
			return (Float) n.floatValue();
		} else if (targetType == Double.class) {
			return (Double) n.doubleValue();
		}
		return n.doubleValue();
	}

	@Override
	public Class<Number> getModelType() {
		return Number.class;
	}

}

Back to the top