001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.util; 018 019import static org.apache.juneau.commons.utils.AssertionUtils.*; 020import static org.apache.juneau.commons.utils.CollectionUtils.*; 021import static org.apache.juneau.commons.utils.Utils.*; 022 023import java.util.*; 024 025/** 026 * A list of default implementation classes. 027 * 028 * <h5 class='section'>Notes:</h5><ul> 029 * <li class='warn'>This class is not thread safe. 030 * </ul> 031 * 032 */ 033public class DefaultClassList { 034 /** 035 * Static creator. 036 * 037 * @return A new object. 038 */ 039 public static DefaultClassList create() { 040 return new DefaultClassList(); 041 } 042 043 /** 044 * Static creator. 045 * 046 * @param values Initial entries in this list. 047 * @return A new object initialized with the specified values. 048 */ 049 public static DefaultClassList of(Class<?>...values) { 050 return new DefaultClassList().add(values); 051 } 052 053 private final List<Class<?>> entries; 054 055 /** 056 * Copy constructor 057 * 058 * @param value The object to copy. 059 */ 060 public DefaultClassList(DefaultClassList value) { 061 entries = copyOf(value.entries); 062 } 063 064 /** 065 * Constructor. 066 */ 067 protected DefaultClassList() { 068 entries = list(); 069 } 070 071 /** 072 * Prepends the specified values to the beginning of this list. 073 * 074 * @param values The values to prepend to this list. 075 * @return This object. 076 */ 077 public DefaultClassList add(Class<?>...values) { 078 prependAll(entries, values); 079 return this; 080 } 081 082 /** 083 * Creates a copy of this list. 084 * 085 * @return A copy of this list. 086 */ 087 public DefaultClassList copy() { 088 return new DefaultClassList(this); 089 } 090 091 /** 092 * Returns the first class in this list which is a subclass of (or same as) the specified type. 093 * 094 * @param <T> The parent type. 095 * @param type The parent type to check for. 096 * @return The first class in this list which is a subclass of the specified type. 097 */ 098 @SuppressWarnings("unchecked") 099 public <T> Optional<Class<? extends T>> get(Class<T> type) { 100 assertArgNotNull("type", type); 101 for (var e : entries) 102 if (nn(e) && type.isAssignableFrom(e)) 103 return opt((Class<? extends T>)e); 104 return opte(); 105 } 106}