View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.server.core.event;
21  
22  import java.util.ArrayList;
23  
24  import org.apache.directory.shared.ldap.message.PersistentSearchControl;
25  
26  
27  /**
28   * The different kinds of events a {@link DirectoryListener} may register for 
29   * notification on using the {@link EventService}.  Sometimes an entry is 
30   * moved and renamed at the same time.  These notifications are sent when 
31   * either RENAME or MOVE notifications are enabled.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev$, $Date$
35   */
36  public enum EventType
37  {
38      ADD(1), DELETE(2), MODIFY(4), RENAME(8), MOVE(16);
39      
40      
41      public static final int ALL_EVENT_TYPES_MASK = getAllEventTypesMask();
42      public static final int MOVE_OR_RENAME_MASK = MOVE.mask | RENAME.mask;
43      private static final EventType[] EMPTY_EVENT_ARRAY = new EventType[0];
44      
45      private int mask;
46      
47      
48      private EventType( int mask )
49      {
50          this.mask = mask;
51      }
52      
53      
54      public int getMask()
55      {
56          return mask;
57      }
58  
59      
60      /**
61       * Gets an array of EventTypes from the PSearchControl changeTypes 
62       * parameter value.  According to the documentation of the changeTypes 
63       * field of the Persistent Search Control:
64       * 
65       * <code>
66       * The changeTypes field is the logical OR of one or more of these values:
67       * add (1), delete (2), modify (4), modDN (8). By default this is set to 1 |
68       * 2 | 4 | 8 which is the integer value 0x0F or 15.
69       * </code>
70       * 
71       * NOTE: When the changeTypes mask includes a modDN(8) we include both the 
72       * RENAME and MOVE EventType objects in the array.
73       * 
74       * @see PersistentSearchControl
75       * @param psearchChangeTypes the value of the changeTypes parameter
76       * @return array of EventType objects
77       */
78      public static EventType[] getEventTypes( int psearchChangeTypes )
79      {
80          ArrayList<EventType> types = new ArrayList<EventType>();
81          
82          if ( isAdd( psearchChangeTypes ) )
83          {
84              types.add( ADD );
85          }
86          
87          if ( isDelete( psearchChangeTypes ) )
88          {
89              types.add( DELETE );
90          }
91          
92          if ( isModify( psearchChangeTypes ) )
93          {
94              types.add( MODIFY );
95          }
96          
97          if ( ( psearchChangeTypes & 8 ) > 0 )
98          {
99              types.add( MOVE );
100             types.add( RENAME );
101         }
102         
103         return types.toArray( EMPTY_EVENT_ARRAY );
104     }
105     
106     
107     private static int getAllEventTypesMask()
108     {
109         int allTypes = 0;
110         
111         for ( EventType type : values() )
112         {
113             allTypes |= type.getMask();
114         }
115         
116         return allTypes;
117     }
118     
119     
120     public static boolean isAdd( int mask )
121     {
122         if ( ( mask & ADD.mask ) > 0 )
123         {
124             return true;
125         }
126         
127         return false;
128     }
129     
130     
131     public static boolean isDelete( int mask )
132     {
133         if ( ( mask & DELETE.mask ) > 0 )
134         {
135             return true;
136         }
137         
138         return false;
139     }
140     
141     
142     public static boolean isModify( int mask )
143     {
144         if ( ( mask & MODIFY.mask ) > 0 )
145         {
146             return true;
147         }
148         
149         return false;
150     }
151     
152     
153     public static boolean isMove( int mask )
154     {
155         if ( ( mask & MOVE.mask ) > 0 )
156         {
157             return true;
158         }
159         
160         return false;
161     }
162     
163     
164     public static boolean isRename( int mask )
165     {
166         if ( ( mask & RENAME.mask ) > 0 )
167         {
168             return true;
169         }
170         
171         return false;
172     }
173     
174     
175     public static boolean isMoveAndRename( int mask )
176     {
177         if ( ( mask & MOVE_OR_RENAME_MASK ) > 0 )
178         {
179             return true;
180         }
181         
182         return false;
183     }
184     
185     
186     public static int getMask( EventType ...eventTypes )
187     {
188         int mask = 0;
189         
190         for ( EventType type : eventTypes )
191         {
192             mask |= type.getMask();
193         }
194         
195         return mask;
196     }
197 }