This project has retired. For details please refer to its Attic page.
Event xref
View Javadoc
1   package org.apache.archiva.event;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.time.LocalDateTime;
23  import java.util.EventObject;
24  
25  /**
26   * Base class for events. Events have a type and a source.
27   * The source is the instance that raised the event.
28   *
29   * There are different event types for a given event. The types are represented in a hierarchical structure.
30   *
31   * Events can be chained, which means a event listener can catch events and rethrow them as its own event.
32   *
33   */
34  public class Event extends EventObject implements Cloneable {
35  
36      private static final long serialVersionUID = -7171846575892044990L;
37  
38      public static final EventType<Event> ANY = EventType.ROOT;
39  
40      private Event previous;
41      private final EventType<? extends Event> type;
42      private final LocalDateTime createTime;
43  
44      public Event(EventType<? extends Event> type, Object originator) {
45          super(originator);
46          this.type = type;
47          this.createTime = LocalDateTime.now();
48      }
49  
50      private Eventent" href="../../../../org/apache/archiva/event/Event.html#Event">Event(Event previous, Object originator) {
51          super(originator);
52          this.previous = previous;
53          this.type = previous.getType();
54          this.createTime = previous.getCreateTime();
55      }
56  
57      /**
58       * Returns the event type that is associated with this event instance.
59       * @return the event type
60       */
61      public EventType<? extends Event> getType() {
62          return type;
63      };
64  
65      /**
66       * Returns the time, when the event was created.
67       * @return
68       */
69      public LocalDateTime getCreateTime() {
70          return createTime;
71      }
72  
73  
74      /**
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
79       */
80      public Event copyFor(Object newSource) {
81          Event./../../../org/apache/archiva/event/Event.html#Event">Event newEvent = (Event) this.clone();
82          newEvent.previous = this;
83          newEvent.source = newSource;
84          return newEvent;
85      }
86  
87      /**
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
90       */
91      public Event getPreviousEvent() {
92          return previous;
93      }
94  
95      /**
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>
98       */
99      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 }