1 package org.apache.archiva.repository.events;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.time.LocalDateTime;
23 import java.util.EventObject;
26 * Base class for events. Events have a type and a source.
27 * The source is the instance that raised the event.
29 * There are different event types for a given event. The types are represented in a hierarchical structure.
31 * Events can be chained, which means a event listener can catch events and rethrow them as its own event.
34 public class Event extends EventObject implements Cloneable {
36 private static final long serialVersionUID = -7171846575892044990L;
38 public static final EventType<Event> ANY = EventType.ROOT;
40 private Event previous;
41 private final EventType<? extends Event> type;
42 private final LocalDateTime createTime;
44 public Event(EventType<? extends Event> type, Object originator) {
47 this.createTime = LocalDateTime.now();
50 private Event(Event previous, Object originator) {
52 this.previous = previous;
53 this.type = previous.getType();
54 this.createTime = previous.getCreateTime();
58 * Returns the event type that is associated with this event instance.
59 * @return the event type
61 public EventType<? extends Event> getType() {
66 * Returns the time, when the event was created.
69 public LocalDateTime getCreateTime() {
75 * Recreates the event with the given instance as the new source. The
76 * current source is stored in the previous event.
77 * @param newSource The new source
78 * @return a new event instance, where <code>this</code> is stored as previous event
80 public Event copyFor(Object newSource) {
81 Event newEvent = (Event) this.clone();
82 newEvent.previous = this;
83 newEvent.source = newSource;
88 * Returns the previous event or <code>null</code>, if this is a root event.
89 * @return the previous event or <code>null</code>, if it does not exist
91 public Event getPreviousEvent() {
96 * Returns <code>true</code>, if the event has a previous event.
97 * @return <code>true</code>, if this has a previous event, otherwise <code>false</code>
99 public boolean hasPreviousEvent() {
100 return previous!=null;
104 protected Object clone() {
106 return super.clone();
107 } catch (CloneNotSupportedException e) {
108 // this should not happen
109 throw new RuntimeException("Event is not clonable");