Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;

public class Main {
    public static float asFloat(Object value) {
        return asFloat(value, 0f);
    }

    public static float asFloat(Object value, float def) {
        if (value instanceof Number) {
            return ((Number) value).floatValue();
        } else {
            try {
                return Float.valueOf(value.toString());
            } catch (NumberFormatException ex) {
                return def;
            }
        }
    }

    public static float asFloat(Object node, String key) {
        return asFloat(node, key, 0f);
    }

    public static float asFloat(Object node, String key, float def) {
        Object value;

        if ((node instanceof HashMap<?, ?>) && ((value = ((HashMap<?, ?>) node).get(key)) != null)) {
            if (value instanceof Number) {
                return ((Number) value).floatValue();
            } else {
                try {
                    return Float.valueOf(value.toString());
                } catch (NumberFormatException ex) {
                    return def;
                }
            }
        } else {
            return def;
        }
    }
}