001    /*
002    // $Id: $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2009-2009 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package org.olap4j.query;
011    
012    import java.util.*;
013    
014    /**
015     * Describes which changes were performed to the query model.
016     *
017     * @author Luc Boudreau
018     * @version $Id: $
019     */
020    public final class QueryEvent {
021    
022        /**
023         * Describes the nature of the event.
024         */
025        public static enum Type {
026            /**
027             * Event where one or more children of a QueryNode were removed.
028             */
029            CHILDREN_REMOVED,
030            /**
031             * Event where one or more nodes were added as children of a QueryNode.
032             */
033            CHILDREN_ADDED,
034            /**
035             * Event where a Selection object operator was changed.
036             */
037            SELECTION_CHANGED
038        }
039    
040        private final QueryNode source;
041        private final QueryEvent.Type operation;
042        private final Map<Integer, QueryNode> children;
043    
044        /**
045         * Creates a QueryEvent with a single child.
046         *
047         * @param operation Even type
048         * @param source Query node that generated this event
049         * @param child Child node
050         */
051        QueryEvent(
052            QueryEvent.Type operation,
053            QueryNode source,
054            QueryNode child,
055            int index)
056        {
057            this.children = Collections.singletonMap(index, child);
058            this.source = source;
059            this.operation = operation;
060        }
061    
062        /**
063         * Creates a QueryEvent with multiple children.
064         *
065         * @param operation Even type
066         * @param source Query node that generated this event
067         * @param children Child nodes and their indexes within the parent
068         */
069        QueryEvent(
070            QueryEvent.Type operation,
071            QueryNode source,
072            Map<Integer, QueryNode> children)
073        {
074            // copy the map, and make immutable
075            this.children =
076                Collections.unmodifiableMap(
077                    new HashMap<Integer, QueryNode>(children));
078            this.source = source;
079            this.operation = operation;
080        }
081    
082        /**
083         * Creates a QueryEvent with no children.
084         *
085         * @param operation Even type
086         * @param source Query node that generated this event
087         */
088        QueryEvent(
089            QueryEvent.Type operation,
090            QueryNode source)
091        {
092            this.children = null;
093            this.source = source;
094            this.operation = operation;
095        }
096    
097        /**
098         * Returns the object that generated this event.
099         */
100        public QueryNode getSource() {
101            return source;
102        }
103    
104        /**
105         * Returns the event type.
106         */
107        // REVIEW: consider renaming to 'getEventType', or rename enum Type to
108        // Operation.
109        public QueryEvent.Type getOperation() {
110            return operation;
111        }
112    
113        /**
114         * Returns a map of objects affected by the event and
115         * their index in the list of the source children.
116         *
117         * <p>If the event is of type {@link QueryEvent.Type#SELECTION_CHANGED},
118         * this method will return null because the source object was affected
119         * and not the children.
120         */
121        // REVIEW: 'children' is already plural. Consider renaming to 'getChildren'
122        // or 'getChildNodes'.
123        public Map<Integer, QueryNode> getChildrens() {
124            return children;
125        }
126    }
127    
128    // End QueryEvent.java