001/*
002// $Id: QueryEvent.java 482 2012-01-05 23:27:27Z jhyde $
003//
004// Licensed to Julian Hyde under one or more contributor license
005// agreements. See the NOTICE file distributed with this work for
006// additional information regarding copyright ownership.
007//
008// Julian Hyde licenses this file to you under the Apache License,
009// Version 2.0 (the "License"); you may not use this file except in
010// compliance with the License. You may obtain a copy of the License at:
011//
012// http://www.apache.org/licenses/LICENSE-2.0
013//
014// Unless required by applicable law or agreed to in writing, software
015// distributed under the License is distributed on an "AS IS" BASIS,
016// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017// See the License for the specific language governing permissions and
018// limitations under the License.
019*/
020package org.olap4j.query;
021
022import java.util.*;
023
024/**
025 * Describes which changes were performed to the query model.
026 *
027 * @author Luc Boudreau
028 * @version $Id: QueryEvent.java 482 2012-01-05 23:27:27Z jhyde $
029 */
030public final class QueryEvent {
031
032    /**
033     * Describes the nature of the event.
034     */
035    public static enum Type {
036        /**
037         * Event where one or more children of a QueryNode were removed.
038         */
039        CHILDREN_REMOVED,
040        /**
041         * Event where one or more nodes were added as children of a QueryNode.
042         */
043        CHILDREN_ADDED,
044        /**
045         * Event where a Selection object operator was changed.
046         */
047        SELECTION_CHANGED
048    }
049
050    private final QueryNode source;
051    private final QueryEvent.Type operation;
052    private final Map<Integer, QueryNode> children;
053
054    /**
055     * Creates a QueryEvent with a single child.
056     *
057     * @param operation Even type
058     * @param source Query node that generated this event
059     * @param child Child node
060     */
061    QueryEvent(
062        QueryEvent.Type operation,
063        QueryNode source,
064        QueryNode child,
065        int index)
066    {
067        this.children = Collections.singletonMap(index, child);
068        this.source = source;
069        this.operation = operation;
070    }
071
072    /**
073     * Creates a QueryEvent with multiple children.
074     *
075     * @param operation Even type
076     * @param source Query node that generated this event
077     * @param children Child nodes and their indexes within the parent
078     */
079    QueryEvent(
080        QueryEvent.Type operation,
081        QueryNode source,
082        Map<Integer, QueryNode> children)
083    {
084        // copy the map, and make immutable
085        this.children =
086            Collections.unmodifiableMap(
087                new HashMap<Integer, QueryNode>(children));
088        this.source = source;
089        this.operation = operation;
090    }
091
092    /**
093     * Creates a QueryEvent with no children.
094     *
095     * @param operation Even type
096     * @param source Query node that generated this event
097     */
098    QueryEvent(
099        QueryEvent.Type operation,
100        QueryNode source)
101    {
102        this.children = null;
103        this.source = source;
104        this.operation = operation;
105    }
106
107    /**
108     * Returns the object that generated this event.
109     */
110    public QueryNode getSource() {
111        return source;
112    }
113
114    /**
115     * Returns the event type.
116     */
117    // REVIEW: consider renaming to 'getEventType', or rename enum Type to
118    // Operation.
119    public QueryEvent.Type getOperation() {
120        return operation;
121    }
122
123    /**
124     * Returns a map of objects affected by the event and
125     * their index in the list of the source children.
126     *
127     * <p>If the event is of type {@link QueryEvent.Type#SELECTION_CHANGED},
128     * this method will return null because the source object was affected
129     * and not the children.
130     */
131    // REVIEW: 'children' is already plural. Consider renaming to 'getChildren'
132    // or 'getChildNodes'.
133    public Map<Integer, QueryNode> getChildrens() {
134        return children;
135    }
136}
137
138// End QueryEvent.java