aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/textserialize/item/TypeCheck.java
blob: 20848d4662cf96a5749a07a04048d9dce23efd4f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.textserialize.item;

import com.yahoo.protect.Validator;

/**
 * @author Tony Vaagenes
 */
public class TypeCheck {

    public static void ensureInstanceOf(Object object, Class<?> c) {
        Validator.ensureInstanceOf(expectationString(c.getName(), object.getClass().getSimpleName()),
                object, c);
    }

    public static void ensureInteger(Object value) {
        ensureInstanceOf(value, Number.class);
        Number number = (Number)value;

        int intValue = number.intValue();
        if (intValue != number.doubleValue())
            throw new IllegalArgumentException("Invalid integer '" + number + "'");
    }

    private static String expectationString(String expected, String got) {
        return "Expected " + expected + ", but got " + got;
    }

}