Skip to content
Snippets Groups Projects
Cell.java 1.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guyslain's avatar
    Guyslain committed
    import datastruct.Lens;
    
    import java.util.ArrayList;
    import java.util.List;
    
    Guyslain's avatar
    Guyslain committed
     * {@link Cell} instances represent the cells of the grid in a simulation of cellular automata.
    
    Guyslain's avatar
    Guyslain committed
    public class Cell<T> implements Lens<T> {
        private T content;
        private final List<OnChangeListener<T>> listeners = new ArrayList<>();
    
    Guyslain's avatar
    Guyslain committed
        /** Initialize a new cell with a given value.
    
    Guyslain's avatar
    Guyslain committed
         * @param initialContent the value initially stored by the cell.
    
    Guyslain's avatar
    Guyslain committed
        public Cell(T initialContent) {
            this.content = initialContent;
    
    Guyslain's avatar
    Guyslain committed
        public void addOnChangeListener(OnChangeListener<T> listener) {
            this.listeners.add(listener);
    
    Guyslain's avatar
    Guyslain committed
         * Sets the content of this {@link Cell}.
    
    Guyslain's avatar
    Guyslain committed
         * @param value the new content of this {@link Cell}
    
    Guyslain's avatar
    Guyslain committed
        public void set(T value) {
            this.content = value;
            for (OnChangeListener<T> listener : this.listeners) {
                listener.valueChanged(this.content, value);
            }
    
    Guyslain's avatar
    Guyslain committed
         * Returns the current content of this {@link Cell}.
    
    Guyslain's avatar
    Guyslain committed
         * @return the current content of this {@link Cell}
    
    Guyslain's avatar
    Guyslain committed
        public T get(){
            return this.content;
    
    Guyslain's avatar
    Guyslain committed