// Path: cas-core/src/main/java/com/ghjansen/cas/core/ca/Combination.java
// public abstract class Combination<A extends State> {
// 
//     private A reference;
//     private List<A> neighborhood;
// 
//     protected Combination(A reference, A... neighbors) throws InvalidStateException {
//         if (reference == null || neighbors == null) {
//             throw new InvalidStateException();
//         }
//         this.reference = reference;
//         this.neighborhood = new ArrayList<A>();
//         for (int i = 0; i < neighbors.length; i++) {
//             this.neighborhood.add(neighbors[i]);
//         }
//     }
// 
//     public A getReferenceState() {
//         return this.reference;
//     }
// 
//     public List<A> getNeighborhood() {
//         return this.neighborhood;
//     }
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/ca/Transition.java
// public abstract class Transition<O extends Combination, A extends State> {
// 
//     private O combination;
//     private A state;
// 
//     protected Transition(O combination, A state) throws InvalidCombinationException, InvalidStateException {
//         if (combination == null) {
//             throw new InvalidCombinationException();
//         }
//         if (state == null) {
//             throw new InvalidStateException();
//         }
//         this.combination = combination;
//         this.state = state;
//     }
// 
//     public O getCombination() {
//         return this.combination;
//     }
// 
//     public A getState() {
//         return this.state;
//     }
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidDimensionalAmountException.java
// public class InvalidDimensionalAmountException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 1514291655215196494L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidDimensionalSpaceException.java
// public class InvalidDimensionalSpaceException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -859556206406629948L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidInitialConditionException.java
// public class InvalidInitialConditionException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 3070196019858178183L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidStateException.java
// public class InvalidStateException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -7435119307025647318L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidTransitionException.java
// public class InvalidTransitionException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 1848434169756515614L;
// 
// }


import java.util.ArrayList;
import java.util.List;
import com.ghjansen.cas.core.ca.Combination;
import com.ghjansen.cas.core.ca.Transition;
import com.ghjansen.cas.core.exception.InvalidDimensionalAmountException;
import com.ghjansen.cas.core.exception.InvalidDimensionalSpaceException;
import com.ghjansen.cas.core.exception.InvalidInitialConditionException;
import com.ghjansen.cas.core.exception.InvalidStateException;
import com.ghjansen.cas.core.exception.InvalidTransitionException;
/*
 * CAS - Cellular Automata Simulator
 * Copyright (C) 2016  Guilherme Humberto Jansen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ghjansen.cas.core.physics;



/**
 * @author Guilherme Humberto Jansen (contact.ghjansen@gmail.com)
 */
public abstract class Space<X, T extends Time, N extends Transition, O extends Combination> {

    protected List<X> initial;
    protected List<List<X>> history;
    protected List<X> last;
    protected List<X> current;
    protected int dimensionalAmount;
    protected boolean keepHistory;
    protected boolean rotating;

    protected Space(T time, List<X> initialCondition, boolean keepHistory)
            throws InvalidDimensionalAmountException, InvalidInitialConditionException, InvalidDimensionalSpaceException {
        if (time.getRelative() != null && !time.getRelative().isEmpty()) {
            this.dimensionalAmount = time.getRelative().size();
        } else {
            throw new InvalidDimensionalAmountException();
        }
        if (initialCondition != null && !initialCondition.isEmpty()) {
            validateDimensions(initialCondition, this.dimensionalAmount);
            this.initial = initialCondition;
        } else {
            throw new InvalidInitialConditionException();
        }
        this.keepHistory = keepHistory;
        this.rotating = false;
        initialize();
    }

