package graph;

import java.io.InputStream;

/**
 * Created by Paweł Kopeć on 26.12.16.
 *
 * Class for automation of Graph subclasses constructing
 *
 *TODO
 * Think of some meta-programming to get rid of ugly repeating
 * of if-else and switch statements in different getInstance(...).
 */
public class GraphFactory {

    public enum GRAPH_TYPES { LIST_GRAPH, MATRIX_GRAPH }

    public static Graph getInstance(GRAPH_TYPES type, int verticesNumber) {
        switch (type) {
            case LIST_GRAPH:
                return new ListGraph(verticesNumber);
            case MATRIX_GRAPH:
                return new MatrixGraph(verticesNumber);
            default:
                return null;
        }
    }

    public static Graph getInstance(GRAPH_TYPES type) {
        return GraphFactory.getInstance(type, 0);
    }

    public static Graph getInstanceFromString(GRAPH_TYPES type, String s) {
        switch (type) {
            case LIST_GRAPH:
                return new ListGraph(s);
            case MATRIX_GRAPH:
                return new MatrixGraph(s);
            default:
                return null;
        }
    }

    public static Graph getInstanceFromStream(GRAPH_TYPES type, InputStream in) {
        switch (type) {
            case LIST_GRAPH:
                return new ListGraph(in);
            case MATRIX_GRAPH:
                return new MatrixGraph(in);
            default:
                return null;
        }
    }
}

@@ -36,16 +36,68 @@
 tStream;
+%0Aimport java.lang.reflect.InvocationTargetException;
 %0A%0A/**%0A *
@@ -149,283 +149,2124 @@
 ass 
-for automation of Graph subclasses constructing%0A *%0A *TODO%0A * Think of some meta-programming to get rid of ugly repeating%0A * of if-else and switch statements in different getInstance(...).%0A */%0Apublic class GraphFactory %7B%0A%0A    public enum GRAPH_TYPES %7B LIST_GRAPH, MATRIX_GRAPH
+providing universal interface%0A * for constructing instances of Graph subclasses.%0A */%0Apublic class GraphFactory %7B%0A%0A    private static final String NO_SUCH_CONSTRUCTOR = %22This subclass does not provide requested constructor.%22;%0A    private static final String NON_CALLABLE_CONSTRUCTOR = %22This subclass is abstract and cannot be initialized.%22;%0A    private static final String NO_ACCESS_TO_CONSTRUCTOR = %22This subclass does not give access to requested constructor.%22;%0A    private static final String CONSTRUCTOR_EXCPECTION = %22This constructor threw its underlying exception.%22;%0A%0A    /**%0A     * Unum for all Graph subclasses that provide a%0A     * constructor for given parameters of getInstance(...)%0A     * methods.%0A     *%0A     * For universal constructor interface%0A     * @see #getInstance(Class, int)%0A     * @see #getInstanceFromString(Class, String)%0A     * @see #getInstanceFromStream(Class, InputStream)%0A     */%0A    public enum GRAPH_TYPES %7B LIST_GRAPH, MATRIX_GRAPH %7D%0A%0A    public static Graph getInstance(Class%3C? extends Graph%3E graphSubclass, int verticesNumber) throws IllegalArgumentException %7B%0A        try %7B%0A            return graphSubclass.getDeclaredConstructor(Integer.TYPE).newInstance(verticesNumber);%0A        %7D catch (Exception e) %7B%0A            throw getTranslatedConstructorException(e);%0A        %7D%0A    %7D%0A%0A    public static Graph getInstance(Class%3C? extends Graph%3E graphSubclass) %7B%0A        return getInstance(graphSubclass, 0);%0A    %7D%0A%0A    public static Graph getInstanceFromString(Class%3C? extends Graph%3E graphSubclass, String s) throws IllegalArgumentException %7B%0A        try %7B%0A            return graphSubclass.getDeclaredConstructor(String.class).newInstance(s);%0A        %7D catch (Exception e) %7B%0A            throw getTranslatedConstructorException(e);%0A        %7D%0A    %7D%0A%0A    public static Graph getInstanceFromStream(Class%3C? extends Graph%3E graphSubclass, InputStream in) throws IllegalArgumentException %7B%0A        try %7B%0A            return graphSubclass.getDeclaredConstructor(InputStream.class).newInstance(in);%0A        %7D catch (Exception e) %7B%0A            throw getTranslatedConstructorException(e);%0A        %7D%0A   
  %7D%0A%0A
@@ -3343,11 +3343,762 @@
  %7D%0A    %7D
+%0A%0A    private static IllegalArgumentException getTranslatedConstructorException(Exception e) %7B%0A        if(e instanceof InstantiationException) %7B%0A            return new IllegalArgumentException(NON_CALLABLE_CONSTRUCTOR);%0A        %7D%0A        else if(e instanceof  IllegalAccessException) %7B%0A            return new IllegalArgumentException(NO_ACCESS_TO_CONSTRUCTOR);%0A        %7D%0A        else if(e instanceof InvocationTargetException) %7B%0A            return new IllegalArgumentException(CONSTRUCTOR_EXCPECTION + %22 %22 + e.getMessage());%0A        %7D%0A        else if(e instanceof NoSuchMethodException) %7B%0A            return new IllegalArgumentException(NO_SUCH_CONSTRUCTOR);%0A        %7D%0A        else %7B%0A            return new IllegalArgumentException(e);%0A        %7D%0A    %7D
 %0A%7D%0A

add dynamic constructor interface