com.nimbusds.oauth2.sdk.util.MultivaluedMapUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oauth2-oidc-sdk Show documentation
Show all versions of oauth2-oidc-sdk Show documentation
OAuth 2.0 SDK with OpenID Connection extensions for developing client
and server applications.
/*
* oauth2-oidc-sdk
*
* Copyright 2012-2016, Connect2id Ltd and 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 com.nimbusds.oauth2.sdk.util;
import java.util.*;
/**
* Multi-valued map utilities.
*/
public final class MultivaluedMapUtils {
/**
* Converts the specified multi-valued map to a single-valued map by
* taking the first value in the list.
*
* @param map The multi-valued map, {@code null} if not specified.
*
* @return The single-valued map, {@code null} if no map was specified.
*/
public static Map toSingleValuedMap(final Map> map) {
if (map == null)
return null;
Map out = new HashMap<>();
for (Map.Entry> en: map.entrySet()) {
if (en.getValue() == null || en.getValue().isEmpty()) {
out.put(en.getKey(), null);
} else {
out.put(en.getKey(), en.getValue().get(0));
}
}
return out;
}
/**
* Gets the first value for the specified key.
*
* @param map The multi-valued map. Must not be {@code null}.
* @param key The key. Must not be {@code null}.
*
* @return The first value, {@code null} if not set.
*/
public static V getFirstValue(final Map> map, final K key) {
List valueList = map.get(key);
if (valueList == null || valueList.isEmpty()) {
return null;
}
return valueList.get(0);
}
/**
* Removes the entry for the specified key and returns its first value.
*
* @param map The multi-valued map. Must not be {@code null}.
* @param key The key. Must not be {@code null}.
*
* @return The first value, {@code null} if not set.
*/
public static V removeAndReturnFirstValue(final Map> map, final String key) {
List valueList = map.remove(key);
if (valueList == null || valueList.isEmpty()) {
return null;
}
return valueList.get(0);
}
/**
* Returns the keys with more than one value.
*
* @param map The multi-valued map, {@code null} if not specified.
* @param excepted The excepted keys, {@code null} or empty if none.
*
* @return The keys with more than one value, empty set if none.
*/
public static Set getKeysWithMoreThanOneValue(final Map> map, final Set excepted) {
if (map == null || map.isEmpty()) {
return Collections.emptySet();
}
Set found = new HashSet<>();
for (Map.Entry> en: map.entrySet()) {
if (CollectionUtils.contains(excepted, en.getKey())) {
continue;
}
if (en.getValue() != null && en.getValue().size() > 1) {
found.add(en.getKey());
}
}
return found;
}
/**
* Prevents public instantiation.
*/
private MultivaluedMapUtils() {}
}