| /** |
| * 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; |
| } |
| |
| } |