Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T parse(Class<T> type, String stringValue) throws IllegalArgumentException {
        if (type.equals(String.class)) {
            return (T) stringValue;
        }

        if (type.equals(Boolean.class) || type.equals(boolean.class)) {
            return (T) Boolean.valueOf(stringValue);
        }
        try {
            if (type.equals(Integer.class) || type.equals(int.class)) {
                return (T) Integer.valueOf(stringValue);
            }
            if (type.equals(Long.class) || type.equals(long.class)) {
                return (T) Long.valueOf(stringValue);
            }
            if (type.equals(Short.class) || type.equals(short.class)) {
                return (T) Short.valueOf(stringValue);
            }
            if (type.equals(Byte.class) || type.equals(byte.class)) {
                return (T) Byte.valueOf(stringValue);
            }
            if (type.equals(Double.class) || type.equals(double.class)) {
                return (T) Double.valueOf(stringValue);
            }
            if (type.equals(Float.class) || type.equals(float.class)) {
                return (T) Float.valueOf(stringValue);
            }
            if (type.equals(File.class)) {
                return (T) new File(stringValue);
            }
        } catch (final NumberFormatException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
        if (type.isEnum()) {
            @SuppressWarnings("rawtypes")
            final Class enumType = type;
            return (T) Enum.valueOf(enumType, stringValue);
        }
        throw new IllegalArgumentException("Can't handle type " + type);

    }
}