Android Open Source - opensudoku Cell Note






From Project

Back to project page opensudoku.

License

The source code is released under:

GNU General Public License

If you think the Android project opensudoku listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* 
 * Copyright (C) 2009 Roman Masek/*  w ww  .jav  a 2s  . c om*/
 * 
 * This file is part of OpenSudoku.
 * 
 * OpenSudoku is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * OpenSudoku 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with OpenSudoku.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package org.moire.opensudoku.game;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * Note attached to cell. This object is immutable by design.
 *
 * @author romario
 */
public class CellNote {
  // TODO: int would be better
  private final Set<Integer> mNotedNumbers;

  public static final CellNote EMPTY = new CellNote();

  public CellNote() {
    mNotedNumbers = Collections.unmodifiableSet(new HashSet<Integer>());
  }

  private CellNote(Set<Integer> notedNumbers) {
    mNotedNumbers = Collections.unmodifiableSet(notedNumbers);
  }

  /**
   * Creates instance from given string (string which has been
   * created by {@link #serialize(StringBuilder)} or {@link #serialize()} method).
   * earlier.
   *
   * @param note
   */
  public static CellNote deserialize(String note) {
    // TODO: optimalization: CellNote object don't have to be created for empty note

    Set<Integer> notedNumbers = new HashSet<Integer>();
    if (note != null && !note.equals("")) {
      StringTokenizer tokenizer = new StringTokenizer(note, ",");
      while (tokenizer.hasMoreTokens()) {
        String value = tokenizer.nextToken();
        if (!value.equals("-")) {
          notedNumbers.add(Integer.parseInt(value));
        }
      }
    }

    return new CellNote(notedNumbers);
  }


  // TODO: this should be int[]

  /**
   * Creates note instance from given <code>Integer</code> array.
   *
   * @param notedNums Array of integers, which should be part of note.
   * @return New note instance.
   */
  public static CellNote fromIntArray(Integer[] notedNums) {
    Set<Integer> notedNumbers = new HashSet<Integer>();

    for (Integer n : notedNums) {
      notedNumbers.add(n);
    }

    return new CellNote(notedNumbers);
  }


  /**
   * Appends string representation of this object to the given <code>StringBuilder</code>.
   * You can later recreate object from this string by calling {@link #deserialize(String)}.
   *
   * @param data
   */
  public void serialize(StringBuilder data) {
    if (mNotedNumbers.size() == 0) {
      data.append("-");
    } else {
      for (Integer num : mNotedNumbers) {
        data.append(num).append(",");
      }
    }
  }

  public String serialize() {
    StringBuilder sb = new StringBuilder();
    serialize(sb);
    return sb.toString();
  }

  /**
   * Returns numbers currently noted in cell.
   *
   * @return
   */
  public Set<Integer> getNotedNumbers() {
    return mNotedNumbers;
  }

  /**
   * Toggles noted number: if number is already noted, it will be removed otherwise it will be added.
   *
   * @param number Number to toggle.
   * @return New CellNote instance with changes.
   */
  public CellNote toggleNumber(int number) {
    if (number < 1 || number > 9)
      throw new IllegalArgumentException("Number must be between 1-9.");

    Set<Integer> notedNumbers = new HashSet<Integer>(getNotedNumbers());
    if (notedNumbers.contains(number)) {
      notedNumbers.remove(number);
    } else {
      notedNumbers.add(number);
    }

    return new CellNote(notedNumbers);
  }

  /**
   * Adds number to the cell's note (if not present already).
   *
   * @param number
   * @return
   */
  public CellNote addNumber(int number) {
    if (number < 1 || number > 9)
      throw new IllegalArgumentException("Number must be between 1-9.");

    Set<Integer> notedNumbers = new HashSet<Integer>(getNotedNumbers());
    notedNumbers.add(number);

    return new CellNote(notedNumbers);
  }

