<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.jena.atlas.lib;

import java.util.* ;

import org.apache.jena.atlas.iterator.IteratorConcat ;

/* Map from K to collection of V */

public abstract class MultiMap<K, V>
{
    private Map<K, Collection<V>> map = new HashMap<>() ;

    protected abstract Collection<V> createCollection() ;
    
    public static <K, V> MultiMapToList<K, V> createMapList() { return new MultiMapToList<>() ; }
    public static <K, V> MultiMapToSet<K, V> createMapSet() { return new MultiMapToSet<>() ; }
    
    protected MultiMap() { }
    
    protected Collection<V> getByKey(K key) { 
        return map.get(key) ; 
    } 

    public abstract Collection<V> get(K key) ; 
    
    public V getOne(K key) { 
        Collection<V> c = map.get(key) ;
        if ( c == null || c.size() == 0 ) 
            return null ;
        return c.iterator().next() ;
    }
    
    public void putAll(K key, @SuppressWarnings("unchecked") V ... values)
    {
        for ( V v : values)
            put(key, v) ;
    }
    
    public void put(K key, V value)
    { 
        Collection<V> x = map.get(key) ;
        if ( x == null )
        {
            x = createCollection() ;
            map.put(key, x) ;
        }
        x.add(value) ;
    }
    
    public void remove(K key, V value)  { map.get(key).remove(value) ; }
    public void removeKey(K key)        { map.remove(key) ; }
    
    protected Collection<V> valuesForKey(K key) { return map.get(key); }
    public abstract Collection<V> values(K key) ;
    public abstract Collection<V> values() ;

    public boolean containsKey(K key) { return map.containsKey(key) ; }
    
    public Set<K> keys()        { return map.keySet() ; }
    
    public void clear()         { map.clear() ; }
    
    public boolean isEmpty()    { return map.isEmpty() ; }

    /** Does not materialise the contents */
    public Iterator<V> flatten()
    {
        IteratorConcat<V> all = new IteratorConcat<>() ;
        for ( K k : map.keySet() )        
        {
            Collection<V> x =  map.get(k) ;
            all.add(x.iterator()) ;
        }
        return all ;
    }
    
    @Override
    public boolean equals(Object other)
    {
        if ( this == other ) return true ;<commit_msg>Cope with no entry at all on deletion<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.jena.atlas.lib;

import java.util.* ;

import org.apache.jena.atlas.iterator.IteratorConcat ;

/* Map from K to collection of V */

public abstract class MultiMap<K, V>
{
    private Map<K, Collection<V>> map = new HashMap<>() ;

    protected abstract Collection<V> createCollection() ;
    
    public static <K, V> MultiMapToList<K, V> createMapList() { return new MultiMapToList<>() ; }
    public static <K, V> MultiMapToSet<K, V> createMapSet() { return new MultiMapToSet<>() ; }
    
    protected MultiMap() { }
    
    protected Collection<V> getByKey(K key) { 
        return map.get(key) ; 
    } 

    public abstract Collection<V> get(K key) ; 
    
    public V getOne(K key) { 
        Collection<V> c = map.get(key) ;
        if ( c == null || c.size() == 0 ) 
            return null ;
        return c.iterator().next() ;
    }
    
    public void putAll(K key, @SuppressWarnings("unchecked") V ... values)
    {
        for ( V v : values)
            put(key, v) ;
    }
    
    public void put(K key, V value)
    { 
        Collection<V> x = map.get(key) ;
        if ( x == null )
        {
            x = createCollection() ;
            map.put(key, x) ;
        }
        x.add(value) ;
    }
    
    public void remove(K key, V value)  {
        if ( map.containsKey(key))
            map.get(key).remove(value) ;
    }
    public void removeKey(K key)        { map.remove(key) ; }
    
    protected Collection<V> valuesForKey(K key) { return map.get(key); }
    public abstract Collection<V> values(K key) ;
    public abstract Collection<V> values() ;

    public boolean containsKey(K key) { return map.containsKey(key) ; }
    
    public Set<K> keys()        { return map.keySet() ; }
    
    public void clear()         { map.clear() ; }
    
    public boolean isEmpty()    { return map.isEmpty() ; }

    /** Does not materialise the contents */
    public Iterator<V> flatten()
    {
        IteratorConcat<V> all = new IteratorConcat<>() ;
        for ( K k : map.keySet() )        
        {
            Collection<V> x =  map.get(k) ;
            all.add(x.iterator()) ;
        }
        return all ;
    }
    
    @Override
    public boolean equals(Object other)
    {
        if ( this == other ) return true ;
{'fraction_non_alphanumeric': 0.11175616835994194, 'fraction_numerical': 0.0016126431220770843, 'mean_word_length': 2.298936170212766, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '6344826', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1817, 'n_words': 607}