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.io.InvalidObjectException;
23 import java.io.ObjectStreamException;
24 import java.io.Serializable;
28 * Event types define a hierarchical structure of events. Each event is bound to a certain event type.
29 * All event types have a super type, only the root event type {@link EventType#ROOT} has no super type.
31 * Event types should be stored as static fields on the events itself.
33 * @param <T> The type class parameter allows to define the types in a type safe way and represents a event class,
34 * where the type is associated to.
36 public class EventType<T extends Event> implements Serializable {
39 public static final EventType<Event> ROOT = new EventType<>();
41 private final String name;
42 private final EventType<? super T> superType;
43 private WeakHashMap<EventType<? extends T>, Void> subTypes;
46 * Creates a type with the given name and the root type as parent.
47 * @param name the name of the new type
49 public EventType(String name) {
50 this.superType = ROOT;
55 * Creates a event type instance with the given super type and name.
57 * @param superType The super type or <code>null</code>, if this is the root type.
60 public EventType(EventType<? super T> superType, String name) {
61 if (superType==null) {
62 throw new NullPointerException("Super Type may not be null");
65 this.superType = superType;
66 superType.register(this);
70 * Creates the root type
77 public String name() {
81 public EventType<? super T> getSuperType() {
85 private void register(EventType<? extends T> subType) {
86 if (subTypes == null) {
87 subTypes = new WeakHashMap<>();
89 for (EventType<? extends T> t : subTypes.keySet()) {
90 if (((t.name == null && subType.name == null) || (t.name != null && t.name.equals(subType.name)))) {
91 throw new IllegalArgumentException("EventType \"" + subType + "\""
92 + "with parent \"" + subType.getSuperType()+"\" already exists");
95 subTypes.put(subType, null);
99 public static List<EventType<?>> fetchSuperTypes(EventType<?> type) {
100 List<EventType<?>> typeList = new ArrayList<>();
101 EventType<?> cType = type;
102 while (cType!=null) {
104 cType = cType.getSuperType();
109 public static boolean isInstanceOf(EventType<?> type, EventType<?> baseType) {
110 EventType<?> cType = type;
112 if (cType == baseType) {
115 cType = cType.getSuperType();
121 private Object writeReplace() throws ObjectStreamException {
122 Deque<String> path = new LinkedList<String>();
123 EventType<?> t = this;
125 path.addFirst(t.name);
128 return new EventTypeSerialization(new ArrayList<>(path));
131 static class EventTypeSerialization implements Serializable {
132 private static final long serialVersionUID = 1841649460281865547L;
133 private List<String> path;
135 public EventTypeSerialization(List<String> path) {
139 private Object readResolve() throws ObjectStreamException {
141 for (int i = 0; i < path.size(); ++i) {
142 String p = path.get(i);
143 if (t.subTypes != null) {
144 EventType<?> s = findSubType(t.subTypes.keySet(), p);
146 throw new InvalidObjectException("Cannot find event type \"" + p + "\" (of " + t + ")");
150 throw new InvalidObjectException("Cannot find event type \"" + p + "\" (of " + t + ")");
156 private EventType<?> findSubType(Set<EventType> subTypes, String name) {
157 for (EventType t : subTypes) {
158 if (((t.name == null && name == null) || (t.name != null && t.name.equals(name)))) {