001// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
002// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
003// * with the License.  You may obtain a copy of the License at                                                              *
004// *                                                                                                                         *
005// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
006// *                                                                                                                         *
007// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
008// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
009// * specific language governing permissions and limitations under the License.                                              *
010// ***************************************************************************************************************************
011package org.apache.juneau.collections;
012
013import static java.util.Collections.*;
014
015import java.lang.reflect.*;
016import java.util.*;
017
018import org.apache.juneau.json.*;
019import org.apache.juneau.serializer.*;
020
021/**
022 * A fluent {@link TreeSet}.
023 *
024 * <p>
025 * Provides various convenience methods for creating and populating a sorted set with minimal code.
026 *
027 * <h5 class='figure'>Examples:</h5>
028 * <p class='bcode w800'>
029 *    <jc>// A set of strings.</jc>
030 *    ASortedSet&lt;String&gt; s = ASortedSet.<jsm>of</jsm>(<js>"foo"</js>,<js>"bar"</js>);
031 *
032 *    <jc>// Append to set.</jc>
033 *    s.a(<js>"baz"</js>).a(<js>"qux"</js>);
034 *
035 *    <jc>// Create an unmodifiable view of this set.</jc>
036 *    Set&lt;String&gt; s2 = s.unmodifiable();
037 *
038 *    <jc>// Convert it to an array.</jc>
039 *    String[] array = s.asArrayOf(String.<jk>class</jk>);
040 *
041 *    <jc>// Convert to simplified JSON.</jc>
042 *    String json = s.asString();
043 *
044 *    <jc>// Convert to XML.</jc>
045 *    String json = s.asString(XmlSerializer.<jsf>DEFAULT</jsm>);
046 * </p>
047 *
048 * @param <T> The entry type.
049 */
050@SuppressWarnings({"unchecked"})
051public final class ASortedSet<T> extends TreeSet<T> {
052
053   private static final long serialVersionUID = 1L;
054
055   //------------------------------------------------------------------------------------------------------------------
056   // Constructors.
057   //------------------------------------------------------------------------------------------------------------------
058
059   /**
060    * Constructor.
061    */
062   public ASortedSet() {}
063
064   /**
065    * Constructor.
066    *
067    * @param c Comparator.
068    */
069   public ASortedSet(Comparator<T> c) {
070      super(c);
071   }
072
073   /**
074    * Copy constructor.
075    *
076    * @param c Initial contents.  Can be <jk>null</jk>.
077    */
078   public ASortedSet(Collection<T> c) {
079      super(c == null ? emptySet() : c);
080   }
081
082   //------------------------------------------------------------------------------------------------------------------
083   // Creators.
084   //------------------------------------------------------------------------------------------------------------------
085
086   /**
087    * Convenience method for creating a list of objects.
088    *
089    * @param t The initial values.
090    * @return A new list.
091    */
092   @SafeVarargs
093   public static <T> ASortedSet<T> of(T...t) {
094      return new ASortedSet<T>().a(t);
095   }
096
097   //------------------------------------------------------------------------------------------------------------------
098   // Appenders.
099   //------------------------------------------------------------------------------------------------------------------
100
101   /**
102    * Add.
103    *
104    * <p>
105    * Adds an entry to this set.
106    *
107    * @param t The entry to add to this set.
108    * @return This object (for method chaining).
109    */
110   public ASortedSet<T> a(T t) {
111      add(t);
112      return this;
113   }
114
115   /**
116    * Add.
117    *
118    * <p>
119    * Adds multiple entries to this set.
120    *
121    * @param t The entries to add to this set.
122    * @return This object (for method chaining).
123    */
124   public ASortedSet<T> a(T...t) {
125      Collections.addAll(this, t);
126      return this;
127   }
128
129   /**
130    * Add all.
131    *
132    * <p>
133    * Adds multiple entries to this set.
134    *
135    * @param c The entries to add to this set.
136    * @return This object (for method chaining).
137    */
138   public ASortedSet<T> aa(Collection<T> c) {
139      if (c != null)
140         addAll(c);
141      return this;
142   }
143
144   /**
145    * Add if.
146    *
147    * <p>
148    * Adds a value to this set if the boolean value is <jk>true</jk>
149    *
150    * @param b The boolean value.
151    * @param t The value to add.
152    * @return This object (for method chaining).
153    */
154   public ASortedSet<T> aif(boolean b, T t) {
155      if (b)
156         a(t);
157      return this;
158   }
159
160   /**
161    * Add if not null.
162    *
163    * <p>
164    * Adds entries to this set skipping <jk>null</jk> values.
165    *
166    * @param t The objects to add to the list.
167    * @return This object (for method chaining).
168    */
169   public ASortedSet<T> aifnn(T...t) {
170      for (T o2 : t)
171         if (o2 != null)
172            a(o2);
173      return this;
174   }
175
176   //------------------------------------------------------------------------------------------------------------------
177   // Other methods.
178   //------------------------------------------------------------------------------------------------------------------
179
180   /**
181    * Returns an unmodifiable view of this set.
182    *
183    * @return An unmodifiable view of this set.
184    */
185   public SortedSet<T> unmodifiable() {
186      return isEmpty() ? emptySortedSet() : unmodifiableSortedSet(this);
187   }
188
189   /**
190    * Convert the contents of this set into a new array.
191    *
192    * @param c The component type of the array.
193    * @return A new array.
194    */
195   public <T2> T2[] asArrayOf(Class<T2> c) {
196      return toArray((T2[])Array.newInstance(c, size()));
197   }
198
199   /**
200    * Convert to a string using the specified serializer.
201    *
202    * @param ws The serializer to use to serialize this collection.
203    * @return This collection serialized to a string.
204    */
205   public String asString(WriterSerializer ws) {
206      return ws.toString(this);
207   }
208
209   /**
210    * Convert to Simplified JSON.
211    *
212    * @return This collection serialized to a string.
213    */
214   public String asString() {
215      return SimpleJsonSerializer.DEFAULT.toString(this);
216   }
217
218   /**
219    * Convert to Simplified JSON.
220    */
221   @Override /* Object */
222   public String toString() {
223      return asString(SimpleJsonSerializer.DEFAULT);
224   }
225}