001package com.nimbusds.openid.connect.provider.spi.claims;
002
003
004import java.util.HashSet;
005import java.util.List;
006import java.util.Set;
007
008import com.nimbusds.langtag.LangTag;
009
010
011/**
012 * Claim utilities.
013 */
014public class ClaimUtils {
015
016
017        /**
018         * Applies the specified language tags to a set of claims.
019         *
020         * <p>Example:
021         *
022         * <pre>
023         * Claims: "name", "given_name", "family_name"
024         * Language tags: "bg-BG", "en-US"
025         * Result: "name#bg-BG", "name#en-US",
026         *         "given_name#bg-BG", "given_name#en-US",
027         *         "family_name#bg-BG", "family_name#en-US"
028         * </pre>
029         *
030         * @param claims   The claims to apply the language tags to. Claims
031         *                 that already have a language will be returned
032         *                 unmodified. Must not be {@code null}.
033         * @param langTags The language tags to apply, {@code null} if not
034         *                 specified.
035         *
036         * @return The claims with applied language tags, or the original
037         *         claims if no language tags are specified.
038         */
039        public static Set<String> applyLangTags(final Set<String> claims, final List<LangTag> langTags) {
040
041                if (langTags == null || langTags.isEmpty())
042                        return claims;
043
044                Set<String> out = new HashSet<>();
045
046                for (String claim: claims) {
047
048                        for (LangTag tag: langTags) {
049
050                                if (claim.contains("#")) {
051                                        out.add(claim);
052                                } else {
053                                        out.add(claim + "#" + tag);
054                                }
055                        }
056                }
057
058                return out;
059        }
060
061        /**
062         * Prevents public instantiation.
063         */
064        private ClaimUtils() {}
065}