<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.river.discovery;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import net.jini.core.constraint.ConnectionAbsoluteTime;
import net.jini.core.constraint.ConnectionRelativeTime;
import net.jini.core.constraint.ConstraintAlternatives;
import net.jini.core.constraint.InvocationConstraint;
import net.jini.core.constraint.InvocationConstraints;
import net.jini.core.constraint.MethodConstraints;
import net.jini.io.UnsupportedConstraintException;

/**
 * Class for processing constraints which apply to the discovery protocol:
 * {@link DiscoveryProtocolVersion}, {@link MulticastMaxPacketSize},
 * {@link MulticastTimeToLive}, {@link UnicastSocketTimeout},
 * {@link net.jini.core.constraint.ConnectionRelativeTime},
 * {@link net.jini.core.constraint.ConnectionAbsoluteTime}.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 */
public class DiscoveryConstraints {

    /** Method object for the multicastRequest method of this class. */
    public static final Method multicastRequestMethod;
    /** Method object for the multicastAnnouncement method of this class. */
    public static final Method multicastAnnouncementMethod;
    /** Method object for the unicastDiscovery method of this class. */
    public static final Method unicastDiscoveryMethod;
    static {
	try {
	    multicastRequestMethod = DiscoveryConstraints.class.getMethod(
		"multicastRequest", (Class<?>[]) null);
	    multicastAnnouncementMethod = DiscoveryConstraints.class.getMethod(
		"multicastAnnouncement", (Class<?>[]) null);
	    unicastDiscoveryMethod = DiscoveryConstraints.class.getMethod(
		"unicastDiscovery", (Class<?>[]) null);
	} catch (NoSuchMethodException e) {
	    throw new AssertionError(e);
	}
    }

    private static final Set<DiscoveryProtocolVersion> supportedProtocols = new HashSet<DiscoveryProtocolVersion>(2);
    static {
	supportedProtocols.add(DiscoveryProtocolVersion.ONE);
	supportedProtocols.add(DiscoveryProtocolVersion.TWO);
    }

    private final InvocationConstraints unfulfilled;
    private final Set<DiscoveryProtocolVersion> protocolVersions;
    private final int preferredProtocolVersion;
    private final ConnectionAbsoluteTime connectionAbsoluteTime;
    private final MulticastMaxPacketSize maxPacketSize;
    private final MulticastTimeToLive timeToLive;
    private final UnicastSocketTimeout socketTimeout;
    private final int hashcode;

    @Override
    public int hashCode() {
	return hashcode;
    }
    
    @Override
    public boolean equals(Object o){
        if ( o == null ) return false;
	if ( o == this ) return true;
	if ( o.hashCode() != hashcode) return false;
	if ( o instanceof DiscoveryConstraints) {
	    DiscoveryConstraints that = (DiscoveryConstraints) o;
	    if ( unfulfilled != null ) {
		if ( !unfulfilled.equals(that.unfulfilled) ) return false;
	    } else if ( unfulfilled != that.unfulfilled) return false;
	    if (  protocolVersions != null ) {
		if ( !protocolVersions.equals(that.protocolVersions) ) return false;
	    } else if ( protocolVersions!= that.protocolVersions) return false;
	    if (  connectionAbsoluteTime != null ) {
		if ( !connectionAbsoluteTime.equals(that.connectionAbsoluteTime) ) return false;
	    } else if ( connectionAbsoluteTime != that.connectionAbsoluteTime) return false;
	    if (preferredProtocolVersion != that.preferredProtocolVersion) return false;
	    if ( maxPacketSize  != null ) {
		if ( !maxPacketSize.equals(that.maxPacketSize) ) return false;
	    } else if ( maxPacketSize != that.maxPacketSize) return false;    
	    if (  timeToLive != null ) {
		if ( !timeToLive.equals(that.timeToLive) ) return false;
	    } else if ( timeToLive != that.timeToLive) return false;
	    if (  socketTimeout != null ) {
		if ( !socketTimeout.equals(that.socketTimeout) ) return false;
	    } else if ( socketTimeout != that.socketTimeout) return false;    		
	    return true;	    
	}
	return false;
    }

    /**
     * Empty method which serves as a {@link MethodConstraints} key for looking
     * up {@link InvocationConstraints} that apply to multicast requests.
     */
    public static void multicastRequest() {
    }

