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

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

mercurial