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()

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

mercurial