src/main/java/de/uapcore/sudoku/DocumentHandler.java

Tue, 28 Jul 2020 14:27:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 14:27:14 +0200
changeset 22
06170a0be62a
parent 12
1c62c6009161
permissions
-rw-r--r--

bugfix: modified state is reset even when saving fails + more tests

universe@5 1 /*
universe@5 2 * Copyright 2013 Mike Becker. All rights reserved.
universe@5 3 *
universe@5 4 * Redistribution and use in source and binary forms, with or without
universe@5 5 * modification, are permitted provided that the following conditions are met:
universe@5 6 *
universe@5 7 * 1. Redistributions of source code must retain the above copyright
universe@5 8 * notice, this list of conditions and the following disclaimer.
universe@5 9 *
universe@5 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@5 11 * notice, this list of conditions and the following disclaimer in the
universe@5 12 * documentation and/or other materials provided with the distribution.
universe@5 13 *
universe@5 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@5 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@5 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@5 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@5 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@5 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@5 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@5 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@5 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@5 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@5 24 * POSSIBILITY OF SUCH DAMAGE.
universe@5 25 */
universe@5 26
universe@5 27 package de.uapcore.sudoku;
universe@5 28
universe@9 29 import java.io.*;
universe@6 30 import java.util.regex.Matcher;
universe@6 31 import java.util.regex.Pattern;
universe@5 32
universe@5 33 /**
universe@11 34 * Implements load and save routines.
universe@5 35 */
universe@5 36 public class DocumentHandler {
universe@5 37
universe@5 38 private String filename;
universe@10 39
universe@10 40 /**
universe@10 41 * Loads data into the specified field.
universe@10 42 *
universe@10 43 * @param field the field to populated with the loaded data
universe@10 44 * @throws IOException if loading fails or no file name has been set before
universe@10 45 * @see #setFilename(String)
universe@10 46 */
universe@5 47 public void load(Field field) throws IOException {
universe@5 48 if (!isFilenameSet()) {
universe@5 49 throw new IOException("no filename supplied");
universe@5 50 }
universe@6 51 int row = 0;
universe@6 52 try (BufferedReader in = new BufferedReader(
universe@6 53 new InputStreamReader(new FileInputStream(filename)))) {
universe@6 54 Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$");
universe@6 55 String line;
universe@6 56 while ((line = in.readLine()) != null) {
universe@6 57 if (line.matches("^\\s*$")) {
universe@6 58 continue;
universe@6 59 }
universe@6 60 Matcher m = pat.matcher(line);
universe@6 61 if (m.matches()) {
universe@12 62 String[] c = line.trim().split(" ");
universe@6 63 if (c.length != 9) {
universe@6 64 break;
universe@6 65 }
universe@6 66 for (int i = 0 ; i < 9 ; i++) {
universe@6 67 field.setCellValue(i, row,
universe@12 68 c[i].equals("_") ? 0 : Integer.parseInt(c[i]));
universe@6 69 }
universe@6 70 row++;
universe@6 71 } else {
universe@6 72 break;
universe@6 73 }
universe@6 74 }
universe@6 75 if (row != 9) {
universe@6 76 throw new IOException("Kein Sudoku-Feld enthalten!");
universe@6 77 }
universe@6 78 }
universe@7 79 field.setAllCellsModified(false);
universe@5 80 }
universe@10 81
universe@10 82 /**
universe@10 83 * Saves the specified field to a file.
universe@22 84 * On success, the modified state of all cells is set to false.
universe@10 85 *
universe@10 86 * @param field the field to save
universe@10 87 * @throws IOException if saving fails or the file name has not been set before
universe@10 88 * @see #setFilename(String)
universe@10 89 */
universe@5 90 public void save(Field field) throws IOException {
universe@5 91 if (!isFilenameSet()) {
universe@5 92 throw new IOException("no filename supplied");
universe@5 93 }
universe@5 94
universe@5 95 try (BufferedWriter out = new BufferedWriter(
universe@5 96 new OutputStreamWriter(new FileOutputStream(filename)))) {
universe@5 97 for (int i = 0 ; i < 9 ; i++) {
universe@5 98 int[] row = field.getRow(i);
universe@5 99 for (int j = 0 ; j < 9 ; j++) {
universe@5 100 out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_');
universe@5 101 out.append(j == 8 ? '\n': ' ');
universe@5 102 }
universe@5 103 }
universe@5 104 }
universe@22 105 field.setAllCellsModified(false);
universe@5 106 }
universe@10 107
universe@10 108 /**
universe@10 109 * Sets the file name for loading and saving data.
universe@10 110 *
universe@10 111 * @param filename the file name
universe@10 112 */
universe@5 113 public void setFilename(String filename) {
universe@5 114 this.filename = filename;
universe@5 115 }
universe@10 116
universe@10 117 /**
universe@10 118 * Clears the file name.
universe@10 119 */
universe@5 120 public void clearFilename() {
universe@5 121 filename = null;
universe@5 122 }
universe@10 123
universe@10 124 /**
universe@10 125 * Checks whether a file name has been set.
universe@10 126 *
universe@10 127 * @return true if a file name is known, false otherwise
universe@10 128 */
universe@5 129 public boolean isFilenameSet() {
universe@5 130 return filename != null;
universe@5 131 }
universe@5 132 }

mercurial