View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   */
20  package org.apache.log4j.chainsaw;
21  
22  import javax.swing.event.EventListenerList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  
29  /**
30   * An implementation of LoggerNameModel which can be used as a delegate
31   *
32   * @author Paul Smith <psmith@apache.org>
33   */
34  public class LoggerNameModelSupport implements LoggerNameModel {
35  
36      private Set<String> loggerNameSet = new HashSet<>();
37      private EventListenerList listenerList = new EventListenerList();
38  
39  
40      /* (non-Javadoc)
41       * @see org.apache.log4j.chainsaw.LoggerNameModel#getLoggerNames()
42       */
43      public Collection getLoggerNames() {
44          return Collections.unmodifiableSet(loggerNameSet);
45      }
46  
47      /* (non-Javadoc)
48       * @see org.apache.log4j.chainsaw.LoggerNameModel#addLoggerName(java.lang.String)
49       */
50      public boolean addLoggerName(String loggerName) {
51          boolean isNew = loggerNameSet.add(loggerName);
52  
53          if (isNew) {
54              notifyListeners(loggerName);
55          }
56  
57          return isNew;
58      }
59  
60      public void reset() {
61          loggerNameSet.clear();
62          LoggerNameListener[] eventListeners = listenerList.getListeners(LoggerNameListener.class);
63  
64          for (LoggerNameListener listener : eventListeners) {
65              listener.reset();
66          }
67      }
68  
69      /**
70       * Notifies all the registered listeners that a new unique
71       * logger name has been added to this model
72       *
73       * @param loggerName
74       */
75      private void notifyListeners(String loggerName) {
76          LoggerNameListener[] eventListeners = listenerList.getListeners(LoggerNameListener.class);
77  
78          for (LoggerNameListener listener : eventListeners) {
79              listener.loggerNameAdded(loggerName);
80          }
81      }
82  
83      /* (non-Javadoc)
84       * @see org.apache.log4j.chainsaw.LoggerNameModel#addLoggerNameListener(org.apache.log4j.chainsaw.LoggerNameListener)
85       */
86      public void addLoggerNameListener(LoggerNameListener l) {
87          listenerList.add(LoggerNameListener.class, l);
88      }
89  
90      /* (non-Javadoc)
91       * @see org.apache.log4j.chainsaw.LoggerNameModel#removeLoggerNameListener(org.apache.log4j.chainsaw.LoggerNameListener)
92       */
93      public void removeLoggerNameListener(LoggerNameListener l) {
94          listenerList.remove(LoggerNameListener.class, l);
95      }
96  }