Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   * 
   *  http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.util;
 
 import java.util.Map;
 import java.util.Set;

An immutable map that could be built by convenient constructors.

Example of using map Builder:

 {
     @code
     Map<String, AttibuteValue> item = new ImmutableMapParameter.Builder<String, AttibuteValue>()
             .put("one", new AttibuteValue("1"))
             .put("two", new AttibuteValue("2"))
             .put("three", new AttibuteValue("3")).build();
 }
 
For small immutable maps (up to five entries), the ImmutableMapParamter.of() methods are preferred:
 Map<String, AttibuteValue> item =
       ImmutableMapParameter
           .of("one", new AttributeValue("1"), 
               "two", new AttributeValue("2"),
               "three", new AttributeValue("3"), 
 
 

Parameters:
<K> Class of the key for the map.
<V> Class of the value for the map.
 
 public class ImmutableMapParameter<K, V> implements Map<K, V> {
 
     private static final String UNMODIFIABLE_MESSAGE = "This is an immutable map.";
     private static final String DUPLICATED_KEY_MESSAGE = "Duplicate keys are provided.";
 
     private final Map<K, V> map;
 
     private ImmutableMapParameter(Map<K, V> map) {
         this. = map;
     }

    
Returns a new MapParameterBuilder instance.
 
     public static <K, V> Builder<K, V> builder() {
         return new Builder<K, V>();
     }

    
Returns an ImmutableMapParameter instance containing a single entry.

Parameters:
k0 Key of the single entry.
v0 Value of the single entry.
 
     public static <K, V> ImmutableMapParameter<K, V> of(K k0, V v0) {
         Map<K, V> map = Collections.singletonMap(k0v0);
         return new ImmutableMapParameter<K, V>(map);
     }

    
Returns an ImmutableMapParameter instance containing two entries.

Parameters:
k0 Key of the first entry.
v0 Value of the first entry.
k1 Key of the second entry.
v1 Value of the second entry.
 
     public static <K, V> ImmutableMapParameter<K, V> of(K k0, V v0, K k1, V v1) {
        Map<K, V> map = new HashMap<K, V>();
        putAndWarnDuplicateKeys(mapk0v0);
        putAndWarnDuplicateKeys(mapk1v1);
        return new ImmutableMapParameter<K, V>(map);
    }

    
Returns an ImmutableMapParameter instance containing three entries.

Parameters:
k0 Key of the first entry.
v0 Value of the first entry.
k1 Key of the second entry.
v1 Value of the second entry.
k2 Key of the third entry.
v2 Value of the third entry.
    public static <K, V> ImmutableMapParameter<K, V> of(K k0, V v0, K k1, V v1,
            K k2, V v2) {
        Map<K, V> map = new HashMap<K, V>();
        putAndWarnDuplicateKeys(mapk0v0);
        putAndWarnDuplicateKeys(mapk1v1);
        putAndWarnDuplicateKeys(mapk2v2);
        return new ImmutableMapParameter<K, V>(map);
    }

    
Returns an ImmutableMapParameter instance containing four entries.

Parameters:
k0 Key of the first entry.
v0 Value of the first entry.
k1 Key of the second entry.
v1 Value of the second entry.
k2 Key of the third entry.
v2 Value of the third entry.
k3 Key of the fourth entry.
v3 Value of the fourth entry.
    public static <K, V> ImmutableMapParameter<K, V> of(K k0, V v0, K k1, V v1,
            K k2, V v2, K k3, V v3) {
        Map<K, V> map = new HashMap<K, V>();
        putAndWarnDuplicateKeys(mapk0v0);
        putAndWarnDuplicateKeys(mapk1v1);
        putAndWarnDuplicateKeys(mapk2v2);
        putAndWarnDuplicateKeys(mapk3v3);
        return new ImmutableMapParameter<K, V>(map);
    }

    
Returns an ImmutableMapParameter instance containing five entries.

Parameters:
k0 Key of the first entry.
v0 Value of the first entry.
k1 Key of the second entry.
v1 Value of the second entry.
k2 Key of the third entry.
v2 Value of the third entry.
k3 Key of the fourth entry.
v3 Value of the fourth entry.
k4 Key of the fifth entry.
v4 Value of the fifth entry.
    public static <K, V> ImmutableMapParameter<K, V> of(K k0, V v0, K k1, V v1,
            K k2, V v2, K k3, V v3, K k4, V v4) {
        Map<K, V> map = new HashMap<K, V>();
        putAndWarnDuplicateKeys(mapk0v0);
        putAndWarnDuplicateKeys(mapk1v1);
        putAndWarnDuplicateKeys(mapk2v2);
        putAndWarnDuplicateKeys(mapk3v3);
        putAndWarnDuplicateKeys(mapk4v4);
        return new ImmutableMapParameter<K, V>(map);
    }

    
Inherited methods
    public boolean containsKey(Object key) {
        return .containsKey(key);
    }
    public boolean containsValue(Object value) {
        return .containsValue(value);
    }
    public Set<Entry<K, V>> entrySet() {
        return .entrySet();
    }
    public V get(Object key) {
        return .get(key);
    }
    public boolean isEmpty() {
        return .isEmpty();
    }
    public Set<K> keySet() {
        return .keySet();
    }
    public int size() {
        return .size();
    }
    public Collection<V> values() {
        return .values();
    }

    
Unsupported methods
    public void clear() {
    }
    public V put(K key, V value) {
    }
    public void putAll(Map<? extends K, ? extends V> map) {
    }
    public V remove(Object key) {
    }
    private static <K, V> void putAndWarnDuplicateKeys(Map<K, V> map, K key,
            V value) {
        if (map.containsKey(key))
            throw new IllegalArgumentException();
        map.put(keyvalue);
    }
    @Override
    public boolean equals(Object o) {
        return .equals(o);
    }
    @Override
    public int hashCode() {
        return .hashCode();
    }
    @Override
    public String toString() {
        return .toString();
    }

    
A convenient builder for creating ImmutableMapParameter instances.
    public static class Builder<K, V> {
        private final Map<K, V> entries;
        public Builder() {
            this. = new HashMap<K, V>();
        }

        
Add a key-value pair into the built map. The method will throw IllegalArgumentException immediately when duplicate keys are provided.

Returns:
Returns a reference to this object so that method calls can be chained together.
        public Builder<K, V> put(K key, V value) {
            putAndWarnDuplicateKeys(keyvalue);
            return this;
        }

        
Generates and returns a new ImmutableMapParameter instance which contains all the entries added into the Builder by put() method.
        public ImmutableMapParameter<K, V> build() {
            HashMap<K, V> builtMap = new HashMap<K, V>();
            builtMap.putAll();
            return new ImmutableMapParameter<K, V>(builtMap);
        }
    }
New to GrepCode? Check out our FAQ X