    /**
     * Empty method which serves as a {@link MethodConstraints} key for looking
     * up {@link InvocationConstraints} that apply to multicast announcements.
     */
    public static void multicastAnnouncement() {
    }

    /**
     * Empty method which serves as a {@link MethodConstraints} key for looking
     * up {@link InvocationConstraints} that apply to unicast discovery.
     */
    public static void unicastDiscovery() {
    }

    /**
     * Processes the discovery-related constraints in the given set of
     * constraints, returning a <code>DiscoveryConstraints</code> instance from
     * which the constraint results can be queried.  Processing of timeout
     * constraints is time dependent, and subsequent invocations of this
     * method with the same <code>InvocationConstraints</code> may result in
     * <code>DiscoveryConstraint</code> instances with different constraint
     * results.
     *
     * @param constraints the constraints to process
     * @return an instance representing processed constraints
     * @throws UnsupportedConstraintException if the discovery-related
     * constraints contain conflicts, or otherwise cannot be processed
     * @throws NullPointerException if <code>constraints</code> is
     * <code>null</code>
     *
     */
    public static DiscoveryConstraints process(
					   InvocationConstraints constraints)
	throws UnsupportedConstraintException
    {
	return new DiscoveryConstraints(constraints);
    }

    private DiscoveryConstraints(InvocationConstraints constraints)
	throws UnsupportedConstraintException
    {
	unfulfilled = new InvocationConstraints(
	    getUnfulfilled(constraints.requirements()),
	    getUnfulfilled(constraints.preferences()));

	ConstraintReducer<DiscoveryProtocolVersion> cr = 
	    new ConstraintReducer<DiscoveryProtocolVersion>(DiscoveryProtocolVersion.class);
	protocolVersions = cr.reduce(
	    new InvocationConstraints(constraints.requirements(), null));
	if (!protocolVersions.isEmpty() &&
	    intersect(protocolVersions, supportedProtocols).isEmpty())
	{
	    throw new UnsupportedConstraintException(
		"no supported protocols: " + protocolVersions);
	}
	preferredProtocolVersion = chooseProtocolVersion(
	    protocolVersions, cr.reduce(constraints), unfulfilled);
	Set<MulticastMaxPacketSize> mmps 
	    = new MulticastMaxPacketSizeReducer().reduce(constraints);
	maxPacketSize = mmps.isEmpty() ? null : getElement(mmps);
	Set<MulticastTimeToLive> mttl 
	    = new ConstraintReducer<MulticastTimeToLive>(
	    MulticastTimeToLive.class).reduce(constraints);
	timeToLive = mttl.isEmpty() ? null : getElement(mttl);
	Set<UnicastSocketTimeout> ust 
	    = new ConstraintReducer<UnicastSocketTimeout>(
	    UnicastSocketTimeout.class).reduce(constraints);
	socketTimeout = ust.isEmpty() ? null : getElement(ust);
	InvocationConstraints absConstraints 
	    = new InvocationConstraints(
		constraints.requirements(),
		constraints.preferences()).makeAbsolute();
	Set<ConnectionAbsoluteTime> cat 
		= new ConnectionAbsoluteTimeReducer().reduce(absConstraints);
	connectionAbsoluteTime = cat.isEmpty() ? null : getElement(cat);
	int hash = 7;
	hash = 41 * hash + (this.unfulfilled != null ? this.unfulfilled.hashCode() : 0);
	hash = 41 * hash + this.protocolVersions.hashCode();
	hash = 41 * hash + this.preferredProtocolVersion;
	hash = 41 * hash + (this.connectionAbsoluteTime != null ? this.connectionAbsoluteTime.hashCode() : 0);
	hash = 41 * hash + (this.maxPacketSize != null ? this.maxPacketSize.hashCode() : 0);
	hash = 41 * hash + (this.timeToLive != null ? this.timeToLive.hashCode() : 0);
	hash = 41 * hash + (this.socketTimeout != null ? this.socketTimeout.hashCode() : 0);
	hashcode = hash;
    }

