001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 002// The Molly framework is freely distributable under the terms of an 003// MIT-style license. For details, see the molly pages web site at: 004// http://www.mollypages.org/. Use, modify, have fun ! 005 006package fc.util; 007 008import java.util.*; 009 010/** 011A range of characters. 012<p> 013Note, many methods in this particular class have the same 014signatures as in the apache <tt>commons.lang</tt> package but the 015<tt>implementation</tt> was strictly clean-room. 016<p> 017Thread Safety: This class is not thread safe in the face of any 018concurrent modification (via changing the start/end chars or 019union/intersection operations). 020 021@author hursh jain 022**/ 023public class CharRange extends Range 024{ 025char startChar; 026char endChar; 027 028/** 029Constructs a character range between the specified 030start and end chars (both inclusive). 031 032@throws IllegalArgumentException 033 if the end char is lesser (via a <tt><</tt> comparison) 034 than the start char (equal to the start char is ok) 035**/ 036public CharRange(char start, char end) 037 { 038 Argcheck.istrue(start <= end, "end char lesser than start char in the specified range, start=["+start+"], end=["+end+"]"); 039 startChar = start; 040 endChar = end; 041 } 042 043/** Get the start character for this character range **/ 044public char getStart() 045 { 046 return startChar; 047 } 048 049/** 050Get the end character for this character range. 051**/ 052public char getEnd() 053 { 054 return endChar; 055 } 056 057/** Is the passed in character inside this range **/ 058public boolean inRange(char c) 059 { 060 boolean result = (c >= startChar) && (c <= endChar); 061 062 if (negated) 063 return !result; 064 else 065 return result; 066 } 067 068/** 069Sets the end character for this character range 070 071@param c the end char 072@throws IllegalArgumentException 073 if the end char is lesser (via a <tt><</tt> comparison) 074 than the current start char (equal to the start char is 075 ok) 076**/ 077public void setEnd(char c) 078 { 079 Argcheck.istrue(startChar <= c, "end char lesser than start char in the specified range, start=["+startChar+"], specified end=["+c+"]"); 080 endChar = c; 081 } 082 083/** Set the start character for this character range 084 085@throws IllegalArgumentException 086 if the start char is greater (via a <tt>></tt> comparison) 087 than the current end char (equal to the end char is 088 ok) 089**/ 090public void setStart(char c) 091 { 092 Argcheck.istrue(c <= endChar, "start char greater than end char in the specified range, end=["+endChar+"], specified start=["+c+"]"); 093 startChar = c; 094 } 095 096/** Output a string representation of the character range **/ 097public java.lang.String toString() 098 { 099 String str = "CharRange:["; 100 if (isNegated()) 101 str += "^"; 102 str += "'" + startChar + "'-'" + endChar + "']"; 103 return str; 104 } 105 106public static void main(String[] args) 107 { 108 CharRange r = new CharRange('b', 'z'); 109 System.out.println("constructed range:" + r); 110 //normal 111 test(r); 112 113 //negate 114 r.setNegated(true); 115 System.out.println("setting the range to be negated:" + r); 116 test(r); 117 118 System.out.println("the following should throw an exception"); 119 r = new CharRange('b', 'a'); 120 } 121 122private static void test(CharRange r) 123 { 124 System.out.println("'b' in range:" + r.inRange('b')); 125 System.out.println("'z' in range:" + r.inRange('z')); 126 System.out.println("'d' in range:" + r.inRange('d')); 127 System.out.println("'a' in range:" + r.inRange('a')); 128 System.out.println("'A' in range:" + r.inRange('A')); 129 } 130 131} //~class CharRange