Skip to main content
summaryrefslogtreecommitdiffstats
blob: 703e2dfc0b37cbc40b4983e1c391c21a6135b0d6 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
 * Copyright (c) 2006, 2007 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.common.internal.types;


/**
 * Common super-type for Float and Integer literals
 * @author cbateman
 *
 */
public abstract class NumericTypeLiteral extends LiteralType 
{
    /**
     * @param signature
     */
    protected NumericTypeLiteral(String signature)
    {
        super(signature);
    }
    
    /**
     * @return the boxed form of the numeric literal value
     */
    protected abstract Number getBoxedValue();
    
    public Number coerceToNumber(Class T) throws TypeCoercionException 
    {
        Number boxedLiteralValue = getBoxedValue();
        
        if (T == Double.class || T == Double.TYPE)
        {
            return new Double(boxedLiteralValue.doubleValue());
        }
        else if (T == Float.class || T == Float.TYPE)
        {
            return new Float(boxedLiteralValue.floatValue());
        }
        else if (T == Long.class || T == Long.TYPE)
        {
            return boxedLiteralValue;
        }
        else if (T == Integer.class || T == Integer.TYPE)
        {
            return Integer.valueOf(boxedLiteralValue.intValue());
        }
        else if (T == Short.class || T == Short.TYPE)
        {
            return Short.valueOf(boxedLiteralValue.shortValue());
        }
        else if (T == Byte.class || T == Byte.TYPE)
        {
            return Byte.valueOf(boxedLiteralValue.byteValue());
        }
        else
        {
            return null;
        }
    }

    public String getLiteralValue() 
    {
        return getBoxedValue().toString();
    }

    public Object getLiteralValueRaw() 
    {
        return getBoxedValue();
    }

    /* (non-Javadoc)
     * @see org.eclipse.jst.jsf.core.internal.types.LiteralType#coerceToBoolean()
     */
    public Boolean coerceToBoolean() throws TypeCoercionException {
        // JSP.2.8.5 does not provide for number -> boolean coercion
        throw new TypeCoercionException("Cannot coerce number to boolean"); //$NON-NLS-1$
    }

    
}

Back to the top