001    /*
002     * Created on Mar 2, 2008
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2008-2010 the original author or authors.
015     */
016    package org.fest.swing.data;
017    
018    import static java.lang.String.valueOf;
019    import static org.fest.util.Objects.HASH_CODE_PRIME;
020    import static org.fest.util.Strings.concat;
021    
022    import javax.swing.JTable;
023    
024    /**
025     * Understands a cell in a <code>{@link JTable}</code>.
026     *
027     * @author Alex Ruiz
028     */
029    public class TableCell {
030    
031      /** The row of the cell. */
032      public final int row;
033    
034      /** The column of the cell. */
035      public final int column;
036    
037      /**
038       * Starting point for the creation of a <code>{@link TableCell}</code>.
039       * <p>
040       * Example:
041       * <pre>
042       * // import static org.fest.swing.data.TableCell.row;
043       * TableCell cell = row(5).column(3);
044       * </pre>
045       * </p>
046       * @param row the row index of the table cell to create.
047       * @return the created builder.
048       */
049      public static TableCellBuilder row(int row) { return new TableCellBuilder(row); }
050    
051      /**
052       * Understands creation of <code>{@link TableCell}</code>s.
053       *
054       * @author Alex Ruiz
055       */
056      public static class TableCellBuilder {
057        private final int row;
058    
059        TableCellBuilder(int row) { this.row = row; }
060    
061        /**
062         * Creates a new table cell using the row index specified in <code>{@link TableCellBuilder#row(int)}</code> and the
063         * column index specified as the argument in this method.
064         * @param column the column index of the table cell to create.
065         * @return the created table cell.
066         */
067        public TableCell column(int column) { return new TableCell(row, column); }
068      }
069    
070      /**
071       * Creates a new </code>{@link TableCell}</code>.
072       * @param row the row of the cell.
073       * @param column the column of the cell.
074       */
075      protected TableCell(int row, int column) {
076        this.row = row;
077        this.column = column;
078      }
079    
080      /** ${@inheritDoc} */
081      @Override public boolean equals(Object obj) {
082        if (this == obj) return true;
083        if (obj == null) return false;
084        if (!(obj instanceof TableCell)) return false;
085        TableCell other = (TableCell) obj;
086        if (row != other.row) return false;
087        return column == other.column;
088      }
089    
090      /** ${@inheritDoc} */
091      @Override public int hashCode() {
092        int result = 1;
093        result = HASH_CODE_PRIME * result + column;
094        result = HASH_CODE_PRIME * result + row;
095        return result;
096      }
097    
098      @Override public String toString() {
099        return concat("[row=", valueOf(row), ", column=", valueOf(column), "]");
100      }
101    }