001package com.nimbusds.oauth2.sdk.id;
002
003
004import java.net.URI;
005import java.util.*;
006
007import net.jcip.annotations.Immutable;
008
009
010/**
011 * Audience identifier.
012 *
013 * <p>Provides helper methods for:
014 *
015 * <ul>
016 *     <li>Converting to / from string arrays and collections
017 *     <li>Matching audience values
018 * </ul>
019 */
020@Immutable
021public final class Audience extends Identifier {
022
023
024        /**
025         * Creates a new audience identifier with the specified value.
026         *
027         * @param value The audience identifier value. Must not be {@code null}
028         *              or empty string.
029         */
030        public Audience(final String value) {
031
032                super(value);
033        }
034
035
036        /**
037         * Creates a new audience identifier with the specified URI value.
038         *
039         * @param value The URI value. Must not be {@code null}.
040         */
041        public Audience(final URI value) {
042
043                super(value.toString());
044        }
045
046
047        /**
048         * Creates a new audience identifier with the specified value.
049         *
050         * @param value The value. Must not be {@code null}.
051         */
052        public Audience(final Identifier value) {
053
054                super(value.getValue());
055        }
056
057
058        /**
059         * Returns a singleton list of this audience.
060         *
061         * @return A singleton list consisting of this audience only.
062         */
063        public List<Audience> toSingleAudienceList() {
064
065                List<Audience> audienceList = new ArrayList<>(1);
066                audienceList.add(this);
067                return audienceList;
068        }
069
070
071        @Override
072        public boolean equals(final Object object) {
073        
074                return object instanceof Audience &&
075                       this.toString().equals(object.toString());
076        }
077
078
079        /**
080         * Returns a string list representation of the specified audience.
081         *
082         * @param audience The audience. May be {@code null}.
083         *
084         * @return The string list, {@code null} if the argument was
085         *         {@code null}.
086         */
087        public static List<String> toStringList(final Audience audience) {
088
089                if (audience == null) {
090                        return null;
091                }
092                return Collections.singletonList(audience.getValue());
093        }
094
095
096        /**
097         * Returns a string list representation of the specified audience list.
098         *
099         * @param audienceList The audience list. May be {@code null}.
100         *
101         * @return The string list, {@code null} if the argument was
102         *         {@code null}.
103         */
104        public static List<String> toStringList(final List<Audience> audienceList) {
105
106                if (audienceList == null) {
107                        return null;
108                }
109
110                List<String> list = new ArrayList<>(audienceList.size());
111                for (Audience aud: audienceList) {
112                        list.add(aud.getValue());
113                }
114                return list;
115        }
116
117
118        /**
119         * Creates an audience list from the specified string list
120         * representation.
121         *
122         * @param strings The string list. May be {@code null}.
123         *
124         * @return The audience list, {@code null} if the argument was
125         *         {@code null}.
126         */
127        public static List<Audience> create(final List<String> strings) {
128
129                if (strings == null) {
130                        return null;
131                }
132
133                List<Audience> audienceList = new ArrayList<>(strings.size());
134
135                for (String s: strings) {
136                        audienceList.add(new Audience(s));
137                }
138                return audienceList;
139        }
140
141
142        /**
143         * Creates an audience list from the specified string array.
144         *
145         * @param strings The strings. May be {@code null}.
146         *
147         * @return The audience list, {@code null} if the argument was
148         *         {@code null}.
149         */
150        public static List<Audience> create(final String ... strings) {
151
152                if (strings == null) {
153                        return null;
154                }
155
156                return create(Arrays.asList(strings));
157        }
158
159
160        /**
161         * Returns {@code true} if the specified collections have at at least
162         * one matching audience value.
163         *
164         * @param c1 The first audience collection. May be {@code null}.
165         * @param c2 The second audience collection. May be {@code null}.
166         *
167         * @return {@code true} if the specified collections have at at least
168         *         one matching audience value, {@code false} if there are no
169         *         matches or either collection is {@code null} or empty.
170         */
171        public static boolean matchesAny(final Collection<Audience> c1, final Collection<Audience> c2) {
172
173                if (c1 == null || c2 == null) {
174                        return false;
175                }
176
177                for (Audience aud: c1) {
178                        if (c2.contains(aud)) {
179                                return true;
180                        }
181                }
182
183                return false;
184        }
185}