EventType.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.io.Serializable;
  19. import java.util.HashSet;
  20. import java.util.Set;

  21. /**
  22.  * <p>
  23.  * A class representing an event type.
  24.  * </p>
  25.  * <p>
  26.  * The events produced by <em>Commons Configuration</em> all have a specific type. The event type can be used to
  27.  * determine the meaning of a specific event. It also acts as filter criterion when event listeners are registered. The
  28.  * listener is then called only for events of this type or derived types. The events in this library form a natural
  29.  * hierarchy with base types and more specialized types. By specifying an appropriate event type at listener
  30.  * registration time, it can be determined on a fine-granular basis which events are propagated to the listener.
  31.  * </p>
  32.  * <p>
  33.  * Note: Users familiar with JavaFX probably recognize this approach to event handling. It allows for generic event
  34.  * listener interfaces and a natural selection of events to be processed.
  35.  * </p>
  36.  *
  37.  * @param <T> the event associated with this type
  38.  * @since 2.0
  39.  */
  40. public class EventType<T extends Event> implements Serializable {
  41.     /** Serial version UID. */
  42.     private static final long serialVersionUID = 20150416L;

  43.     /** Constant for the format used by toString(). */
  44.     private static final String FMT_TO_STRING = "%s [ %s ]";

  45.     /**
  46.      * Returns a set with all event types that are super types of the specified type. This set contains the direct and
  47.      * indirect super types and also includes the given type itself. The passed in type may be <strong>null</strong>, then an empty
  48.      * set is returned.
  49.      *
  50.      * @param eventType the event type in question
  51.      * @return a set with all super event types
  52.      */
  53.     public static Set<EventType<?>> fetchSuperEventTypes(final EventType<?> eventType) {
  54.         final Set<EventType<?>> types = new HashSet<>();
  55.         EventType<?> currentType = eventType;
  56.         while (currentType != null) {
  57.             types.add(currentType);
  58.             currentType = currentType.getSuperType();
  59.         }
  60.         return types;
  61.     }

  62.     /**
  63.      * Checks whether an event type is derived from another type. This implementation tests whether {@code baseType} is a
  64.      * direct or indirect super type of {@code derivedType}. If one of the types is <strong>null</strong>, result is <strong>false</strong>.
  65.      *
  66.      * @param derivedType the derived event type
  67.      * @param baseType the base event type
  68.      * @return <strong>true</strong> if the derived type is an instance of the base type, <strong>false</strong> otherwise
  69.      */
  70.     public static boolean isInstanceOf(final EventType<?> derivedType, final EventType<?> baseType) {
  71.         EventType<?> currentType = derivedType;
  72.         while (currentType != null) {
  73.             if (currentType == baseType) {
  74.                 return true;
  75.             }
  76.             currentType = currentType.getSuperType();
  77.         }
  78.         return false;
  79.     }

  80.     /** Stores the super type of this type. */
  81.     private final EventType<? super T> superType;

  82.     /** A name for this event type. */
  83.     private final String name;

  84.     /**
  85.      * Creates a new instance of {@code EventType} and initializes it with the super type and a type name. If no super type
  86.      * is specified, this is the root event type.
  87.      *
  88.      * @param superEventType the super event type
  89.      * @param typeName the name of this event type
  90.      */
  91.     public EventType(final EventType<? super T> superEventType, final String typeName) {
  92.         superType = superEventType;
  93.         name = typeName;
  94.     }

  95.     /**
  96.      * Gets the name of this event type. The name has no specific semantic meaning. It is just used for debugging
  97.      * purposes and also part of the string representation of this event type.
  98.      *
  99.      * @return the event type name
  100.      */
  101.     public String getName() {
  102.         return name;
  103.     }

  104.     /**
  105.      * Gets the super event type. Result is <strong>null</strong> for the root event type.
  106.      *
  107.      * @return the super event type
  108.      */
  109.     public EventType<? super T> getSuperType() {
  110.         return superType;
  111.     }

  112.     /**
  113.      * Returns a string representation for this object. This method is mainly overridden for debugging purposes. The
  114.      * returned string contains the name of this event type.
  115.      *
  116.      * @return a string for this object
  117.      */
  118.     @Override
  119.     public String toString() {
  120.         return String.format(FMT_TO_STRING, getClass().getSimpleName(), getName());
  121.     }
  122. }