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 */ 017 package org.apache.commons.collections.list; 018 019 import java.util.List; 020 021 import org.apache.commons.collections.functors.InstanceofPredicate; 022 023 /** 024 * Decorates another <code>List</code> to validate that elements 025 * added are of a specific type. 026 * <p> 027 * The validation of additions is performed via an instanceof test against 028 * a specified <code>Class</code>. If an object cannot be added to the 029 * collection, an IllegalArgumentException is thrown. 030 * 031 * @since Commons Collections 3.0 032 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 033 * 034 * @author Stephen Colebourne 035 * @author Matthew Hawthorne 036 */ 037 public class TypedList { 038 039 /** 040 * Factory method to create a typed list. 041 * <p> 042 * If there are any elements already in the list being decorated, they 043 * are validated. 044 * 045 * @param list the list to decorate, must not be null 046 * @param type the type to allow into the collection, must not be null 047 * @throws IllegalArgumentException if list or type is null 048 * @throws IllegalArgumentException if the list contains invalid elements 049 */ 050 public static List decorate(List list, Class type) { 051 return new PredicatedList(list, InstanceofPredicate.getInstance(type)); 052 } 053 054 /** 055 * Restrictive constructor. 056 */ 057 protected TypedList() { 058 } 059 060 }