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

Wed, 26 Aug 2020 19:09:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 26 Aug 2020 19:09:07 +0200
changeset 25
569220009c54
parent 22
06170a0be62a
permissions
-rw-r--r--

fixes wrong call of assertEquals()

/*
 * Copyright 2013 Mike Becker. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package de.uapcore.sudoku;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Implements load and save routines.
 */
public class DocumentHandler {
    
    private String filename;

    /**
     * Loads data into the specified field.
     *
     * @param field the field to populated with the loaded data
     * @throws IOException if loading fails or no file name has been set before
     * @see #setFilename(String)
     */
    public void load(Field field) throws IOException {
        if (!isFilenameSet()) {
            throw new IOException("no filename supplied");
        }
        int row = 0;
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(new FileInputStream(filename)))) {
            Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$");
            String line;
            while ((line = in.readLine()) != null) {
                if (line.matches("^\\s*$")) {
                    continue;
                }
                Matcher m = pat.matcher(line);
                if (m.matches()) {
                    String[] c = line.trim().split(" ");
                    if (c.length != 9) {
                        break;
                    }
                    for (int i = 0 ; i < 9 ; i++) {
                        field.setCellValue(i, row,
                                c[i].equals("_") ? 0 : Integer.parseInt(c[i]));
                    }
                    row++;
                } else {
                    break;
                }
            }
            if (row != 9) {
                throw new IOException("Kein Sudoku-Feld enthalten!");
            }
        }
        field.setAllCellsModified(false);
    }

    /**
     * Saves the specified field to a file.
     * On success, the modified state of all cells is set to false.
     *
     * @param field the field to save
     * @throws IOException if saving fails or the file name has not been set before
     * @see #setFilename(String)
     */
    public void save(Field field) throws IOException {
        if (!isFilenameSet()) {
            throw new IOException("no filename supplied");
        }
        
        try (BufferedWriter out = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(filename)))) {
            for (int i = 0 ; i < 9 ; i++) {
                int[] row = field.getRow(i);
                for (int j = 0 ; j < 9 ; j++) {
                    out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_');
                    out.append(j == 8 ? '\n': ' ');
                }
            }
        }
        field.setAllCellsModified(false);
    }

    /**
     * Sets the file name for loading and saving data.
     *
     * @param filename the file name
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }

    /**
     * Clears the file name.
     */
    public void clearFilename() {
        filename = null;
    }

    /**
     * Checks whether a file name has been set.
     *
     * @return true if a file name is known, false otherwise
     */
    public boolean isFilenameSet() {
        return filename != null;
    }
}

mercurial