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

org.apache.felix.framework.util.ImmutableMap Maven / Gradle / Ivy

There is a newer version: 8.1.2
Show newest version
/*
 * 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.util;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class ImmutableMap extends AbstractMap
{
    final Entry[] entries;

    public static  ImmutableMap newInstance(Entry... entries)
    {
        return new ImmutableMap(entries);
    }

    public static  ImmutableMap newInstance(Map entries)
    {
        if (entries instanceof ImmutableMap)
        {
            return (ImmutableMap) entries;
        }
        else
        {
            return new ImmutableMap(entries);
        }
    }

    protected ImmutableMap(Entry[] entries)
    {
        this.entries = entries.clone();
    }

    protected ImmutableMap(Map map)
    {
        this.entries = map.entrySet().toArray(new Entry[map.size()]);
    }

    @Override
    public V get(Object key)
    {
        if (key == null)
        {
            for (int i = 0; i < entries.length; i++)
            {
                if (entries[i].getKey() == null)
                {
                    return entries[i].getValue();
                }
            }
        }
        else
        {
            for (int i = 0; i < entries.length; i++)
            {
                if (key.equals(entries[i].getKey()))
                {
                    return entries[i].getValue();
                }
            }
        }
        return null;
    }

    @Override
    public Set> entrySet()
    {
        return new EntrySet();
    }

    private class EntrySet extends AbstractSet>
    {
        @Override
        public Iterator> iterator()
        {
            return new EntryItr(0);
        }

        @Override
        public int size()
        {
            return entries.length;
        }
    }

    private class EntryItr implements Iterator>
    {
        int cursor;

        private EntryItr(int cursor)
        {
            this.cursor = cursor;
        }

        public boolean hasNext()
        {
            return cursor != size();
        }

        public Entry next()
        {
            return entries[cursor++];
        }

        public void remove()
        {
            throw new UnsupportedOperationException();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy