001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.utils; 014 015import java.util.*; 016 017/** 018 * An extension of {@link LinkedHashSet} with a convenience {@link #append(Object)} method. 019 * 020 * <p> 021 * Primarily used for testing purposes for quickly creating populated sets. 022 * <p class='bcode w800'> 023 * <jc>// Example:</jc> 024 * Set<String> s = <jk>new</jk> ASet<String>().append(<js>"foo"</js>).append(<js>"bar"</js>); 025 * </p> 026 * 027 * @param <T> The entry type. 028 */ 029@SuppressWarnings({"unchecked"}) 030public final class ASet<T> extends LinkedHashSet<T> { 031 032 private static final long serialVersionUID = 1L; 033 034 /** 035 * Convenience method for creating a list of objects. 036 * 037 * @param t The initial values. 038 * @return A new list. 039 */ 040 @SafeVarargs 041 public static <T> ASet<T> create(T...t) { 042 return new ASet<T>().appendAll(t); 043 } 044 045 /** 046 * Convenience method for creating a list of objects. 047 * 048 * <p> 049 * Identical to {@link #create(Object...)}. 050 * 051 * @param t The initial values. 052 * @return A new list. 053 */ 054 @SafeVarargs 055 public static <T> ASet<T> of(T...t) { 056 return new ASet<T>().appendAll(t); 057 } 058 059 /** 060 * Adds an entry to this set. 061 * 062 * @param t The entry to add to this set. 063 * @return This object (for method chaining). 064 */ 065 public ASet<T> append(T t) { 066 add(t); 067 return this; 068 } 069 070 /** 071 * Adds multiple entries to this set. 072 * 073 * @param t The entries to add to this set. 074 * @return This object (for method chaining). 075 */ 076 public ASet<T> appendAll(T...t) { 077 addAll(Arrays.asList(t)); 078 return this; 079 } 080 081 /** 082 * Adds a value to this set if the boolean value is <jk>true</jk> 083 * 084 * @param b The boolean value. 085 * @param t The value to add. 086 * @return This object (for method chaining). 087 */ 088 public ASet<T> appendIf(boolean b, T t) { 089 if (b) 090 append(t); 091 return this; 092 } 093}