public static Double getDouble(Object x) {
        if (x == null) {
            return null;
        }

        if (x instanceof Double) {
            return (Double) x;
        }

        if (x instanceof String) {
            String s = x.toString();
            if (s == null || s.isEmpty()) {
                return null;
            }
        }

        /*
         * This is the last and probably expensive fallback. This should be avoided by
         * only passing in Doubles, Integers, Longs or stuff that can be parsed from it's String
         * representation. You might have to build cached objects that did a safe conversion
         * once for example. There is no way around for the actual values we compare if the
         * user sent them in as non-numerical type.
         */
        return Doubles.tryParse(x.toString());
    }
@Test
    public void testGetDouble() throws Exception {
        assertEquals(null, Tools.getDouble(null));
        assertEquals(null, Tools.getDouble(""));

        assertEquals(0.0, Tools.getDouble(0), 0);
        assertEquals(1.0, Tools.getDouble(1), 0);
        assertEquals(1.42, Tools.getDouble(1.42), 0);
        assertEquals(9001.0, Tools.getDouble(9001), 0);
        assertEquals(9001.23, Tools.getDouble(9001.23), 0);

        assertEquals(1253453.0, Tools.getDouble((long) 1253453), 0);

        assertEquals(88.0, Tools.getDouble("88"), 0);
        assertEquals(1.42, Tools.getDouble("1.42"), 0);
        assertEquals(null, Tools.getDouble("lol NOT A NUMBER"));

        assertEquals(null, Tools.getDouble(new HashMap<String, String>()));

        assertEquals(42.23, Tools.getDouble(new Object() {
            @Override
            public String toString() {
                return "42.23";
            }
        }), 0);
    }