Sat, 26 Jan 2013 19:34:31 +0100
moved field methods to field class + added (parts of the) document handler
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 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | import java.io.BufferedWriter; |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | import java.io.FileOutputStream; |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | import java.io.IOException; |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | import java.io.OutputStreamWriter; |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | /** |
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 | * @author mike |
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 | 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
|
39 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | private String filename; |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | 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
|
43 | if (!isFilenameSet()) { |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | 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
|
45 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | // TODO: implement |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | 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
|
50 | if (!isFilenameSet()) { |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | 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
|
52 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | 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
|
55 | 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
|
56 | 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
|
57 | 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
|
58 | 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
|
59 | 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
|
60 | 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
|
61 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | 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
|
67 | 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
|
68 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | 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
|
71 | filename = null; |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | 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
|
75 | 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
|
76 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | } |