001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.main;
029
030import java.util.HashMap;
031import java.util.Map;
032
033/**
034 * Event class for OpenCms for system wide events that are thrown by various
035 * operations (e.g. publishing) and can be caught and processed by
036 * classes that implement the {@link I_CmsEventListener} interface.<p>
037 *
038 * @since 6.0.0
039 *
040 * @see I_CmsEventListener
041 */
042public class CmsEvent {
043
044    /** The event data associated with this event. */
045    private Map<String, Object> m_data;
046
047    /** The event type this instance represents. */
048    private Integer m_type;
049
050    /**
051     * Construct a new CmsEvent with the specified parameters.<p>
052     *
053     * The event data <code>Map</code> provides a facility to
054     * pass objects with the event that contain information about
055     * the event environment. For example, if the event is of type
056     * {@link I_CmsEventListener#EVENT_LOGIN_USER} the Map contains
057     * a single object with the key <code>"data"</code> and a value
058     * that is the OpenCms user object that represents the user that just logged in.<p>
059     *
060     * @param type event type
061     * @param data event data
062     *
063     * @see I_CmsEventListener
064     */
065    public CmsEvent(int type, Map<String, Object> data) {
066
067        m_type = Integer.valueOf(type);
068        m_data = data;
069        if (m_data == null) {
070            m_data = new HashMap<String, Object>();
071        }
072    }
073
074    /**
075     * @see java.lang.Object#equals(java.lang.Object)
076     */
077    @Override
078    public boolean equals(Object obj) {
079
080        if (obj == this) {
081            return true;
082        }
083        if (obj instanceof CmsEvent) {
084            return m_type.equals(((CmsEvent)obj).getTypeInteger());
085        }
086        return false;
087    }
088
089    /**
090     * Provides access to the event data that was passed with this event.<p>
091     *
092     * @return the event data of this event
093     */
094    public Map<String, Object> getData() {
095
096        return m_data;
097    }
098
099    /**
100     * Provides access to the event type that was passed with this event.<p>
101     *
102     * Event types of the core OpenCms classes are defined in {@link I_CmsEventListener}.
103     * For your extensions, you should define them in a central class
104     * or interface as public member variables. Make sure the integer values
105     * do not conflict with the values from the core classes.<p>
106     *
107     * @return the event type of this event
108     *
109     * @see I_CmsEventListener
110     */
111    public int getType() {
112
113        return m_type.intValue();
114    }
115
116    /**
117     * Provides access to the event type as Integer.<p>
118     *
119     * @return the event type of this event as Integer
120     */
121    public Integer getTypeInteger() {
122
123        return m_type;
124    }
125
126    /**
127     * @see java.lang.Object#hashCode()
128     */
129    @Override
130    public int hashCode() {
131
132        return m_type.hashCode();
133    }
134
135    /**
136     * Return a String representation of this CmsEvent.<p>
137     *
138     * @return a String representation of this event
139     */
140    @Override
141    public String toString() {
142
143        return "CmsEvent['" + m_type + "']";
144    }
145}