java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/Atribut.java
author František Kučera <franta-hg@frantovo.cz>
Wed Dec 15 23:58:34 2010 +0100 (2010-12-15)
changeset 11 9b399cde6a3b
parent 10 ed2b6ebf138d
child 19 c20edbed09c3
permissions -rw-r--r--
Klávesové zkratky, kontroly, čeština.
     1 package cz.frantovo.rozsireneAtributy;
     2 
     3 import java.nio.ByteBuffer;
     4 import java.nio.charset.Charset;
     5 
     6 public class Atribut {
     7 
     8 	private String klíč;
     9 	private String hodnota;
    10 
    11 	public Atribut(String klíč, String hodnota) {
    12 		this.klíč = klíč;
    13 		this.hodnota = hodnota;
    14 	}
    15 
    16 	public Atribut(String klíč, ByteBuffer hodnota) {
    17 		this.klíč = klíč;
    18 		setHodnota(hodnota);
    19 	}
    20 
    21 	public Atribut() {
    22 	}
    23 
    24 	public String getKlíč() {
    25 		return klíč;
    26 	}
    27 
    28 	public void setKlíč(String klíč) {
    29 		this.klíč = klíč;
    30 	}
    31 
    32 	/**
    33 	 * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit
    34 	 * TODO: další kontroly?
    35 	 * @return jestli je platný
    36 	 */
    37 	public boolean isPlatnýKlíč() {
    38 		return klíč != null && klíč.length() > 0;
    39 	}
    40 
    41 	/**
    42 	 * nulová hodnota → smazání atributu
    43 	 * (ale může být prázdný řetězec)
    44 	 * @return jestli je platná
    45 	 */
    46 	public boolean isPlatnáHodnota() {
    47 		return hodnota != null;
    48 	}
    49 
    50 	public String getHodnota() {
    51 		return hodnota;
    52 	}
    53 
    54 	public final ByteBuffer getHodnotaBajty() {
    55 		return zakóduj(getHodnota());
    56 	}
    57 
    58 	public void setHodnota(String hodnota) {
    59 		this.hodnota = hodnota;
    60 	}
    61 
    62 	public final void setHodnota(ByteBuffer hodnota) {
    63 		setHodnota(dekóduj(hodnota));
    64 	}
    65 
    66 	private static String dekóduj(ByteBuffer bajty) {
    67 		bajty.flip();
    68 		return Charset.defaultCharset().decode(bajty).toString();
    69 	}
    70 
    71 	private static ByteBuffer zakóduj(String text) {
    72 		if (text == null) {
    73 			return null;
    74 		} else {
    75 			return Charset.defaultCharset().encode(text);
    76 		}
    77 	}
    78 }