/*
 * 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;

import com.icesoft.net.messaging.expression.Expression;

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

/**
 * <p>
 *   The MessageSelector class is a convenience class for building message selectors as defined by the Java Message
 *   Service specification. Even though it is specified by JMS, the message selector mechanism seems abstract enough to
 *   be used for other message services.
 * </p>
 * <p>
 *   Each MessageSelector contains an Expression construct that represents the actual message selector expression as
 *   defined by JMS. The MessageSelector can determine if a particular Message matches the contained expression by using
 *   the <code>matches(Message)</code> method.
 * </p>
 *
 * @see        Message
 * @see        Expression
 */
public class MessageSelector {
    private static final Logger LOGGER = Logger.getLogger(MessageSelector.class.getName());

    private Expression expression;

    public MessageSelector(final Expression expression)
    throws IllegalArgumentException {
        if (expression == null) {
            throw new IllegalArgumentException("expression is null");
        }
        this.expression = expression;
    }

    /**
     * <p>
     *   Gets the Expression of this MessageSelector.
     * </p>
     *
     * @return     the Expression.
     */
    public Expression getExpression() {
        return expression;
    }

    /**
     * <p>
     *   Determines if the specified <code>message</code> matches this MessageSelector's expression.
     * </p>
     *
     * @param      message
     *                 the Message to be tested.
     *
     * @return     <code>true</code> if there is a match, <code>false</code> if not.
     */
    public boolean matches(final Message message) {
        return expression.evaluate(message);
    }

    public String toString() {
        return expression.toString();
    }
}
