src/de/uapcore/sudoku/DocumentHandler.java

Sat, 26 Jan 2013 19:34:31 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 19:34:31 +0100
changeset 5
8ddf4af937d7
child 6
5bab2e971333
permissions
-rw-r--r--

moved field methods to field class + added (parts of the) document handler

     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.BufferedWriter;
    30 import java.io.FileOutputStream;
    31 import java.io.IOException;
    32 import java.io.OutputStreamWriter;
    34 /**
    35  *
    36  * @author mike
    37  */
    38 public class DocumentHandler {
    40     private String filename;
    42     public void load(Field field) throws IOException {
    43         if (!isFilenameSet()) {
    44             throw new IOException("no filename supplied");
    45         }
    46         // TODO: implement
    47     }
    49     public void save(Field field) throws IOException {
    50         if (!isFilenameSet()) {
    51             throw new IOException("no filename supplied");
    52         }
    54         try (BufferedWriter out = new BufferedWriter(
    55                 new OutputStreamWriter(new FileOutputStream(filename)))) {
    56             for (int i = 0 ; i < 9 ; i++) {
    57                 int[] row = field.getRow(i);
    58                 for (int j = 0 ; j < 9 ; j++) {
    59                     out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_');
    60                     out.append(j == 8 ? '\n': ' ');
    61                 }
    62             }
    63         }
    64     }
    66     public void setFilename(String filename) {
    67         this.filename = filename;
    68     }
    70     public void clearFilename() {
    71         filename = null;
    72     }
    74     public boolean isFilenameSet() {
    75         return filename != null;
    76     }
    77 }

mercurial