Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/NotifierArgs.java
41161 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.ldap;2627import javax.naming.directory.SearchControls;28import javax.naming.event.*;2930/**31* This class holds the information in an event registration/deregistration32* request. This includes the name, filter, search controls and33* the different interfaces that the listener implements. This last piece34* of information determines which event(s) the listener is interested in.35*<p>36* It overrides equals() and hashCode() to use all these pieces of37* information so that it can be used correctly in a hashtable.38*39* @author Rosanna Lee40*/41final class NotifierArgs {42static final int ADDED_MASK = 0x1;43static final int REMOVED_MASK = 0x2;44static final int CHANGED_MASK = 0x4;45static final int RENAMED_MASK = 0x8;4647// these fields are package private; used by NamingEventNotifier48String name;49String filter;50SearchControls controls;51int mask;5253// package private54NotifierArgs(String name, int scope, NamingListener l) {55this(name, "(objectclass=*)", null, l);5657// if scope is not default, create search ctl and set it58if (scope != EventContext.ONELEVEL_SCOPE) {59controls = new SearchControls();60controls.setSearchScope(scope);61}62}6364// package private65NotifierArgs(String name, String filter, SearchControls ctls,66NamingListener l) {67this.name = name;68this.filter = filter;69this.controls = ctls;7071if (l instanceof NamespaceChangeListener) {72mask |= ADDED_MASK|REMOVED_MASK|RENAMED_MASK;73}74if (l instanceof ObjectChangeListener) {75mask |= CHANGED_MASK;76}77}7879// checks name, filter, controls80public boolean equals(Object obj) {81if (obj instanceof NotifierArgs) {82NotifierArgs target = (NotifierArgs)obj;83return mask == target.mask &&84name.equals(target.name) && filter.equals(target.filter) &&85checkControls(target.controls);86}87return false;88}8990private boolean checkControls(SearchControls ctls) {91if ((controls == null || ctls == null)) {92return ctls == controls;93}94// ctls are nonempty9596return (controls.getSearchScope() == ctls.getSearchScope()) &&97(controls.getTimeLimit() == ctls.getTimeLimit()) &&98(controls.getDerefLinkFlag() == ctls.getDerefLinkFlag()) &&99(controls.getReturningObjFlag() == ctls.getReturningObjFlag()) &&100(controls.getCountLimit() == ctls.getCountLimit()) &&101checkStringArrays(controls.getReturningAttributes(),102ctls.getReturningAttributes());103}104105private static boolean checkStringArrays(String[] s1, String[] s2) {106if ((s1 == null) || (s2 == null)) {107return s1 == s2;108}109110// both are nonnull111if (s1.length != s2.length) {112return false;113}114115for (int i = 0; i < s1.length; i++) {116if (!s1[i].equals(s2[i])) {117return false;118}119}120return true;121}122123// save from having to recalculate each time124private int sum = -1;125public int hashCode() {126if (sum == -1)127sum = mask + name.hashCode() + filter.hashCode() + controlsCode();128return sum;129}130131// used in calculating hash code132private int controlsCode() {133if (controls == null) return 0;134135int total = controls.getTimeLimit() + (int)controls.getCountLimit() +136(controls.getDerefLinkFlag() ? 1 : 0) +137(controls.getReturningObjFlag() ? 1 : 0);138139String[] attrs = controls.getReturningAttributes();140if (attrs != null) {141for (int i = 0; i < attrs.length; i++) {142total += attrs[i].hashCode();143}144}145146return total;147}148}149150151