All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.surefire.util.internal.ImmutableMap Maven / Gradle / Ivy

The newest version!
package org.apache.maven.surefire.util.internal;

/*
 * 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.
 */

import java.util.AbstractMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.unmodifiableSet;

/**
 * Copies input map in {@link #ImmutableMap(Map) constructor}, and Entries are linked and thread-safe.
 * The map is immutable with linear list of entries.
 *
 * @param  key
 * @param  value
 * @since 2.20
 */
public final class ImmutableMap
        extends AbstractMap
{
    private final Node first;

    public ImmutableMap( Map map )
    {
        Node first = null;
        Node previous = null;
        for ( Entry e : map.entrySet() )
        {
            Node node = new Node( e.getKey(), e.getValue() );
            if ( first == null )
            {
                first = node;
            }
            else
            {
                previous.next = node;
            }
            previous = node;
        }
        this.first = first;
    }

    @Override
    public Set> entrySet()
    {
        Set> entries = new LinkedHashSet>();
        Node node = first;
        while ( node != null )
        {
            entries.add( node );
            node = node.next;
        }
        return unmodifiableSet( entries );
    }

    static final class Node
            implements Entry
    {
        final K key;
        final V value;
        volatile Node next;

        Node( K key, V value )
        {
            this.key = key;
            this.value = value;
        }

        @Override
        public K getKey()
        {
            return key;
        }

        @Override
        public V getValue()
        {
            return value;
        }

        @Override
        public V setValue( V value )
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean equals( Object o )
        {
            if ( this == o )
            {
                return true;
            }

            if ( o == null || getClass() != o.getClass() )
            {
                return false;
            }

            Node node = (Node) o;

            return getKey() != null ? getKey().equals( node.getKey() ) : node.getKey() == null
                           && getValue() != null ? getValue().equals( node.getValue() ) : node.getValue() == null;

        }

        @Override
        public int hashCode()
        {
            int result = getKey() != null ? getKey().hashCode() : 0;
            result = 31 * result + ( getValue() != null ? getValue().hashCode() : 0 );
            return result;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy