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

net.digitalid.utility.immutable.ImmutableMap Maven / Gradle / Ivy

The newest version!
package net.digitalid.utility.immutable;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import net.digitalid.utility.annotations.method.Impure;
import net.digitalid.utility.annotations.method.Pure;
import net.digitalid.utility.annotations.ownership.Capturable;
import net.digitalid.utility.annotations.ownership.Captured;
import net.digitalid.utility.annotations.ownership.NonCapturable;
import net.digitalid.utility.annotations.ownership.NonCaptured;
import net.digitalid.utility.annotations.parameter.Unmodified;
import net.digitalid.utility.annotations.state.Unmodifiable;
import net.digitalid.utility.immutable.entry.ReadOnlyEntrySet;
import net.digitalid.utility.validation.annotations.method.Chainable;
import net.digitalid.utility.validation.annotations.type.Immutable;
import net.digitalid.utility.validation.annotations.type.Mutable;

/**
 * This class implements an immutable map.
 */
@Immutable
public class ImmutableMap extends LinkedHashMap {
    
    /* -------------------------------------------------- Constructors -------------------------------------------------- */
    
    protected ImmutableMap(@NonCaptured @Unmodified @Nonnull Map map) {
        super(map);
    }
    
    /**
     * Returns an immutable map with the mappings of the given map or null if the given map is null.
     * The given map is not captured as its keys and values are copied to the immutable map.
     */
    @Pure
    public static  ImmutableMap withMappingsOf(@NonCaptured @Unmodified Map map) {
        return map == null ? null : new ImmutableMap<>(map);
    }
    
    /* -------------------------------------------------- Modified Operations -------------------------------------------------- */
    
    @Pure
    @Override
    public final @Unmodifiable @Nonnull Set keySet() {
        return Collections.unmodifiableSet(super.keySet());
    }
    
    @Pure
    @Override
    public final @Unmodifiable @Nonnull Collection values() {
        return Collections.unmodifiableCollection(super.values());
    }
    
    @Pure
    @Override
    public final @Nonnull ReadOnlyEntrySet entrySet() {
        return ReadOnlyEntrySet.with(super.entrySet());
    }
    
    /* -------------------------------------------------- Unsupported Operations -------------------------------------------------- */
    
    @Pure
    @Override
    public final @Capturable V put(@Captured K key, @Captured V value) {
        throw new UnsupportedOperationException();
    }
    
    @Pure
    @Override
    public final @Capturable V remove(@NonCaptured @Unmodified @Nullable Object key) {
        throw new UnsupportedOperationException();
    }
    
    @Pure
    @Override
    public final void putAll(@NonCaptured @Unmodified @Nonnull Map map) {
        throw new UnsupportedOperationException();
    }
    
    @Pure
    @Override
    public final void clear() {
        throw new UnsupportedOperationException();
    }
    
    /* -------------------------------------------------- Builder -------------------------------------------------- */
    
    /**
     * This class implements a builder for the immutable map.
     */
    @Mutable
    public static class ImmutableMapBuilder extends LinkedHashMap {
        
        /**
         * Adds the given key-value pair to this builder and returns itself.
         */
        @Impure
        @Chainable
        public @NonCapturable @Nonnull ImmutableMapBuilder with(@Captured K key, @Captured V value) {
            put(key, value);
            return this;
        }
        
        /**
         * Returns an immutable map with the key-value pairs of this builder.
         */
        @Pure
        public @Nonnull ImmutableMap build() {
            return ImmutableMap.withMappingsOf(this);
        }
        
    }
    
    /**
     * Returns an immutable map builder with the given key-value pair.
     */
    @Pure
    public static @Capturable  @Nonnull ImmutableMapBuilder with(@Captured K key, @Captured V value) {
        return new ImmutableMapBuilder().with(key, value);
    }
    
    /**
     * Returns an immutable map with no entries in it.
     */
    @Pure
    public static  @Nonnull ImmutableMap withNoEntries() {
        return new ImmutableMapBuilder().build();
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy