You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/05/19 21:52:55 UTC

svn commit: r1596013 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/event/Event.java test/java/org/apache/commons/configuration/event/TestEvent.java

Author: oheger
Date: Mon May 19 19:52:55 2014
New Revision: 1596013

URL: http://svn.apache.org/r1596013
Log:
Added a new base class for all events generated by this library.

This class already defines some properties common to all derived events.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/Event.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEvent.java

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/Event.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/Event.java?rev=1596013&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/Event.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/Event.java Mon May 19 19:52:55 2014
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.event;
+
+import java.util.EventObject;
+
+/**
+ * <p>
+ * The base class for all events generated by this library.
+ * </p>
+ * <p>
+ * The events produced by objects in this library are arranged in an inheritance
+ * hierarchy. This base class defines some basic properties common to all
+ * configuration events. Especially, an event has an {@link EventType} which
+ * describes its semantics. The event type can also be used for filtering for
+ * events or for defining event listeners on a fine-grained basis.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class Event extends EventObject
+{
+    /**
+     * The root event type for all configuration-related events. All specific
+     * event types have this type as super direct (directly or indirectly).
+     */
+    public static final EventType<Event> ANY =
+            new EventType<Event>(null, "ANY");
+
+    /**
+     * Constant for the format used in toString() for a property representation.
+     */
+    private static final String FMT_PROPERTY = " %s=%s";
+
+    /**
+     * Constant for the initial buffer size for the generation of the string
+     * representation.
+     */
+    private static final int BUF_SIZE = 256;
+
+    /** The type of this event. */
+    private final EventType<? extends Event> eventType;
+
+    /**
+     * Creates a new instance of {@code Event} and sets basic properties.
+     *
+     * @param source the object on which the Event initially occurred (must not
+     *        be <b>null</b>)
+     * @param evType the type of this event (must not be <b>null</b>)
+     * @throws IllegalArgumentException if a required parameter is null
+     */
+    public Event(Object source, EventType<? extends Event> evType)
+    {
+        super(source);
+        if (evType == null)
+        {
+            throw new IllegalArgumentException("Event type must not be null!");
+        }
+        eventType = evType;
+    }
+
+    /**
+     * Returns the type of this event.
+     *
+     * @return the event type
+     */
+    public EventType<? extends Event> getEventType()
+    {
+        return eventType;
+    }
+
+    /**
+     * Returns a string representation for this object. This string contains the
+     * event class and a list of all properties.
+     *
+     * @return a string for this object
+     */
+    @Override
+    public String toString()
+    {
+        StringBuilder buf = new StringBuilder(BUF_SIZE);
+        buf.append(getClass().getSimpleName());
+        buf.append(" [");
+        appendPropertyRepresentation(buf, "source", getSource());
+        appendPropertyRepresentation(buf, "eventType", getEventType());
+        buf.append(" ]");
+        return buf.toString();
+    }
+
+    /**
+     * Helper method for appending a representation for a property to the
+     * overall string representation for this object. This method is called by
+     * {@code toString()} for generating string fragments for the properties of
+     * this class. It can also be used by derived classes which extend the
+     * string representation of this base class.
+     *
+     * @param buf the target buffer
+     * @param property the name of the property
+     * @param value the property value
+     */
+    protected void appendPropertyRepresentation(StringBuilder buf,
+            String property, Object value)
+    {
+        buf.append(String.format(FMT_PROPERTY, property, String.valueOf(value)));
+    }
+}

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEvent.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEvent.java?rev=1596013&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEvent.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEvent.java Mon May 19 19:52:55 2014
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.event;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Test class for {@code Event}.
+ *
+ * @version $Id$
+ */
+public class TestEvent
+{
+    /**
+     * Tries to create an instance without a source.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testInitNoSource()
+    {
+        new Event(null, Event.ANY);
+    }
+
+    /**
+     * Tries to create an instance without a type.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testInitNoType()
+    {
+        new Event(this, null);
+    }
+
+    /**
+     * Tests the string representation.
+     */
+    @Test
+    public void testToString()
+    {
+        Event event = new Event(this, Event.ANY);
+        String s = event.toString();
+        assertEquals("Wrong string representation", "Event [ source=" + this
+                + " eventType=" + Event.ANY + " ]", s);
+    }
+}