Event.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.event;

  18. import java.util.EventObject;

  19. /**
  20.  * <p>
  21.  * The base class for all events generated by this library.
  22.  * </p>
  23.  * <p>
  24.  * The events produced by objects in this library are arranged in an inheritance hierarchy. This base class defines some
  25.  * basic properties common to all configuration events. Especially, an event has an {@link EventType} which describes
  26.  * its semantics. The event type can also be used for filtering for events or for defining event listeners on a
  27.  * fine-grained basis.
  28.  * </p>
  29.  *
  30.  * @since 2.0
  31.  */
  32. public class Event extends EventObject {

  33.     /**
  34.      * The root event type for all configuration-related events. All specific event types have this type as super direct
  35.      * (directly or indirectly).
  36.      */
  37.     public static final EventType<Event> ANY = new EventType<>(null, "ANY");

  38.     private static final long serialVersionUID = -8168310049858198944L;

  39.     /**
  40.      * Constant for the format used in toString() for a property representation.
  41.      */
  42.     private static final String FMT_PROPERTY = " %s=%s";

  43.     /**
  44.      * Constant for the initial buffer size for the generation of the string representation.
  45.      */
  46.     private static final int BUF_SIZE = 256;

  47.     /** The type of this event. */
  48.     private final EventType<? extends Event> eventType;

  49.     /**
  50.      * Creates a new instance of {@code Event} and sets basic properties.
  51.      *
  52.      * @param source the object on which the Event initially occurred (must not be <strong>null</strong>)
  53.      * @param evType the type of this event (must not be <strong>null</strong>)
  54.      * @throws IllegalArgumentException if a required parameter is null
  55.      */
  56.     public Event(final Object source, final EventType<? extends Event> evType) {
  57.         super(source);
  58.         if (evType == null) {
  59.             throw new IllegalArgumentException("Event type must not be null!");
  60.         }
  61.         eventType = evType;
  62.     }

  63.     /**
  64.      * Helper method for appending a representation for a property to the overall string representation for this object.
  65.      * This method is called by {@code toString()} for generating string fragments for the properties of this class. It can
  66.      * also be used by derived classes which extend the string representation of this base class.
  67.      *
  68.      * @param buf the target buffer
  69.      * @param property the name of the property
  70.      * @param value the property value
  71.      */
  72.     protected void appendPropertyRepresentation(final StringBuilder buf, final String property, final Object value) {
  73.         buf.append(String.format(FMT_PROPERTY, property, String.valueOf(value)));
  74.     }

  75.     /**
  76.      * Gets the type of this event.
  77.      *
  78.      * @return the event type
  79.      */
  80.     public EventType<? extends Event> getEventType() {
  81.         return eventType;
  82.     }

  83.     /**
  84.      * Returns a string representation for this object. This string contains the event class and a list of all properties.
  85.      *
  86.      * @return a string for this object
  87.      */
  88.     @Override
  89.     public String toString() {
  90.         final StringBuilder buf = new StringBuilder(BUF_SIZE);
  91.         buf.append(getClass().getSimpleName());
  92.         buf.append(" [");
  93.         appendPropertyRepresentation(buf, "source", getSource());
  94.         appendPropertyRepresentation(buf, "eventType", getEventType());
  95.         buf.append(" ]");
  96.         return buf.toString();
  97.     }
  98. }