    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.event;
    018
    019import java.util.EventObject;
    020
    021/**
    022 * <p>
    023 * The base class for all events generated by this library.
    024 * </p>
    025 * <p>
    026 * The events produced by objects in this library are arranged in an inheritance hierarchy. This base class defines some
    027 * basic properties common to all configuration events. Especially, an event has an {@link EventType} which describes
    028 * its semantics. The event type can also be used for filtering for events or for defining event listeners on a
    029 * fine-grained basis.
    030 * </p>
    031 *
    032 * @since 2.0
    033 */
    034public class Event extends EventObject {
    035
    036    /**
    037     * The root event type for all configuration-related events. All specific event types have this type as super direct
    038     * (directly or indirectly).
    039     */
    040    public static final EventType<Event> ANY = new EventType<>(null, "ANY");
    041
    042    private static final long serialVersionUID = -8168310049858198944L;
    043
    044    /**
    045     * Constant for the format used in toString() for a property representation.
    046     */
    047    private static final String FMT_PROPERTY = " %s=%s";
    048
    049    /**
    050     * Constant for the initial buffer size for the generation of the string representation.
    051     */
    052    private static final int BUF_SIZE = 256;
    053
    054    /** The type of this event. */
    055    private final EventType<? extends Event> eventType;
    056
    057    /**
    058     * Creates a new instance of {@code Event} and sets basic properties.
    059     *
    060     * @param source the object on which the Event initially occurred (must not be <b>null</b>)
    061     * @param evType the type of this event (must not be <b>null</b>)
    062     * @throws IllegalArgumentException if a required parameter is null
    063     */
    064    public Event(final Object source, final EventType<? extends Event> evType) {
    065        super(source);
    066        if (evType == null) {
    067            throw new IllegalArgumentException("Event type must not be null!");
    068        }
    069        eventType = evType;
    070    }
    071
    072    /**
    073     * Gets the type of this event.
    074     *
    075     * @return the event type
    076     */
    077    public EventType<? extends Event> getEventType() {
    078        return eventType;
    079    }
    080
    081    /**
    082     * Returns a string representation for this object. This string contains the event class and a list of all properties.
    083     *
    084     * @return a string for this object
    085     */
    086    @Override
    087    public String toString() {
    088        final StringBuilder buf = new StringBuilder(BUF_SIZE);
    089        buf.append(getClass().getSimpleName());
    090        buf.append(" [");
    091        appendPropertyRepresentation(buf, "source", getSource());
    092        appendPropertyRepresentation(buf, "eventType", getEventType());
    093        buf.append(" ]");
    094        return buf.toString();
    095    }
    096
    097    /**
    098     * Helper method for appending a representation for a property to the overall string representation for this object.
    099     * This method is called by {@code toString()} for generating string fragments for the properties of this class. It can
    100     * also be used by derived classes which extend the string representation of this base class.
    101     *
    102     * @param buf the target buffer
    103     * @param property the name of the property
    104     * @param value the property value
    105     */
    106    protected void appendPropertyRepresentation(final StringBuilder buf, final String property, final Object value) {
    107        buf.append(String.format(FMT_PROPERTY, property, String.valueOf(value)));
    108    }
    109}