java/RAII/src/cz/frantovo/priklady/raii/Zdroj.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 09 Sep 2014 14:51:08 +0200
changeset 21 627912dacae3
child 25 95be94124ce9
permissions -rw-r--r--
RAII v Javě pomocí try+AutoCloseable
franta-hg@21
     1
/**
franta-hg@21
     2
 * RAII v Javě
franta-hg@21
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@21
     4
 *
franta-hg@21
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@21
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@21
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@21
     8
 * (at your option) any later version.
franta-hg@21
     9
 *
franta-hg@21
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@21
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@21
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@21
    13
 * GNU General Public License for more details.
franta-hg@21
    14
 *
franta-hg@21
    15
 * You should have received a copy of the GNU General Public License
franta-hg@21
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@21
    17
 */
franta-hg@21
    18
package cz.frantovo.priklady.raii;
franta-hg@21
    19
franta-hg@21
    20
/**
franta-hg@21
    21
 *
franta-hg@21
    22
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@21
    23
 */
franta-hg@21
    24
public class Zdroj implements AutoCloseable {
franta-hg@21
    25
franta-hg@21
    26
	private final String název;
franta-hg@21
    27
	private final boolean chybaVytváření;
franta-hg@21
    28
	private final boolean chybaDělání;
franta-hg@21
    29
	private final boolean chybaZavírání;
franta-hg@21
    30
franta-hg@21
    31
	public Zdroj(String název, boolean chybaVytváření, boolean chybaDělání, boolean chybaZavírání) throws ChybaVytváření {
franta-hg@21
    32
		this.název = název;
franta-hg@21
    33
		System.err.println("Vytváříme: " + this + " chyba=" + chybaVytváření);
franta-hg@21
    34
		this.chybaVytváření = chybaVytváření;
franta-hg@21
    35
		this.chybaDělání = chybaDělání;
franta-hg@21
    36
		this.chybaZavírání = chybaZavírání;
franta-hg@21
    37
franta-hg@21
    38
		if (this.chybaVytváření) {
franta-hg@21
    39
			throw new ChybaVytváření(this);
franta-hg@21
    40
		}
franta-hg@21
    41
	}
franta-hg@21
    42
franta-hg@21
    43
	public void něcoDělej() throws ChybaDělání {
franta-hg@21
    44
		System.err.println("Něco děláme: " + this + " chyba=" + chybaDělání);
franta-hg@21
    45
franta-hg@21
    46
		if (chybaDělání) {
franta-hg@21
    47
			throw new ChybaDělání(this);
franta-hg@21
    48
		}
franta-hg@21
    49
	}
franta-hg@21
    50
franta-hg@21
    51
	@Override
franta-hg@21
    52
	public void close() throws ChybaZavírání {
franta-hg@21
    53
		System.err.println("Zavíráme: " + this + " chyba=" + chybaZavírání);
franta-hg@21
    54
franta-hg@21
    55
		if (chybaZavírání) {
franta-hg@21
    56
			throw new ChybaZavírání(this);
franta-hg@21
    57
		}
franta-hg@21
    58
	}
franta-hg@21
    59
franta-hg@21
    60
	@Override
franta-hg@21
    61
	protected void finalize() throws Throwable {
franta-hg@21
    62
		System.err.println("Finalizujeme: " + this);
franta-hg@21
    63
	}
franta-hg@21
    64
franta-hg@21
    65
	@Override
franta-hg@21
    66
	public String toString() {
franta-hg@21
    67
		return getClass().getSimpleName() + " [" + název + "]";
franta-hg@21
    68
	}
franta-hg@21
    69
franta-hg@21
    70
}