  /**
   * Removes number from the cell's note.
   *
   * @param number
   * @return
   */
  public CellNote removeNumber(int number) {
    if (number < 1 || number > 9)
      throw new IllegalArgumentException("Number must be between 1-9.");

    Set<Integer> notedNumbers = new HashSet<Integer>(getNotedNumbers());
    notedNumbers.remove(number);

    return new CellNote(notedNumbers);
  }

  public CellNote clear() {
    return new CellNote();
  }

  /**
   * Returns true, if note is empty.
   *
   * @return True if note is empty.
   */
  public boolean isEmpty() {
    return mNotedNumbers.size() == 0;
  }

}




Java Source Code List

org.moire.opensudoku.db.DatabaseHelper.java
org.moire.opensudoku.db.FolderColumns.java
org.moire.opensudoku.db.SudokuColumns.java
org.moire.opensudoku.db.SudokuDatabase.java
org.moire.opensudoku.db.SudokuImportParams.java
org.moire.opensudoku.db.SudokuInvalidFormatException.java
org.moire.opensudoku.game.CellCollection.java
org.moire.opensudoku.game.CellGroup.java
org.moire.opensudoku.game.CellNote.java
org.moire.opensudoku.game.Cell.java
org.moire.opensudoku.game.FolderInfo.java
org.moire.opensudoku.game.SudokuGame.java
org.moire.opensudoku.game.command.AbstractCellCommand.java
org.moire.opensudoku.game.command.AbstractCommand.java
org.moire.opensudoku.game.command.ClearAllNotesCommand.java
org.moire.opensudoku.game.command.CommandStack.java
org.moire.opensudoku.game.command.EditCellNoteCommand.java
org.moire.opensudoku.game.command.FillInNotesCommand.java
org.moire.opensudoku.game.command.SetCellValueCommand.java
org.moire.opensudoku.gui.Changelog.java
org.moire.opensudoku.gui.FileImportActivity.java
org.moire.opensudoku.gui.FileListActivity.java
org.moire.opensudoku.gui.FolderDetailLoader.java
org.moire.opensudoku.gui.FolderListActivity.java
org.moire.opensudoku.gui.GameSettingsActivity.java
org.moire.opensudoku.gui.GameTimeFormat.java
org.moire.opensudoku.gui.HintsQueue.java
org.moire.opensudoku.gui.ImportSudokuActivity.java
org.moire.opensudoku.gui.SeekBarPreference.java
org.moire.opensudoku.gui.SudokuBoardView.java
org.moire.opensudoku.gui.SudokuEditActivity.java
org.moire.opensudoku.gui.SudokuExportActivity.java
org.moire.opensudoku.gui.SudokuImportActivity.java
org.moire.opensudoku.gui.SudokuListActivity.java
org.moire.opensudoku.gui.SudokuListFilter.java
org.moire.opensudoku.gui.SudokuPlayActivity.java
org.moire.opensudoku.gui.Timer.java
org.moire.opensudoku.gui.exporting.FileExportTaskParams.java
org.moire.opensudoku.gui.exporting.FileExportTaskResult.java
org.moire.opensudoku.gui.exporting.FileExportTask.java
org.moire.opensudoku.gui.importing.AbstractImportTask.java
org.moire.opensudoku.gui.importing.ExtrasImportTask.java
org.moire.opensudoku.gui.importing.OpenSudokuImportTask.java
org.moire.opensudoku.gui.importing.SdmImportTask.java
org.moire.opensudoku.gui.inputmethod.IMControlPanelStatePersister.java
org.moire.opensudoku.gui.inputmethod.IMControlPanel.java
org.moire.opensudoku.gui.inputmethod.IMNumpad.java
org.moire.opensudoku.gui.inputmethod.IMPopupDialog.java
org.moire.opensudoku.gui.inputmethod.IMPopup.java
org.moire.opensudoku.gui.inputmethod.IMSingleNumber.java
org.moire.opensudoku.gui.inputmethod.InputMethod.java
org.moire.opensudoku.utils.AndroidUtils.java
org.moire.opensudoku.utils.Const.java
org.moire.opensudoku.utils.StringUtils.java