Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5161e465029b650681fda90fdd94cb1e538326da (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.validation.internal.el.operators;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.jst.jsf.common.internal.types.ValueType;
import org.eclipse.jst.jsf.validation.internal.el.diagnostics.DiagnosticFactory;
import org.eclipse.jst.jsp.core.internal.java.jspel.JSPELParserConstants;
import org.eclipse.jst.jsp.core.internal.java.jspel.Token;

/**
 * Encapsulates an EL unary operator
 * @author cbateman
 *
 */
public abstract class UnaryOperator 
{
    /**
     * The common factory used to construct diagnostics
     */
    protected final DiagnosticFactory     _diagnosticFactory;
    
    /**
     * @param token
     * @return true if the token is a unary operator
     */
    public static boolean isUnaryOperator(Token token)
    {
        return (token.kind == JSPELParserConstants.MINUS)
                  || (token.kind == JSPELParserConstants.NOT1)
                  || (token.kind == JSPELParserConstants.NOT2)
                  || (token.kind == JSPELParserConstants.EMPTY);
    }

    /**
     * @param token 
     * @param diagnosticFactory 
     * @return a new UnaryOperator instance matching token 
     */
    public static UnaryOperator createUnaryOperator(Token token, DiagnosticFactory diagnosticFactory)
    {
        if (!isUnaryOperator(token))
        {
            throw new IllegalArgumentException("token must be a unary operator"); //$NON-NLS-1$
        }
        
        switch(token.kind)
        {
            case JSPELParserConstants.MINUS:
                return new MinusUnaryOperator(diagnosticFactory);

            case JSPELParserConstants.NOT1:
            case JSPELParserConstants.NOT2:
                return new NotUnaryOperator(diagnosticFactory);
                
            case JSPELParserConstants.EMPTY:
                return new EmptyUnaryOperator(diagnosticFactory);
        }

        // should never get here because all four ops are covered
        throw new AssertionError();
    }

    /**
     * Constructor
     */
    UnaryOperator(DiagnosticFactory diagnosticFactory) 
    {
        /* no construction or sub-classing outside package*/
        _diagnosticFactory = diagnosticFactory;
    }
    
    /**
     * If ValueType is a literal and the operation can be performed, then
     * the return must be a new LiteralType transformed using this operator.
     * 
     * If ValueType is not a literal and the operaton can be performed, then
     * the return is a new ValueType transformed per the rules of the operator
     * (i.e. if it is a string type and the operator is "!", then the string
     * must be coerced to a boolean and this is what will be returned)
     * 
     * If the operation cannot be performed on ValueType, return null
     * 
     * @param type
     * @return a new value type after the operation is performed
     */
    public abstract ValueType performOperation(ValueType type);
    
    
    /**
     * @param type
     * @return a Diagnostic interpreting whether it is valid to perform the
     * operation on this type
     */
    public abstract Diagnostic validate(ValueType type);
}

Back to the top