mx.ecosur.multigame.grid.entity.GridCell.java Source code

Java tutorial

Introduction

Here is the source code for mx.ecosur.multigame.grid.entity.GridCell.java

Source

/*
 * Copyright (C) 2010 ECOSUR, Andrew Waterman and Max Pimm
 *
 * Licensed under the Academic Free License v. 3.0.
 * http://www.opensource.org/licenses/afl-3.0.php
 */

/**
 * A cell represents a location on a gameboard. Cells have a color, specified 
 * by means of the Color enum.
 * 
 * @author awaterma@ecosur.mx
 * 
 */

package mx.ecosur.multigame.grid.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import mx.ecosur.multigame.grid.comparator.CellComparator;
import mx.ecosur.multigame.grid.Color;

import mx.ecosur.multigame.model.interfaces.Cell;
import org.apache.commons.lang.builder.HashCodeBuilder;

@Entity
public class GridCell implements Cell, Cloneable {

    private static final long serialVersionUID = -7917786935353131901L;

    protected int row, column;

    private Color color;

    protected int id;

    public GridCell() {
        super();
    }

    /*
     * Instantiates a new Cell, at the named co-ordinates, set to the specified
     * Color
     */
    public GridCell(int column, int row, Color color) {
        this.column = column;
        this.row = row;
        this.color = color;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "CELL_ROW")
    public int getRow() {
        return row;
    }

    public void setRow(int y) {
        this.row = y;
    }

    @Column(name = "CELL_COLUMN")
    public int getColumn() {
        return column;
    }

    public void setColumn(int x) {
        this.column = x;
    }

    public Color getColor() {
        if (color == null)
            color = Color.UNKNOWN;
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public GridCell clone() throws CloneNotSupportedException {
        super.clone();
        GridCell clone = new GridCell(this.column, this.row, Color.valueOf(color.name()));
        clone.setId(getId());
        return clone;
    }

    public String toString() {
        return "(Column, Row) Column = " + column + ", Row = " + row + ", Color = " + color;
    }

    @Override
    public boolean equals(Object obj) {
        boolean ret = false;
        if (obj instanceof GridCell && obj != null) {
            GridCell c = (GridCell) obj;
            ret = getRow() == c.getRow() && getColumn() == c.getColumn();
        }

        return ret;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(row).append(column).toHashCode();
    }
}