    protected void validateDimensions(List<X> space, int dimensionalAmount) throws InvalidDimensionalSpaceException {
        if (dimensionalAmount == 1) {
            if (!space.isEmpty() && !(space.get(0) instanceof Cell)) {
// Path: cas-core/src/main/java/com/ghjansen/cas/core/ca/Combination.java
// public abstract class Combination<A extends State> {
// 
//     private A reference;
//     private List<A> neighborhood;
// 
//     protected Combination(A reference, A... neighbors) throws InvalidStateException {
//         if (reference == null || neighbors == null) {
//             throw new InvalidStateException();
//         }
//         this.reference = reference;
//         this.neighborhood = new ArrayList<A>();
//         for (int i = 0; i < neighbors.length; i++) {
//             this.neighborhood.add(neighbors[i]);
//         }
//     }
// 
//     public A getReferenceState() {
//         return this.reference;
//     }
// 
//     public List<A> getNeighborhood() {
//         return this.neighborhood;
//     }
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/ca/Transition.java
// public abstract class Transition<O extends Combination, A extends State> {
// 
//     private O combination;
//     private A state;
// 
//     protected Transition(O combination, A state) throws InvalidCombinationException, InvalidStateException {
//         if (combination == null) {
//             throw new InvalidCombinationException();
//         }
//         if (state == null) {
//             throw new InvalidStateException();
//         }
//         this.combination = combination;
//         this.state = state;
//     }
// 
//     public O getCombination() {
//         return this.combination;
//     }
// 
//     public A getState() {
//         return this.state;
//     }
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidDimensionalAmountException.java
// public class InvalidDimensionalAmountException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 1514291655215196494L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidDimensionalSpaceException.java
// public class InvalidDimensionalSpaceException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -859556206406629948L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidInitialConditionException.java
// public class InvalidInitialConditionException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 3070196019858178183L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidStateException.java
// public class InvalidStateException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -7435119307025647318L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidTransitionException.java
// public class InvalidTransitionException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 1848434169756515614L;
// 
// }


// Path: cas-core/src/main/java/com/ghjansen/cas/core/physics/Space.java
import java.util.ArrayList;
import java.util.List;
import com.ghjansen.cas.core.ca.Combination;
import com.ghjansen.cas.core.ca.Transition;
import com.ghjansen.cas.core.exception.InvalidDimensionalAmountException;
import com.ghjansen.cas.core.exception.InvalidDimensionalSpaceException;
import com.ghjansen.cas.core.exception.InvalidInitialConditionException;
import com.ghjansen.cas.core.exception.InvalidStateException;
import com.ghjansen.cas.core.exception.InvalidTransitionException;

/*
 * CAS - Cellular Automata Simulator
 * Copyright (C) 2016  Guilherme Humberto Jansen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ghjansen.cas.core.physics;



/**
 * @author Guilherme Humberto Jansen (contact.ghjansen@gmail.com)
 */
public abstract class Space<X, T extends Time, N extends Transition, O extends Combination> {

    protected List<X> initial;
    protected List<List<X>> history;
    protected List<X> last;
    protected List<X> current;
    protected int dimensionalAmount;
    protected boolean keepHistory;
    protected boolean rotating;

    protected Space(T time, List<X> initialCondition, boolean keepHistory)
            throws InvalidDimensionalAmountException, InvalidInitialConditionException, InvalidDimensionalSpaceException {
        if (time.getRelative() != null && !time.getRelative().isEmpty()) {
            this.dimensionalAmount = time.getRelative().size();
        } else {
            throw new InvalidDimensionalAmountException();
        }
        if (initialCondition != null && !initialCondition.isEmpty()) {
            validateDimensions(initialCondition, this.dimensionalAmount);
            this.initial = initialCondition;
        } else {
            throw new InvalidInitialConditionException();
        }
        this.keepHistory = keepHistory;
        this.rotating = false;
        initialize();
    }

    protected void validateDimensions(List<X> space, int dimensionalAmount) throws InvalidDimensionalSpaceException {
        if (dimensionalAmount == 1) {
            if (!space.isEmpty() && !(space.get(0) instanceof Cell)) {
                throw new InvalidDimensionalSpaceException();