23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> I/O<br />

from inside the class with established values, these checks are not absolutely necessary; but<br />

they did help me catch several data entry errors as this class was developed.<br />

A private constructor is unusual but not unheard of, especially when there are only a finite<br />

number of valid objects. There are only a few dozen character blocks defined in Unicode 2.0,<br />

each with a precise range. Declaring the constructor private prevents arbitrary blocks from<br />

being created. Instead, a <strong>Java</strong> static block initializes all possible character blocks the first time<br />

the class is loaded and stores them in a Hashtable called blocks. From this point forward,<br />

the static getBlock() method can retrieve a particular block by its name. The names of the<br />

blocks are used as keys in the hash table. The static getNames() method returns a list of the<br />

available blocks, and the static getNumBlocks() method returns the number of blocks stored<br />

in the hash table. There are several instance methods as well. The getStart(), getEnd(),<br />

and getName() methods are accessors that return the values of the respective fields. Finally,<br />

getCharactersInBlock() returns an array of chars containing each of the defined<br />

characters in the block in sequence. However, many of the blocks contain undefined empty<br />

spaces. For example, characters 983, 984, and 985 in the middle of the Greek block are not<br />

defined. Testing each character in the range with the static method Character.isDefined()<br />

weeds out undefined characters.<br />

Example 14.1. The CharacterBlock Class<br />

import java.util.*;<br />

public class CharacterBlock {<br />

String name;<br />

int start;<br />

int end;<br />

private static Hashtable blocks = new Hashtable(66);<br />

/* The blocks given here are as listed in The Unicode Standard,<br />

Version 2.0. Ranges are given in hexadecimal as in that<br />

document. It is not difficult to add additional blocks to this<br />

list as they're defined.<br />

*/<br />

static {<br />

// General Scripts<br />

makeBlock("Basic Latin", 0x0000, 0x007F);<br />

makeBlock("Latin-1 Supplement", 0x0080, 0x00FF);<br />

makeBlock("Latin Extended-A", 0x0100, 0x017F);<br />

makeBlock("Latin Extended-B", 0x0180, 0x024F);<br />

makeBlock("IPA Extensions", 0x0250, 0x02AF);<br />

makeBlock("Spacing Modifier Letters", 0x02B0, 0x02FF);<br />

makeBlock("Combining Diacritical Marks", 0x0300, 0x036F);<br />

makeBlock("Greek", 0x0370, 0x03FF);<br />

makeBlock("Cyrillic", 0x0400, 0x04FF);<br />

makeBlock("Armenian", 0x0530, 0x058F);<br />

makeBlock("Hebrew", 0x0590, 0x05FF);<br />

makeBlock("Arabic", 0x0600, 0x06FF);<br />

makeBlock("Devanagari", 0x0900, 0x097F);<br />

makeBlock("Bengali", 0x0980, 0x09FF);<br />

makeBlock("Gurmukhi", 0x0A00, 0x0A7F);<br />

makeBlock("Gujarati", 0x0A80, 0x0AFF);<br />

makeBlock("Oriya", 0x0B00, 0x0B7F);<br />

makeBlock("Tamil", 0x0B80, 0x0BFF);<br />

340

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!