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

Sat, 25 Jul 2020 14:01:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 14:01:28 +0200
changeset 9
576e7a2861ae
parent 7
src/de/uapcore/sudoku/DocumentHandler.java@2c0a2766461c
child 10
369903afbb29
permissions
-rw-r--r--

converts to maven project

     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  *
    35  * @author mike
    36  */
    37 public class DocumentHandler {
    39     private String filename;
    41     public void load(Field field) throws IOException {
    42         if (!isFilenameSet()) {
    43             throw new IOException("no filename supplied");
    44         }
    45         int row = 0;
    46         try (BufferedReader in = new BufferedReader(
    47                 new InputStreamReader(new FileInputStream(filename)))) {
    48             Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$");
    49             String line;
    50             while ((line = in.readLine()) != null) {
    51                 if (line.matches("^\\s*$")) {
    52                     continue;
    53                 }
    54                 Matcher m = pat.matcher(line);
    55                 if (m.matches()) {
    56                     String c[] = line.trim().split(" ");
    57                     if (c.length != 9) {
    58                         break;
    59                     }
    60                     for (int i = 0 ; i < 9 ; i++) {
    61                         field.setCellValue(i, row,
    62                                 c[i].equals("_") ? 0 : Integer.valueOf(c[i]));
    63                     }
    64                     row++;
    65                 } else {
    66                     break;
    67                 }
    68             }
    69             if (row != 9) {
    70                 throw new IOException("Kein Sudoku-Feld enthalten!");
    71             }
    72         }
    73         field.setAllCellsModified(false);
    74     }
    76     public void save(Field field) throws IOException {
    77         if (!isFilenameSet()) {
    78             throw new IOException("no filename supplied");
    79         }
    81         try (BufferedWriter out = new BufferedWriter(
    82                 new OutputStreamWriter(new FileOutputStream(filename)))) {
    83             for (int i = 0 ; i < 9 ; i++) {
    84                 int[] row = field.getRow(i);
    85                 for (int j = 0 ; j < 9 ; j++) {
    86                     out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_');
    87                     out.append(j == 8 ? '\n': ' ');
    88                 }
    89             }
    90         }
    91     }
    93     public void setFilename(String filename) {
    94         this.filename = filename;
    95     }
    97     public void clearFilename() {
    98         filename = null;
    99     }
   101     public boolean isFilenameSet() {
   102         return filename != null;
   103     }
   104 }

mercurial