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

com.github.fge.uritemplate.vars.values.MapValue Maven / Gradle / Ivy

/*
 * Copyright (c) 2014, Francis Galiegue ([email protected])
 *
 * This software is dual-licensed under:
 *
 * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
 *   later version;
 * - the Apache Software License (ASL) version 2.0.
 *
 * The text of this file and of both licenses is available at the root of this
 * project or, if you have the jar distribution, in directory META-INF/, under
 * the names LGPL-3.0.txt and ASL-2.0.txt respectively.
 *
 * Direct link to the sources:
 *
 * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
 * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
 */

package com.github.fge.uritemplate.vars.values;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import java.util.Map;

/**
 * Map variable value
 *
 * 

Note that some methods allow to pass values of arbitrary type. It is the * caller's responsibility to ensure that these values have a correct {@link * Object#toString() .toString()} implementation.

* *

Also note that null keys or values are not accepted.

* *

While there is one public constructor, it is deprecated. Use {@link * #copyOf(Map)} instead, or for more control, use a {@link Builder} (see * {@link #newBuilder()}).

*/ @Immutable public final class MapValue extends VariableValue { private final Map map; private MapValue(final Builder builder) { super(ValueType.MAP); map = ImmutableMap.copyOf(builder.map); } /** * Create a new builder for this class * * @return a {@link Builder} */ public static Builder newBuilder() { return new Builder(); } /** * Convenience method to build a variable value from an existing {@link Map} * * @param map the map * @param the type of values in this map * @return a new map value as a {@link VariableValue} * @throws NullPointerException map is null, or one of its keys or values * is null */ public static VariableValue copyOf(final Map map) { return newBuilder().putAll(map).build(); } @Override public Map getMapValue() { // Safe: this is an ImmutableMap return map; } @Override public boolean isEmpty() { return map.isEmpty(); } /** * Builder class for a {@link MapValue} */ @NotThreadSafe public static final class Builder { /* * We use a LinkedHashMap to respect insertion order. While not required * by URIs, it is nicer to the user. And Guava's ImmutableMap respects * insertion order as well. */ private final Map map = Maps.newLinkedHashMap(); private Builder() { } /** * Add one key/value pair to the map * * @param key the key * @param value the value * @param the type of the value * @return this * @throws NullPointerException the key or value is null */ public Builder put(final String key, final T value) { map.put( BUNDLE.checkNotNull(key, "mapValue.nullKey"), BUNDLE.checkNotNull(value, "mapValue.nullValue").toString() ); return this; } /** * Inject a map of key/value pairs * * @param map the map * @param the type of this map's values * @return this * @throws NullPointerException map is null, or one of its keys or * values is null */ public Builder putAll(final Map map) { BUNDLE.checkNotNull(map, "mapValue.nullMap"); for (final Map.Entry entry: map.entrySet()) put(entry.getKey(), entry.getValue()); return this; } /** * Build the value * * @return the map value as a {@link VariableValue} */ public VariableValue build() { return new MapValue(this); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy