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

org.jboss.weld.util.collections.Multimaps Maven / Gradle / Ivy

Go to download

This jar bundles all the bits of Weld and CDI required for running in a Servlet container.

There is a newer version: 6.0.0.Beta4
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed 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.jboss.weld.util.collections;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Multimap utilities.
 *
 * @author Jozef Hartinger
 *
 */
public class Multimaps {

    public static final Multimap EMPTY_MULTIMAP = new EmptyMultimap();

    private Multimaps() {
    }

    /**
     * Note that {@link Multimap#get(Object)} always returns unmodifiable collections. Moreover, it does not trigger initialization of a new value collection
     * (i.e. when no collection of values for a given key exists).
     *
     * @param multimap
     * @return an unmodifiable view of the given multimap
     */
    public static  Multimap unmodifiableMultimap(Multimap multimap) {
        if (multimap instanceof UnmodifiableMultimap) {
            return multimap;
        }
        return new UnmodifiableMultimap(multimap);
    }

    /**
     *
     * @return an immutable empty multimap
     */
    @SuppressWarnings("unchecked")
    public static  Multimap emptyMultimap() {
        return (Multimap) EMPTY_MULTIMAP;
    }

    /**
     *
     * @author Martin Kouba
     *
     * @param 
     * @param 
     */
    static class UnmodifiableMultimap implements Multimap {

        private final Multimap delegate;

        /**
         *
         * @param delegate
         */
        public UnmodifiableMultimap(Multimap delegate) {
            this.delegate = delegate;
        }

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

        @Override
        public boolean isEmpty() {
            return delegate.isEmpty();
        }

        /**
         * Returns unmodifiable collections. Moreover, it does not trigger initialization of a new value collection (i.e. when no collection of values for a
         * given key exists).
         */
        @Override
        public Collection get(K key) {
            if (delegate.containsKey(key)) {
                return unmodifiableValueCollection(delegate.get(key));
            }
            return Collections.emptyList();
        }

        @Override
        public boolean put(K key, V value) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean containsKey(Object key) {
            return delegate.containsKey(key);
        }

        @Override
        public Set keySet() {
            return delegate.keySet();
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }

        @Override
        public List values() {
            return delegate.values();
        }

        @Override
        public Set uniqueValues() {
            return delegate.uniqueValues();
        }

        @Override
        public boolean putAll(K key, Collection values) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection replaceValues(K key, Iterable values) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Set>> entrySet() {
            return delegate.entrySet();
        }

    }

    static class EmptyMultimap implements Multimap, Serializable {

        private static final long serialVersionUID = -7386055586552408792L;

        @Override
        public int size() {
            return 0;
        }

        @Override
        public boolean isEmpty() {
            return true;
        }

        @Override
        public Collection get(K key) {
            return Collections.emptyList();
        }

        @Override
        public boolean containsKey(Object key) {
            return false;
        }

        @Override
        public Set keySet() {
            return Collections.emptySet();
        }

        @Override
        public List values() {
            return Collections.emptyList();
        }

        @Override
        public Set uniqueValues() {
            return Collections.emptySet();
        }

        @Override
        public Set>> entrySet() {
            return Collections.emptySet();
        }

        @Override
        public boolean put(K key, V value) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean putAll(K key, Collection values) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection replaceValues(K key, Iterable values) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }

        public boolean equals(Object o) {
            return (o instanceof Multimap) && ((Multimap) o).isEmpty();
        }

        public int hashCode() {
            return 1;
        }

        private Object readResolve() {
            return EMPTY_MULTIMAP;
        }

    }

    static  Collection unmodifiableValueCollection(Collection values) {
        if (values instanceof Set) {
            return Collections.unmodifiableSet((Set) values);
        } else if (values instanceof List) {
            return Collections.unmodifiableList((List) values);
        }
        return Collections.unmodifiableCollection(values);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy