/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.net.messaging.expression;

import com.icesoft.net.messaging.Message;

import java.util.logging.Level;
import java.util.logging.Logger;

public class NotEqual
extends ComparisonOperator<Operand>
implements Expression, Operand {
    private static final Logger LOGGER = Logger.getLogger(NotEqual.class.getName());

    private static final int IDENTIFIER_INDEX = 0;
    private static final int LITERAL_INDEX = 1;

    public NotEqual(final Identifier identifier, final Literal literal)
    throws IllegalArgumentException {
        // throws IllegalArgumentException
        super(identifier, literal);
    }

    public boolean evaluate(final Message message)
    throws IllegalArgumentException {
        if (message == null) {
            throw new IllegalArgumentException("Illegal value of argument 'message': null");
        }
        boolean _result;
        String _identifier = getIdentifier().getValue();
        if (message.propertyExists(_identifier)) {
            Literal _literal = getLiteral();
            Object _value = message.getObjectProperty(_identifier);
            _result =
                _value instanceof String &&
                _literal instanceof StringLiteral &&
                !((String)_value).equalsIgnoreCase(((StringLiteral)_literal).getValue());
        } else {
            _result = false;
        }
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.log(Level.FINEST, this + " evaluates to " + _result);
        }
        return _result;
    }

    public Identifier getIdentifier() {
        return (Identifier)getOperands()[IDENTIFIER_INDEX];
    }

    public Literal getLiteral() {
        return (Literal)getOperands()[LITERAL_INDEX];
    }

    public String toString() {
        return getIdentifier() + " <> " + getLiteral();
    }
}
