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.cp; 014 015import static org.apache.juneau.common.internal.ArgUtils.*; 016import static org.apache.juneau.internal.CollectionUtils.*; 017 018import java.util.*; 019 020/** 021 * A list of default implementation classes. 022 * 023 * <h5 class='section'>Notes:</h5><ul> 024 * <li class='warn'>This class is not thread safe. 025 * </ul> 026 * 027 * <h5 class='section'>See Also:</h5><ul> 028 * </ul> 029 */ 030public class DefaultClassList { 031 032 //----------------------------------------------------------------------------------------------------------------- 033 // Static 034 //----------------------------------------------------------------------------------------------------------------- 035 036 /** 037 * Static creator. 038 * 039 * @return A new object. 040 */ 041 public static DefaultClassList create() { 042 return new DefaultClassList(); 043 } 044 045 /** 046 * Static creator. 047 * 048 * @param values Initial entries in this list. 049 * @return A new object initialized with the specified values. 050 */ 051 public static DefaultClassList of(Class<?>...values) { 052 return new DefaultClassList().add(values); 053 } 054 055 //----------------------------------------------------------------------------------------------------------------- 056 // Instance 057 //----------------------------------------------------------------------------------------------------------------- 058 059 private final List<Class<?>> entries; 060 061 /** 062 * Constructor. 063 */ 064 protected DefaultClassList() { 065 entries = list(); 066 } 067 068 /** 069 * Copy constructor 070 * 071 * @param value The object to copy. 072 */ 073 public DefaultClassList(DefaultClassList value) { 074 entries = copyOf(value.entries); 075 } 076 077 /** 078 * Prepends the specified values to the beginning of this list. 079 * 080 * @param values The values to prepend to this list. 081 * @return This object. 082 */ 083 public DefaultClassList add(Class<?>...values) { 084 prependAll(entries, values); 085 return this; 086 } 087 088 /** 089 * Returns the first class in this list which is a subclass of (or same as) the specified type. 090 * 091 * @param <T> The parent type. 092 * @param type The parent type to check for. 093 * @return The first class in this list which is a subclass of the specified type. 094 */ 095 @SuppressWarnings("unchecked") 096 public <T> Optional<Class<? extends T>> get(Class<T> type) { 097 assertArgNotNull("type", type); 098 for (Class<?> e : entries) 099 if (e != null && type.isAssignableFrom(e)) 100 return optional((Class<? extends T>)e); 101 return empty(); 102 } 103 104 /** 105 * Creates a copy of this list. 106 * 107 * @return A copy of this list. 108 */ 109 public DefaultClassList copy() { 110 return new DefaultClassList(this); 111 } 112}