    /**<commit_msg>Module :: JGDMS Jini Platform - FindBugs static analysis - bugs #27<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.river.discovery;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import net.jini.core.constraint.ConnectionAbsoluteTime;
import net.jini.core.constraint.ConnectionRelativeTime;
import net.jini.core.constraint.ConstraintAlternatives;
import net.jini.core.constraint.InvocationConstraint;
import net.jini.core.constraint.InvocationConstraints;
import net.jini.core.constraint.MethodConstraints;
import net.jini.io.UnsupportedConstraintException;

/**
 * Class for processing constraints which apply to the discovery protocol:
 * {@link DiscoveryProtocolVersion}, {@link MulticastMaxPacketSize},
 * {@link MulticastTimeToLive}, {@link UnicastSocketTimeout},
 * {@link net.jini.core.constraint.ConnectionRelativeTime},
 * {@link net.jini.core.constraint.ConnectionAbsoluteTime}.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 */
public class DiscoveryConstraints {

    /** Method object for the multicastRequest method of this class. */
    public static final Method multicastRequestMethod;
    /** Method object for the multicastAnnouncement method of this class. */
    public static final Method multicastAnnouncementMethod;
    /** Method object for the unicastDiscovery method of this class. */
    public static final Method unicastDiscoveryMethod;
    static {
	try {
	    multicastRequestMethod = DiscoveryConstraints.class.getMethod(
		"multicastRequest", (Class<?>[]) null);
	    multicastAnnouncementMethod = DiscoveryConstraints.class.getMethod(
		"multicastAnnouncement", (Class<?>[]) null);
	    unicastDiscoveryMethod = DiscoveryConstraints.class.getMethod(
		"unicastDiscovery", (Class<?>[]) null);
	} catch (NoSuchMethodException e) {
	    throw new AssertionError(e);
	}
    }

    private static final Set<DiscoveryProtocolVersion> supportedProtocols = new HashSet<DiscoveryProtocolVersion>(2);
    static {
	supportedProtocols.add(DiscoveryProtocolVersion.ONE);
	supportedProtocols.add(DiscoveryProtocolVersion.TWO);
    }

    private final InvocationConstraints unfulfilled;
    private final Set<DiscoveryProtocolVersion> protocolVersions;
    private final int preferredProtocolVersion;
    private final ConnectionAbsoluteTime connectionAbsoluteTime;
    private final MulticastMaxPacketSize maxPacketSize;
    private final MulticastTimeToLive timeToLive;
    private final UnicastSocketTimeout socketTimeout;
    private final int hashcode;

    @Override
    public int hashCode() {
	return hashcode;
    }
    
    @Override
    public boolean equals(Object o){
        if ( o == null ) return false;
	if ( o == this ) return true;
	if ( o.hashCode() != hashcode) return false;
	if ( o instanceof DiscoveryConstraints) {
	    DiscoveryConstraints that = (DiscoveryConstraints) o;
	    if ( unfulfilled != null ) {
		if ( !unfulfilled.equals(that.unfulfilled) ) return false;
	    } else if ( unfulfilled != that.unfulfilled) return false;
	    if (  protocolVersions != null ) {
		if ( !protocolVersions.equals(that.protocolVersions) ) return false;
	    } else if ( protocolVersions!= that.protocolVersions) return false;
	    if (  connectionAbsoluteTime != null ) {
		if ( !connectionAbsoluteTime.equals(that.connectionAbsoluteTime) ) return false;
	    } else if ( connectionAbsoluteTime != that.connectionAbsoluteTime) return false;
	    if (preferredProtocolVersion != that.preferredProtocolVersion) return false;
	    if ( maxPacketSize  != null ) {
		if ( !maxPacketSize.equals(that.maxPacketSize) ) return false;
	    } else if ( maxPacketSize != that.maxPacketSize) return false;    
	    if (  timeToLive != null ) {
		if ( !timeToLive.equals(that.timeToLive) ) return false;
	    } else if ( timeToLive != that.timeToLive) return false;
	    if (  socketTimeout != null ) {
		if ( !socketTimeout.equals(that.socketTimeout) ) return false;
	    } else if ( socketTimeout != that.socketTimeout) return false;    		
	    return true;	    
	}
	return false;
    }

    /**
     * Empty method which serves as a {@link MethodConstraints} key for looking
     * up {@link InvocationConstraints} that apply to multicast requests.
     */
    public static void multicastRequest() {
    }

