java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Atribut.java
author František Kučera <franta-hg@frantovo.cz>
Sat Aug 18 13:27:00 2012 +0200 (2012-08-18)
changeset 19 c20edbed09c3
parent 11 9b399cde6a3b
child 27 36cee2c8f5f8
permissions -rw-r--r--
informace o licenci uvnitř .java souborů
     1 /**
     2  * Rozšířené atributy – program na správu rozšířených atributů souborů
     3  * Copyright © 2012 František Kučera (frantovo.cz)
     4  * 
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  * 
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  * 
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.rozsireneAtributy;
    19 
    20 import java.nio.ByteBuffer;
    21 import java.nio.charset.Charset;
    22 
    23 public class Atribut {
    24 
    25 	private String klíč;
    26 	private String hodnota;
    27 
    28 	public Atribut(String klíč, String hodnota) {
    29 		this.klíč = klíč;
    30 		this.hodnota = hodnota;
    31 	}
    32 
    33 	public Atribut(String klíč, ByteBuffer hodnota) {
    34 		this.klíč = klíč;
    35 		setHodnota(hodnota);
    36 	}
    37 
    38 	public Atribut() {
    39 	}
    40 
    41 	public String getKlíč() {
    42 		return klíč;
    43 	}
    44 
    45 	public void setKlíč(String klíč) {
    46 		this.klíč = klíč;
    47 	}
    48 
    49 	/**
    50 	 * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit
    51 	 * TODO: další kontroly?
    52 	 * @return jestli je platný
    53 	 */
    54 	public boolean isPlatnýKlíč() {
    55 		return klíč != null && klíč.length() > 0;
    56 	}
    57 
    58 	/**
    59 	 * nulová hodnota → smazání atributu
    60 	 * (ale může být prázdný řetězec)
    61 	 * @return jestli je platná
    62 	 */
    63 	public boolean isPlatnáHodnota() {
    64 		return hodnota != null;
    65 	}
    66 
    67 	public String getHodnota() {
    68 		return hodnota;
    69 	}
    70 
    71 	public final ByteBuffer getHodnotaBajty() {
    72 		return zakóduj(getHodnota());
    73 	}
    74 
    75 	public void setHodnota(String hodnota) {
    76 		this.hodnota = hodnota;
    77 	}
    78 
    79 	public final void setHodnota(ByteBuffer hodnota) {
    80 		setHodnota(dekóduj(hodnota));
    81 	}
    82 
    83 	private static String dekóduj(ByteBuffer bajty) {
    84 		bajty.flip();
    85 		return Charset.defaultCharset().decode(bajty).toString();
    86 	}
    87 
    88 	private static ByteBuffer zakóduj(String text) {
    89 		if (text == null) {
    90 			return null;
    91 		} else {
    92 			return Charset.defaultCharset().encode(text);
    93 		}
    94 	}
    95 }