All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xwiki.extension.event.AbstractExtensionEvent Maven / Gradle / Ivy

There is a newer version: 16.10.2
Show newest version
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.event;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.extension.ExtensionId;

/**
 * Base class for all {@link ExtensionEvent}.
 *
 * @version $Id: 8f0d5a6b233ab41acf583e115652bf864c41ec6d $
 * @since 4.0M1
 */
public abstract class AbstractExtensionEvent implements ExtensionEvent
{
    /**
     * @see #getExtensionId()
     */
    private ExtensionId extensionId;

    /**
     * @see #getNamespace()
     */
    private String namespace;

    /**
     * Required since null namespace means root namespace.
     */
    private boolean noNamespace;

    /**
     * Default constructor.
     */
    public AbstractExtensionEvent()
    {
        this.noNamespace = true;
    }

    /**
     * @param extensionId the event related extension identifier
     * @param namespace the namespace on which the event happened
     */
    protected AbstractExtensionEvent(ExtensionId extensionId, String namespace)
    {
        this.extensionId = extensionId;
        this.namespace = namespace;
        this.noNamespace = false;
    }

    // ExtensionEvent

    @Override
    public ExtensionId getExtensionId()
    {
        return this.extensionId;
    }

    @Override
    public String getNamespace()
    {
        return this.namespace;
    }

    @Override
    public boolean hasNamespace()
    {
        return !this.noNamespace;
    }

    // Event

    @Override
    public boolean matches(Object event)
    {
        return this.getClass() == event.getClass()
            && matchesExtensionId(((AbstractExtensionEvent) event).getExtensionId())
            && matchesNamespace(((AbstractExtensionEvent) event).getNamespace());
    }

    /**
     * @param extensionId the event related extension identifier
     * @return true if the passed event matches this event, false otherwise.
     */
    private boolean matchesExtensionId(ExtensionId extensionId)
    {
        return this.extensionId == null || this.extensionId.equals(extensionId)
            || (this.extensionId.getVersion() == null && this.extensionId.getId().equals(extensionId.getId()));
    }

    /**
     * @param namespace the event related namespace
     * @return true if the passed event matches this event, false otherwise.
     */
    private boolean matchesNamespace(String namespace)
    {
        return this.noNamespace || StringUtils.equals(this.namespace, namespace);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy