This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.event;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.time.LocalDateTime;
023import java.util.EventObject;
024
025/**
026 * Base class for events. Events have a type and a source.
027 * The source is the instance that raised the event.
028 *
029 * There are different event types for a given event. The types are represented in a hierarchical structure.
030 *
031 * Events can be chained, which means a event listener can catch events and rethrow them as its own event.
032 *
033 */
034public class Event extends EventObject implements Cloneable {
035
036    private static final long serialVersionUID = -7171846575892044990L;
037
038    public static final EventType<Event> ANY = EventType.ROOT;
039
040    private Event previous;
041    private final EventType<? extends Event> type;
042    private final LocalDateTime createTime;
043
044    public Event(EventType<? extends Event> type, Object originator) {
045        super(originator);
046        this.type = type;
047        this.createTime = LocalDateTime.now();
048    }
049
050    private Event(Event previous, Object originator) {
051        super(originator);
052        this.previous = previous;
053        this.type = previous.getType();
054        this.createTime = previous.getCreateTime();
055    }
056
057    /**
058     * Returns the event type that is associated with this event instance.
059     * @return the event type
060     */
061    public EventType<? extends Event> getType() {
062        return type;
063    };
064
065    /**
066     * Returns the time, when the event was created.
067     * @return
068     */
069    public LocalDateTime getCreateTime() {
070        return createTime;
071    }
072
073
074    /**
075     * Recreates the event with the given instance as the new source. The
076     * current source is stored in the previous event.
077     * @param newSource The new source
078     * @return a new event instance, where <code>this</code> is stored as previous event
079     */
080    public Event copyFor(Object newSource) {
081        Event newEvent = (Event) this.clone();
082        newEvent.previous = this;
083        newEvent.source = newSource;
084        return newEvent;
085    }
086
087    /**
088     * Returns the previous event or <code>null</code>, if this is a root event.
089     * @return the previous event or <code>null</code>, if it does not exist
090     */
091    public Event getPreviousEvent() {
092        return previous;
093    }
094
095    /**
096     * Returns <code>true</code>, if the event has a previous event.
097     * @return <code>true</code>, if this has a previous event, otherwise <code>false</code>
098     */
099    public boolean hasPreviousEvent() {
100        return previous!=null;
101    }
102
103    @Override
104    protected Object clone() {
105        try {
106            return super.clone();
107        } catch (CloneNotSupportedException e) {
108            // this should not happen
109            throw new RuntimeException("Event is not clonable");
110        }
111    }
112}