java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Atribut.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 11 Dec 2023 00:10:33 +0100
branchv_0
changeset 27 dc5786f3482b
parent 19 adea235f456a
permissions -rw-r--r--
oprava verze licence / fix license version: GNU GPLv3
     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.
     8  * 
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  * 
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.rozsireneAtributy;
    18 
    19 import java.nio.ByteBuffer;
    20 import java.nio.charset.Charset;
    21 
    22 public class Atribut {
    23 
    24 	private String klíč;
    25 	private String hodnota;
    26 
    27 	public Atribut(String klíč, String hodnota) {
    28 		this.klíč = klíč;
    29 		this.hodnota = hodnota;
    30 	}
    31 
    32 	public Atribut(String klíč, ByteBuffer hodnota) {
    33 		this.klíč = klíč;
    34 		setHodnota(hodnota);
    35 	}
    36 
    37 	public Atribut() {
    38 	}
    39 
    40 	public String getKlíč() {
    41 		return klíč;
    42 	}
    43 
    44 	public void setKlíč(String klíč) {
    45 		this.klíč = klíč;
    46 	}
    47 
    48 	/**
    49 	 * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit
    50 	 * TODO: další kontroly?
    51 	 * @return jestli je platný
    52 	 */
    53 	public boolean isPlatnýKlíč() {
    54 		return klíč != null && klíč.length() > 0;
    55 	}
    56 
    57 	/**
    58 	 * nulová hodnota → smazání atributu
    59 	 * (ale může být prázdný řetězec)
    60 	 * @return jestli je platná
    61 	 */
    62 	public boolean isPlatnáHodnota() {
    63 		return hodnota != null;
    64 	}
    65 
    66 	public String getHodnota() {
    67 		return hodnota;
    68 	}
    69 
    70 	public final ByteBuffer getHodnotaBajty() {
    71 		return zakóduj(getHodnota());
    72 	}
    73 
    74 	public void setHodnota(String hodnota) {
    75 		this.hodnota = hodnota;
    76 	}
    77 
    78 	public final void setHodnota(ByteBuffer hodnota) {
    79 		setHodnota(dekóduj(hodnota));
    80 	}
    81 
    82 	private static String dekóduj(ByteBuffer bajty) {
    83 		bajty.flip();
    84 		return Charset.defaultCharset().decode(bajty).toString();
    85 	}
    86 
    87 	private static ByteBuffer zakóduj(String text) {
    88 		if (text == null) {
    89 			return null;
    90 		} else {
    91 			return Charset.defaultCharset().encode(text);
    92 		}
    93 	}
    94 }