    /**
     * Empty method which serves as a {@link MethodConstraints} key for looking
     * up {@link InvocationConstraints} that apply to multicast announcements.
     */
    public static void multicastAnnouncement() {
    }

    /**
     * Empty method which serves as a {@link MethodConstraints} key for looking
     * up {@link InvocationConstraints} that apply to unicast discovery.
     */
    public static void unicastDiscovery() {
    }

    /**
     * Processes the discovery-related constraints in the given set of
     * constraints, returning a <code>DiscoveryConstraints</code> instance from
     * which the constraint results can be queried.  Processing of timeout
     * constraints is time dependent, and subsequent invocations of this
     * method with the same <code>InvocationConstraints</code> may result in
     * <code>DiscoveryConstraint</code> instances with different constraint
     * results.
     *
     * @param constraints the constraints to process
     * @return an instance representing processed constraints
     * @throws UnsupportedConstraintException if the discovery-related
     * constraints contain conflicts, or otherwise cannot be processed
     * @throws NullPointerException if <code>constraints</code> is
     * <code>null</code>
     *
     */
    public static DiscoveryConstraints process(
					   InvocationConstraints constraints)
	throws UnsupportedConstraintException
    {
	return new DiscoveryConstraints(constraints);
    }

    private DiscoveryConstraints(InvocationConstraints constraints)
	throws UnsupportedConstraintException
    {
	unfulfilled = new InvocationConstraints(
	    getUnfulfilled(constraints.requirements()),
	    getUnfulfilled(constraints.preferences()));

	ConstraintReducer<DiscoveryProtocolVersion> cr = 
	    new ConstraintReducer<DiscoveryProtocolVersion>(DiscoveryProtocolVersion.class);
	protocolVersions = cr.reduce(
	    new InvocationConstraints(constraints.requirements(), null));
	if (!protocolVersions.isEmpty() &&
	    intersect(protocolVersions, supportedProtocols).isEmpty())
	{
	    throw new UnsupportedConstraintException(
		"no supported protocols: " + protocolVersions);
	}
	preferredProtocolVersion = chooseProtocolVersion(
	    protocolVersions, cr.reduce(constraints), unfulfilled);
	Set<MulticastMaxPacketSize> mmps 
	    = new MulticastMaxPacketSizeReducer().reduce(constraints);
	maxPacketSize = mmps.isEmpty() ? null : getElement(mmps);
	Set<MulticastTimeToLive> mttl 
	    = new ConstraintReducer<MulticastTimeToLive>(
	    MulticastTimeToLive.class).reduce(constraints);
	timeToLive = mttl.isEmpty() ? null : getElement(mttl);
	Set<UnicastSocketTimeout> ust 
	    = new ConstraintReducer<UnicastSocketTimeout>(
	    UnicastSocketTimeout.class).reduce(constraints);
	socketTimeout = ust.isEmpty() ? null : getElement(ust);
	InvocationConstraints absConstraints 
	    = new InvocationConstraints(
		constraints.requirements(),
		constraints.preferences()).makeAbsolute();
	Set<ConnectionAbsoluteTime> cat 
		= new ConnectionAbsoluteTimeReducer().reduce(absConstraints);
	connectionAbsoluteTime = cat.isEmpty() ? null : getElement(cat);
	int hash = 7;
	hash = 41 * hash + this.unfulfilled.hashCode();
	hash = 41 * hash + this.protocolVersions.hashCode();
	hash = 41 * hash + this.preferredProtocolVersion;
	hash = 41 * hash + (this.connectionAbsoluteTime != null ? this.connectionAbsoluteTime.hashCode() : 0);
	hash = 41 * hash + (this.maxPacketSize != null ? this.maxPacketSize.hashCode() : 0);
	hash = 41 * hash + (this.timeToLive != null ? this.timeToLive.hashCode() : 0);
	hash = 41 * hash + (this.socketTimeout != null ? this.socketTimeout.hashCode() : 0);
	hashcode = hash;
    }

    /**
{'fraction_non_alphanumeric': 0.09126466753585398, 'fraction_numerical': 0.003117737089734142, 'mean_word_length': 4.2427934621099554, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4559650', 'n_tokens_mistral': 4902, 'n_tokens_neox': 4659, 'n_words': 1367}