Skip to main content
summaryrefslogtreecommitdiffstats
blob: 029acabcd377a3ecbd356a4abe1fa96b165808c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*********************************************************************
 * Copyright (c) 2004, 2007 Boeing
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Boeing - initial API and implementation
 **********************************************************************/

package org.eclipse.osee.framework.lifecycle;

/**
 * @author Roberto E. Escobar
 * @author Jeff C. Phillips
 */
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Conditions;

public class LifecycleServiceImpl implements ILifecycleService {

   private final HashCollection<AbstractLifecycleVisitor.Type<?>, LifecycleHandler> handlersByType =
      new HashCollection<>();

   @Override
   public Collection<AbstractLifecycleVisitor.Type<?>> getHandlerTypes() {
      return handlersByType.keySet();
   }

   @SuppressWarnings("unchecked")
   private <H> ArrayList<H> get(AbstractLifecycleVisitor.Type<H> type) {
      return (ArrayList<H>) handlersByType.getValues(type);
   }

   @Override
   public <H extends LifecycleHandler> IStatus dispatch(IProgressMonitor monitor, AbstractLifecycleVisitor<H> visitor, String sourceId) {
      AbstractLifecycleVisitor.Type<H> type = visitor.getAssociatedType();
      IStatus status = null;
      Collection<H> handlers = get(type);
      if (handlers != null) {
         for (H handler : handlers) {
            status = visitor.dispatch(monitor, handler, sourceId);
            if (!status.isOK()) {
               break;
            }
         }
      } else {
         status = new Status(IStatus.ERROR, getClass().getName(), String.format("Error handler [%s] not found.", type));
      }
      return status;
   }

   @Override
   public int getHandlerCount(AbstractLifecycleVisitor.Type<?> type) {
      ArrayList<?> handlers = get(type);
      return handlers == null ? 0 : handlers.size();
   }

   @Override
   public <H extends LifecycleHandler> void addHandler(AbstractLifecycleVisitor.Type<H> type, final H handler) {
      Conditions.checkNotNull(type, "handler type");
      Conditions.checkNotNull(handler, "handler");
      handlersByType.put(type, handler);
   }

   @Override
   public <H extends LifecycleHandler> void removeHandler(AbstractLifecycleVisitor.Type<H> type, final H handler) {
      Conditions.checkNotNull(type, "handler type");
      Conditions.checkNotNull(handler, "handler");
      handlersByType.removeValue(type, handler);
   }
}

Back to the top