Sat, 26 Jan 2013 19:34:31 +0100
moved field methods to field class + added (parts of the) document handler
universe@5 | 1 | /* |
universe@5 | 2 | * Copyright 2013 Mike Becker. All rights reserved. |
universe@5 | 3 | * |
universe@5 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@5 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@5 | 6 | * |
universe@5 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@5 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@5 | 9 | * |
universe@5 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@5 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@5 | 12 | * documentation and/or other materials provided with the distribution. |
universe@5 | 13 | * |
universe@5 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@5 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@5 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@5 | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@5 | 18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@5 | 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@5 | 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@5 | 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@5 | 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@5 | 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@5 | 24 | * POSSIBILITY OF SUCH DAMAGE. |
universe@5 | 25 | */ |
universe@5 | 26 | |
universe@5 | 27 | package de.uapcore.sudoku; |
universe@5 | 28 | |
universe@5 | 29 | import java.io.BufferedWriter; |
universe@5 | 30 | import java.io.FileOutputStream; |
universe@5 | 31 | import java.io.IOException; |
universe@5 | 32 | import java.io.OutputStreamWriter; |
universe@5 | 33 | |
universe@5 | 34 | /** |
universe@5 | 35 | * |
universe@5 | 36 | * @author mike |
universe@5 | 37 | */ |
universe@5 | 38 | public class DocumentHandler { |
universe@5 | 39 | |
universe@5 | 40 | private String filename; |
universe@5 | 41 | |
universe@5 | 42 | public void load(Field field) throws IOException { |
universe@5 | 43 | if (!isFilenameSet()) { |
universe@5 | 44 | throw new IOException("no filename supplied"); |
universe@5 | 45 | } |
universe@5 | 46 | // TODO: implement |
universe@5 | 47 | } |
universe@5 | 48 | |
universe@5 | 49 | public void save(Field field) throws IOException { |
universe@5 | 50 | if (!isFilenameSet()) { |
universe@5 | 51 | throw new IOException("no filename supplied"); |
universe@5 | 52 | } |
universe@5 | 53 | |
universe@5 | 54 | try (BufferedWriter out = new BufferedWriter( |
universe@5 | 55 | new OutputStreamWriter(new FileOutputStream(filename)))) { |
universe@5 | 56 | for (int i = 0 ; i < 9 ; i++) { |
universe@5 | 57 | int[] row = field.getRow(i); |
universe@5 | 58 | for (int j = 0 ; j < 9 ; j++) { |
universe@5 | 59 | out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_'); |
universe@5 | 60 | out.append(j == 8 ? '\n': ' '); |
universe@5 | 61 | } |
universe@5 | 62 | } |
universe@5 | 63 | } |
universe@5 | 64 | } |
universe@5 | 65 | |
universe@5 | 66 | public void setFilename(String filename) { |
universe@5 | 67 | this.filename = filename; |
universe@5 | 68 | } |
universe@5 | 69 | |
universe@5 | 70 | public void clearFilename() { |
universe@5 | 71 | filename = null; |
universe@5 | 72 | } |
universe@5 | 73 | |
universe@5 | 74 | public boolean isFilenameSet() { |
universe@5 | 75 | return filename != null; |
universe@5 | 76 | } |
universe@5 | 77 | } |