001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.core.event;
021
022 import java.util.ArrayList;
023
024 import org.apache.directory.shared.ldap.codec.search.controls.persistentSearch.PersistentSearchControl;
025
026
027
028 /**
029 * The different kinds of events a {@link DirectoryListener} may register for
030 * notification on using the {@link EventService}. Sometimes an entry is
031 * moved and renamed at the same time. These notifications are sent when
032 * either RENAME or MOVE notifications are enabled.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev$, $Date$
036 */
037 public enum EventType
038 {
039 ADD(1), DELETE(2), MODIFY(4), RENAME(8), MOVE(16);
040
041
042 public static final int ALL_EVENT_TYPES_MASK = getAllEventTypesMask();
043 public static final int MOVE_OR_RENAME_MASK = MOVE.mask | RENAME.mask;
044 private static final EventType[] EMPTY_EVENT_ARRAY = new EventType[0];
045
046 private int mask;
047
048
049 private EventType( int mask )
050 {
051 this.mask = mask;
052 }
053
054
055 public int getMask()
056 {
057 return mask;
058 }
059
060
061 /**
062 * Gets an array of EventTypes from the PSearchControl changeTypes
063 * parameter value. According to the documentation of the changeTypes
064 * field of the Persistent Search Control:
065 *
066 * <code>
067 * The changeTypes field is the logical OR of one or more of these values:
068 * add (1), delete (2), modify (4), modDN (8). By default this is set to 1 |
069 * 2 | 4 | 8 which is the integer value 0x0F or 15.
070 * </code>
071 *
072 * NOTE: When the changeTypes mask includes a modDN(8) we include both the
073 * RENAME and MOVE EventType objects in the array.
074 *
075 * @see PersistentSearchControl
076 * @param psearchChangeTypes the value of the changeTypes parameter
077 * @return array of EventType objects
078 */
079 public static EventType[] getEventTypes( int psearchChangeTypes )
080 {
081 ArrayList<EventType> types = new ArrayList<EventType>();
082
083 if ( isAdd( psearchChangeTypes ) )
084 {
085 types.add( ADD );
086 }
087
088 if ( isDelete( psearchChangeTypes ) )
089 {
090 types.add( DELETE );
091 }
092
093 if ( isModify( psearchChangeTypes ) )
094 {
095 types.add( MODIFY );
096 }
097
098 if ( ( psearchChangeTypes & 8 ) > 0 )
099 {
100 types.add( MOVE );
101 types.add( RENAME );
102 }
103
104 return types.toArray( EMPTY_EVENT_ARRAY );
105 }
106
107
108 private static int getAllEventTypesMask()
109 {
110 int allTypes = 0;
111
112 for ( EventType type : values() )
113 {
114 allTypes |= type.getMask();
115 }
116
117 return allTypes;
118 }
119
120
121 public static boolean isAdd( int mask )
122 {
123 if ( ( mask & ADD.mask ) > 0 )
124 {
125 return true;
126 }
127
128 return false;
129 }
130
131
132 public static boolean isDelete( int mask )
133 {
134 if ( ( mask & DELETE.mask ) > 0 )
135 {
136 return true;
137 }
138
139 return false;
140 }
141
142
143 public static boolean isModify( int mask )
144 {
145 if ( ( mask & MODIFY.mask ) > 0 )
146 {
147 return true;
148 }
149
150 return false;
151 }
152
153
154 public static boolean isMove( int mask )
155 {
156 if ( ( mask & MOVE.mask ) > 0 )
157 {
158 return true;
159 }
160
161 return false;
162 }
163
164
165 public static boolean isRename( int mask )
166 {
167 if ( ( mask & RENAME.mask ) > 0 )
168 {
169 return true;
170 }
171
172 return false;
173 }
174
175
176 public static boolean isMoveAndRename( int mask )
177 {
178 if ( ( mask & MOVE_OR_RENAME_MASK ) > 0 )
179 {
180 return true;
181 }
182
183 return false;
184 }
185
186
187 public static int getMask( EventType ...eventTypes )
188 {
189 int mask = 0;
190
191 for ( EventType type : eventTypes )
192 {
193 mask |= type.getMask();
194 }
195
196 return mask;
197 }
198 }