Package com.googlecode.aviator.runtime.type

Source Code of com.googlecode.aviator.runtime.type.AviatorBoolean

/**
*  Copyright (C) 2010 dennis zhuang (killme2008@gmail.com)
*
*  This library is free software; you can redistribute it and/or modify
*  it under the terms of the GNU Lesser General Public License as published
*  by the Free Software Foundation; either version 2.1 of the License, or
*  (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
**/
package com.googlecode.aviator.runtime.type;

import java.util.Map;

import com.googlecode.aviator.exception.ExpressionRuntimeException;


/**
* Aviator boolean type
*
* @author dennis
*
*/
public class AviatorBoolean extends AviatorObject {

    Boolean value;

    public static final AviatorBoolean TRUE = new AviatorBoolean(Boolean.TRUE);

    public static final AviatorBoolean FALSE = new AviatorBoolean(Boolean.FALSE);


    @Override
    public AviatorObject not(Map<String, Object> env) {
        return this.value.booleanValue() ? FALSE : TRUE;
    }


    @Override
    public final boolean booleanValue(Map<String, Object> env) {
        return value.booleanValue();
    }


    @Override
    public AviatorObject add(AviatorObject other, Map<String, Object> env) {
        switch (other.getAviatorType()) {
        case String:
            return new AviatorString(this.value.toString() + ((AviatorString) other).lexeme);
        case JavaType:
            AviatorJavaType javaType = (AviatorJavaType) other;
            final Object otherJavaValue = javaType.getValue(env);
            if (otherJavaValue instanceof String || otherJavaValue instanceof Character) {
                return new AviatorString(this.value.toString() + otherJavaValue.toString());
            }
            else {
                return super.add(other, env);
            }
        default:
            return super.add(other, env);
        }

    }


    @Override
    public AviatorType getAviatorType() {
        return AviatorType.Boolean;
    }


    @Override
    public final Object getValue(Map<String, Object> env) {
        return this.value;
    }


    private AviatorBoolean(Boolean value) {
        super();
        this.value = value;
    }


    public static AviatorBoolean valueOf(boolean b) {
        return b ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
    }


    @Override
    public int compare(AviatorObject other, Map<String, Object> env) {
        switch (other.getAviatorType()) {
        case Boolean:
            AviatorBoolean otherBoolean = (AviatorBoolean) other;
            return this.value.compareTo(otherBoolean.value);
        case JavaType:
            AviatorJavaType javaType = (AviatorJavaType) other;
            final Object otherValue = javaType.getValue(env);
            if (otherValue == null) {
                return 1;
            }
            if (otherValue instanceof Boolean) {
                return this.value.compareTo((Boolean) otherValue);
            }
            else {
                throw new ExpressionRuntimeException("Could not compare " + desc(env) + " with " + other.desc(env));
            }
        case Nil:
            return 1;
        default:
            throw new ExpressionRuntimeException("Could not compare " + this.desc(env) + " with " + other.desc(env));
        }

    }

}
TOP

Related Classes of com.googlecode.aviator.runtime.type.AviatorBoolean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.