    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 *     https://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 <strong>null</strong>)
    061     * @param evType the type of this event (must not be <strong>null</strong>)
    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     * Helper method for appending a representation for a property to the overall string representation for this object.
    074     * This method is called by {@code toString()} for generating string fragments for the properties of this class. It can
    075     * also be used by derived classes which extend the string representation of this base class.
    076     *
    077     * @param buf the target buffer
    078     * @param property the name of the property
    079     * @param value the property value
    080     */
    081    protected void appendPropertyRepresentation(final StringBuilder buf, final String property, final Object value) {
    082        buf.append(String.format(FMT_PROPERTY, property, String.valueOf(value)));
    083    }
    084
    085    /**
    086     * Gets the type of this event.
    087     *
    088     * @return the event type
    089     */
    090    public EventType<? extends Event> getEventType() {
    091        return eventType;
    092    }
    093
    094    /**
    095     * Returns a string representation for this object. This string contains the event class and a list of all properties.
    096     *
    097     * @return a string for this object
    098     */
    099    @Override
    100    public String toString() {
    101        final StringBuilder buf = new StringBuilder(BUF_SIZE);
    102        buf.append(getClass().getSimpleName());
    103        buf.append(" [");
    104        appendPropertyRepresentation(buf, "source", getSource());
    105        appendPropertyRepresentation(buf, "eventType", getEventType());
    106        buf.append(" ]");
    107        return buf.toString();
    108    }
    109}