com.googlecode.kevinarpe.papaya.container.ContainerFactory Maven / Gradle / Ivy
package com.googlecode.kevinarpe.papaya.container;
/*
* #%L
* This file is part of Papaya.
* %%
* Copyright (C) 2013 - 2014 Kevin Connor ARPE ([email protected])
* %%
* Papaya is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GPL Classpath Exception:
* This project is subject to the "Classpath" exception as provided in
* the LICENSE file that accompanied this code.
*
* Papaya is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Papaya. If not, see .
* #L%
*/
/**
* Allows for mocking of {@link Class#newInstance()} for containers. As of writing, it is not
* possible to directly mock {@link Class#newInstance()}, so this interface may be used as a
* substitute.
*
* Since containers are a synthesis of types: a parent (container) and child (element), two types
* are needed. However, due to restrictions in the Java typing system, it is not possible to
* statically specify a class object having an inner type. For example,
* {@code ArrayList.class} is not allowed. Instead, {@code ArrayList.class} is used. To
* instantiate an instance of {@code ArrayList} at runtime only from a
* {@code Class>} reference, the result must be cast from the raw type {@code ArrayList} to
* {@code ArrayList}. Finally, there is risk for a {@link ClassCastException} to be thrown.
*
* This interface is inherently flawed and show be used with great care.
*
* @author Kevin Connor ARPE ([email protected])
*/
public interface ContainerFactory {
/**
* Calls {@link Class#newInstance()} and casts result.
*
* @param containerClass
* class object used to instantiate, e.g., {@code ArrayList.class}.
* Must not be {@code null}.
* @param
* return type for cast, e.g., {@code ArrayList}
*
* @return new instance of class, cast to expected type
*
* @throws NullPointerException
* if {@code containerClass} is {@code null}
* @throws IllegalAccessException
* may be thrown directly by {@link Class#newInstance()}
* @throws InstantiationException
* may be thrown directly by {@link Class#newInstance()}
* @throws ClassCastException
* if cast to type {@code TContainerWithElementType} fails for result of
* {@link Class#newInstance()}.
*/
TContainerWithElementType newInstance(Class> containerClass)
throws IllegalAccessException, InstantiationException, ClassCastException;
}