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