<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.felix.framework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.felix.framework.util.Util;
import org.apache.felix.framework.wiring.BundleCapabilityImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;

class BundleRevisionDependencies
{
    private final Map<BundleRevision, Map<BundleCapability, Set<BundleRevision>>>
        m_dependentsMap = new HashMap<BundleRevision, Map<BundleCapability, Set<BundleRevision>>>();

    public synchronized void addDependent(BundleRevision provider, BundleCapability cap, BundleRevision requirer)
    {
        Map<BundleCapability, Set<BundleRevision>> caps = m_dependentsMap.get(provider);
        if (caps == null)
        {
            caps = new HashMap<BundleCapability, Set<BundleRevision>>();
            m_dependentsMap.put(provider, caps);
        }
        Set<BundleRevision> dependents = caps.get(cap);
        if (dependents == null)
        {
            dependents = new HashSet<BundleRevision>();
            caps.put(cap, dependents);
        }
        dependents.add(requirer);
    }

    public synchronized void removeDependent(
        BundleRevision provider, BundleCapability cap, BundleRevision requirer)
    {
        Map<BundleCapability, Set<BundleRevision>> caps = m_dependentsMap.get(provider);
        if (caps != null)
        {
            Set<BundleRevision> dependents = caps.get(cap);
            if (dependents == null)
            {
                dependents.remove(requirer);
                if (dependents.isEmpty())
                {
                    caps.remove(cap);
                    if (caps.isEmpty())
                    {
                        m_dependentsMap.remove(provider);
                    }
                }
            }
        }
    }

    public synchronized void removeDependents(BundleRevision provider)
    {
        m_dependentsMap.remove(provider);
    }

    public synchronized Map<BundleCapability, Set<BundleRevision>>
        getDependents(BundleRevision provider)
    {
        return m_dependentsMap.get(provider);
    }

    public synchronized boolean hasDependents(BundleImpl bundle)
    {
        List<BundleRevision> revisions = bundle.getRevisions();
        for (BundleRevision revision : revisions)
        {
            if (m_dependentsMap.containsKey(revision))
            {
                return true;
            }
        }
        return false;
    }

    public synchronized Set<Bundle> getDependentBundles(BundleImpl bundle)
    {
        Set<Bundle> result = new HashSet<Bundle>();

        List<BundleRevision> revisions = bundle.getRevisions();
        for (BundleRevision revision : revisions)
        {
// TODO: OSGi R4.3 - This is sort of a hack. We need to special case fragments,
//       since their dependents are their hosts.
            if (Util.isFragment(revision))
            {
                if (revision.getWiring() != null)
                {
                    for (BundleWire wire : revision.getWiring().getRequiredWires(null))
                    {<commit_msg>Dependents for fragments are calculated differently. (FELIX-2950)<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.felix.framework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.felix.framework.util.Util;
import org.apache.felix.framework.wiring.BundleCapabilityImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;

class BundleRevisionDependencies
{
    private final Map<BundleRevision, Map<BundleCapability, Set<BundleRevision>>>
        m_dependentsMap = new HashMap<BundleRevision, Map<BundleCapability, Set<BundleRevision>>>();

    public synchronized void addDependent(BundleRevision provider, BundleCapability cap, BundleRevision requirer)
    {
        Map<BundleCapability, Set<BundleRevision>> caps = m_dependentsMap.get(provider);
        if (caps == null)
        {
            caps = new HashMap<BundleCapability, Set<BundleRevision>>();
            m_dependentsMap.put(provider, caps);
        }
        Set<BundleRevision> dependents = caps.get(cap);
        if (dependents == null)
        {
            dependents = new HashSet<BundleRevision>();
            caps.put(cap, dependents);
        }
        dependents.add(requirer);
    }

    public synchronized void removeDependent(
        BundleRevision provider, BundleCapability cap, BundleRevision requirer)
    {
        Map<BundleCapability, Set<BundleRevision>> caps = m_dependentsMap.get(provider);
        if (caps != null)
        {
            Set<BundleRevision> dependents = caps.get(cap);
            if (dependents == null)
            {
                dependents.remove(requirer);
                if (dependents.isEmpty())
                {
                    caps.remove(cap);
                    if (caps.isEmpty())
                    {
                        m_dependentsMap.remove(provider);
                    }
                }
            }
        }
    }

    public synchronized void removeDependents(BundleRevision provider)
    {
        m_dependentsMap.remove(provider);
    }

    public synchronized Map<BundleCapability, Set<BundleRevision>>
        getDependents(BundleRevision provider)
    {
        return m_dependentsMap.get(provider);
    }

    public synchronized boolean hasDependents(BundleImpl bundle)
    {
        List<BundleRevision> revisions = bundle.getRevisions();
        for (BundleRevision revision : revisions)
        {
            // We have to special case fragments, since their dependencies
            // are actually reversed (i.e., they require a host, but then
            // the host ends up dependent on them at run time).
            if (Util.isFragment(revision)
                && (revision.getWiring() != null)
                && !revision.getWiring().getRequiredWires(null).isEmpty())
            {
                return true;
            }
            else if (m_dependentsMap.containsKey(revision))
            {
                return true;
            }
        }
        return false;
    }

    public synchronized Set<Bundle> getDependentBundles(BundleImpl bundle)
    {
        Set<Bundle> result = new HashSet<Bundle>();

        List<BundleRevision> revisions = bundle.getRevisions();
        for (BundleRevision revision : revisions)
        {
// TODO: OSGi R4.3 - This is sort of a hack. We need to special case fragments,
//       since their dependents are their hosts.
            if (Util.isFragment(revision))
            {
                if (revision.getWiring() != null)
                {
                    for (BundleWire wire : revision.getWiring().getRequiredWires(null))
                    {
{'fraction_non_alphanumeric': 0.08708877872915728, 'fraction_numerical': 0.0018026137899954935, 'mean_word_length': 2.4353715170278636, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '683078', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2243, 'n_words': 637}