diff options
Diffstat (limited to 'modules/hebrew-wlc/WLC2OSIS/Utilities/Fmt.java')
-rw-r--r-- | modules/hebrew-wlc/WLC2OSIS/Utilities/Fmt.java | 710 |
1 files changed, 355 insertions, 355 deletions
diff --git a/modules/hebrew-wlc/WLC2OSIS/Utilities/Fmt.java b/modules/hebrew-wlc/WLC2OSIS/Utilities/Fmt.java index 193ab42..de6ae93 100644 --- a/modules/hebrew-wlc/WLC2OSIS/Utilities/Fmt.java +++ b/modules/hebrew-wlc/WLC2OSIS/Utilities/Fmt.java @@ -1,355 +1,355 @@ -package Utilities ;
-
-import java.text.* ;
-import java.text.Format.* ;
-import java.io.* ;
-import java.awt.*;
-//==============================================================================
-/**
-*
-* <b>Formating tools.</b><p>
-*
-* <pre>
-* F = new Fmt() ;
-* System.out.println(F.i(2,10)) ; // 10 digit integer field.
-* System.out.println(F.f(3.1415,10, 2)) ; // 10 digit F10.2 field.
-* System.out.println(F.fill("-", 10)) ; // 10 digits of "-".
-* System.out.println(F.prepad(F.fill("-",10), 12) ;
-* // 2 digits of blank before fill.
-* </pre>
-*
-*/
-//==============================================================================
-
-public class Fmt{
-
-NumberFormat NF = NumberFormat.getInstance() ; // Provides commas in integers
-
-//-----------------------------------------------------------------------------
-/**
-* Simple constructor: Fmt F = new Fmt() ;
-*
-*/
-public Fmt(){
-}
-//-----------------------------------------------------------------------------
-/**
-*
-* Yields a string of specified FieldWidth and DecimalDigits containing
-* the given double.<p>
-*
-* @param f double to be converted.
-* @param FieldWidth Maximum number of characters to be produced. If more
-* characters are produced, the field is filled with *.
-* @param DecimalDigits int number of points after the decimal point.
-* @return String String of exactly FieldWidth characters.
-*
-*/
-public String f( double f, int FieldWidth, int DecimalDigits) {
- if(DecimalDigits>FieldWidth-2){
- String s = fill("!", FieldWidth) ;
- return s ;
- }
- String FormatString = "0." ;
- for (int k = 0; k < DecimalDigits; k++){
- FormatString = FormatString + "0" ;
- }
- while (FormatString.length() < FieldWidth){
- FormatString = "#" + FormatString ;
- }
-
- DecimalFormat DF = new DecimalFormat(FormatString) ;
- String s = DF.format(f) ;
- if (s.length()< FieldWidth){
- s = prepad(s, FieldWidth) ;
- }
- else if(s.length()>FieldWidth){
- s = fill("*", FieldWidth) ;
- }
- return s ;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Yields a string of specified FieldWidth containing the given integer.<p>
-*
-* @param i integer to be converted.
-* @param FieldWidth Maximum number of characters to be produced. If more
-* characters are produced, the field is filled with *.
-* @return String String of exactly FieldWidth characters.
-*
-*/
-public String i( int i, int FieldWidth) {
- String s = NF.format(i) ;
- if (s.length()< FieldWidth){
- s = prepad(s, FieldWidth) ;
- }
- else if(s.length()>FieldWidth){
- s = "";
- for(int k = 0; k<FieldWidth; k++){
- s = s + "*" ;
- }
- }
- return s ;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Yields a string containing the given integer.<p>
-*
-* @param i integer to be converted.
-* @return String String representing i.
-*
-*/
-public String i( int i) {
- return NF.format(i) ;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Yields a string of Width specified characters.<p>
-*
-* @param c String (usually a single character) to be repeated.
-* @param Width int number of times to repeat S.
-* @return String of Width characters.
-*
-*/
-public String fill( String c, int Width){
- String s = "" ;
- for(int i = 0; i < Width; i++){
- s = s + c ;
- }
- return s;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Prints a string of Width specified characters to a specified PrintStream.<p>
-*
-* @param P PrintStream such as System.out.
-* @param c String (usually a single character) to be repeated.
-* @param Width int number of times to repeat S.
-*
-*/
-public void bar(PrintStream P, String c, int Width){
- String s = "" ;
- for(int i = 0; i < Width; i++){
- s = s + c ;
- }
- P.println(s) ;
- return ;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Prints a string of Width specified characters to System.out.<p>
-*
-* @param c String (usually a single character) to be repeated.
-* @param Width int number of times to repeat S.
-* @return String String of Width characters.
-*
-*/
-public void bar(String c, int Width){
- bar(System.out, c, Width) ;
- return ;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Removes blanks anywhere in String.<p>
-*
-* @param c String to be deblanked.
-* @return String String without any blanks.
-*
-*/
-public String deblank(String c){
- String s = "" ;
- for (int i = 0; i < c.length(); i++){
- if(c.charAt(i) != ' '){
- s = s + c.charAt(i) ;
- }
- }
- return s ;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Pads the front of a String, giving a String of a specified length.<p>
-*
-* @param s String to be prepaded.
-* @param n Desired length of output string.
-* @return String Prepadded String.
-*
-*/
-public String prepad(String s, int n){
- int len = s.length() ;
- if (len == n){ // String is already long enough.
- return s ;
- }
- else if(len > n){
- return fill("!", n) ;
- }
-// Prepad
- String OutString = "" ;
- int Pads = n - len ;
- for (int i = 0; i < Pads; i++){
- OutString = OutString + " " ;
- }
-// Original String
- OutString = OutString + s ;
- return OutString ;
- }
-//-----------------------------------------------------------------------------
-/**
-*
-* Pads the end of a String, giving a String of a specified length.<p>
-*
-* @param s String to be postpaded.
-* @param n Desired length of output string.
-* @return String Prepadded String.
-*
-*/
-public String postpad(String s, int n){
- int len = s.length() ;
- if (len == n){ // String is already long enough.
- return s ;
- }
- else if(len > n){
- return fill("!", n) ;
- }
-// Original String
-// Postpad
- String OutString = "" ;
- for (int i = 0; i < len; i++){
- OutString = OutString + s.charAt(i) ;
- }
-// Postpad
- while (OutString.length() <= n){
- OutString = OutString + " " ;
- }
- return OutString ;
- }
-//-----------------------------------------------------------------------------
-/**
-* Gives a convenient maximum size for a text area
-* specified in rows, columns.<p>
-*
-* @param font Font of text.
-* @param rows int giving number of rows of text.
-* @param columns int giving number of columns of text.
-* @return D Dimension of best size.
-*/
-public Dimension areaSize( Font font, int rows, int columns ){
- double XFactor = 0.70 ;
-// double YFactor = 1.42;
- double YFactor = 1.65;
-
- Integer X = new Integer( font.getSize() ) ;
- double x = XFactor*X.doubleValue()*columns ;
- Integer Y = new Integer( font.getSize() ) ;
- double y = YFactor*Y.doubleValue()*rows ;
- Dimension D = new Dimension( (new Double(x)).intValue(),
- (new Double(y)).intValue() ) ;
- return D ;
- }
-//-----------------------------------------------------------------------------
-/**
-* Gives a convenient maximum size for a text area
-* specified as a dimension.<p>
-*
-* @param font Font of text.
-* @param d Dimnsion object giving number of rows and columns of text.
-* @return D Dimension of best size.
-*/
-public Dimension areaSize( Font font, Dimension d){
- double XFactor = 0.70 ;
- double YFactor = 1.42;
-
- Integer X = new Integer( font.getSize() ) ;
- double x = XFactor*X.doubleValue()*d.getWidth() ;
- Integer Y = new Integer( font.getSize() ) ;
- double y = YFactor*Y.doubleValue()*d.getHeight() ;
- Dimension D = new Dimension( (new Double(x)).intValue(),
- (new Double(y)).intValue() ) ;
- return D ;
- }
-//-----------------------------------------------------------------------------
-/**
-* Removes the extension, including the ".", from a file name String.<p>
-*
-* If the Name doesn't contain a ".", the original Name is returned.
-*
-* @param Name String containing file name.
-* @return String with extension and "." removed.
-*/
-public static String removeExtension( String Name){
-
- boolean HasDot = false ;
- for (int k = 0; k < Name.length() ; k++){
- if(Name.charAt(k) == '.'){
- HasDot = true ;
- }
- }
- if(!HasDot) return Name ;
-
- String NewName = "" ;
- for (int k = Name.length()-1; k >= 0; k--){
- if( Name.charAt(k) == '.'){
- for (int j = 0; j < k ; j ++) {
- NewName = NewName + Name.charAt(j) ;
- }
- break ;
- }
- }
- if (NewName == "."){
- NewName = "" ;
- }
- return NewName ;
- }
-//-----------------------------------------------------------------------------
-/**
-* gets the extension, including the ".", from a file name String.<p>
-*
-* If the Name doesn't contain a ".", the original Name is returned.
-* From Sun JFileChooser tutorial
-*
-* @param s String containing file name with extension.
-* @return String giving the extension.
-*/
-public static String getExtension( String s){
-
- String ext = "";
- int i = s.lastIndexOf('.');
-
- if (i > 0 && i < s.length() - 1) {
- ext = s.substring(i+1).toLowerCase();
- }
- return ext;
- }
-//-----------------------------------------------------------------------------
-/**
-* Prints the name of the calling location
-* plus the current Thread to System.out.<p>
-*
-* @param Location String giving location of call.
-*/
-public void printThread( String Location){
- Thread t = Thread.currentThread() ;
- System.out.println("At " + Location + ", the current Thread is "
- + t.getName() );
- return;
- }
-//-----------------------------------------------------------------------------
-/**
-* Test program.<p>
-*/
-public static void main( String[] args) {
- Fmt F = new Fmt() ;
- String Name = "XYX.txt.spa" ;
- System.out.println(Name) ;
- System.out.println(F.removeExtension(Name)) ;
- System.out.println(F.removeExtension(F.removeExtension(Name))) ;
- }
- } // End of class
-//==============================================================================
-//==============================================================================
+package Utilities ; + +import java.text.* ; +import java.text.Format.* ; +import java.io.* ; +import java.awt.*; +//============================================================================== +/** +* +* <b>Formating tools.</b><p> +* +* <pre> +* F = new Fmt() ; +* System.out.println(F.i(2,10)) ; // 10 digit integer field. +* System.out.println(F.f(3.1415,10, 2)) ; // 10 digit F10.2 field. +* System.out.println(F.fill("-", 10)) ; // 10 digits of "-". +* System.out.println(F.prepad(F.fill("-",10), 12) ; +* // 2 digits of blank before fill. +* </pre> +* +*/ +//============================================================================== + +public class Fmt{ + +NumberFormat NF = NumberFormat.getInstance() ; // Provides commas in integers + +//----------------------------------------------------------------------------- +/** +* Simple constructor: Fmt F = new Fmt() ; +* +*/ +public Fmt(){ +} +//----------------------------------------------------------------------------- +/** +* +* Yields a string of specified FieldWidth and DecimalDigits containing +* the given double.<p> +* +* @param f double to be converted. +* @param FieldWidth Maximum number of characters to be produced. If more +* characters are produced, the field is filled with *. +* @param DecimalDigits int number of points after the decimal point. +* @return String String of exactly FieldWidth characters. +* +*/ +public String f( double f, int FieldWidth, int DecimalDigits) { + if(DecimalDigits>FieldWidth-2){ + String s = fill("!", FieldWidth) ; + return s ; + } + String FormatString = "0." ; + for (int k = 0; k < DecimalDigits; k++){ + FormatString = FormatString + "0" ; + } + while (FormatString.length() < FieldWidth){ + FormatString = "#" + FormatString ; + } + + DecimalFormat DF = new DecimalFormat(FormatString) ; + String s = DF.format(f) ; + if (s.length()< FieldWidth){ + s = prepad(s, FieldWidth) ; + } + else if(s.length()>FieldWidth){ + s = fill("*", FieldWidth) ; + } + return s ; + } +//----------------------------------------------------------------------------- +/** +* +* Yields a string of specified FieldWidth containing the given integer.<p> +* +* @param i integer to be converted. +* @param FieldWidth Maximum number of characters to be produced. If more +* characters are produced, the field is filled with *. +* @return String String of exactly FieldWidth characters. +* +*/ +public String i( int i, int FieldWidth) { + String s = NF.format(i) ; + if (s.length()< FieldWidth){ + s = prepad(s, FieldWidth) ; + } + else if(s.length()>FieldWidth){ + s = ""; + for(int k = 0; k<FieldWidth; k++){ + s = s + "*" ; + } + } + return s ; + } +//----------------------------------------------------------------------------- +/** +* +* Yields a string containing the given integer.<p> +* +* @param i integer to be converted. +* @return String String representing i. +* +*/ +public String i( int i) { + return NF.format(i) ; + } +//----------------------------------------------------------------------------- +/** +* +* Yields a string of Width specified characters.<p> +* +* @param c String (usually a single character) to be repeated. +* @param Width int number of times to repeat S. +* @return String of Width characters. +* +*/ +public String fill( String c, int Width){ + String s = "" ; + for(int i = 0; i < Width; i++){ + s = s + c ; + } + return s; + } +//----------------------------------------------------------------------------- +/** +* +* Prints a string of Width specified characters to a specified PrintStream.<p> +* +* @param P PrintStream such as System.out. +* @param c String (usually a single character) to be repeated. +* @param Width int number of times to repeat S. +* +*/ +public void bar(PrintStream P, String c, int Width){ + String s = "" ; + for(int i = 0; i < Width; i++){ + s = s + c ; + } + P.println(s) ; + return ; + } +//----------------------------------------------------------------------------- +/** +* +* Prints a string of Width specified characters to System.out.<p> +* +* @param c String (usually a single character) to be repeated. +* @param Width int number of times to repeat S. +* @return String String of Width characters. +* +*/ +public void bar(String c, int Width){ + bar(System.out, c, Width) ; + return ; + } +//----------------------------------------------------------------------------- +/** +* +* Removes blanks anywhere in String.<p> +* +* @param c String to be deblanked. +* @return String String without any blanks. +* +*/ +public String deblank(String c){ + String s = "" ; + for (int i = 0; i < c.length(); i++){ + if(c.charAt(i) != ' '){ + s = s + c.charAt(i) ; + } + } + return s ; + } +//----------------------------------------------------------------------------- +/** +* +* Pads the front of a String, giving a String of a specified length.<p> +* +* @param s String to be prepaded. +* @param n Desired length of output string. +* @return String Prepadded String. +* +*/ +public String prepad(String s, int n){ + int len = s.length() ; + if (len == n){ // String is already long enough. + return s ; + } + else if(len > n){ + return fill("!", n) ; + } +// Prepad + String OutString = "" ; + int Pads = n - len ; + for (int i = 0; i < Pads; i++){ + OutString = OutString + " " ; + } +// Original String + OutString = OutString + s ; + return OutString ; + } +//----------------------------------------------------------------------------- +/** +* +* Pads the end of a String, giving a String of a specified length.<p> +* +* @param s String to be postpaded. +* @param n Desired length of output string. +* @return String Prepadded String. +* +*/ +public String postpad(String s, int n){ + int len = s.length() ; + if (len == n){ // String is already long enough. + return s ; + } + else if(len > n){ + return fill("!", n) ; + } +// Original String +// Postpad + String OutString = "" ; + for (int i = 0; i < len; i++){ + OutString = OutString + s.charAt(i) ; + } +// Postpad + while (OutString.length() <= n){ + OutString = OutString + " " ; + } + return OutString ; + } +//----------------------------------------------------------------------------- +/** +* Gives a convenient maximum size for a text area +* specified in rows, columns.<p> +* +* @param font Font of text. +* @param rows int giving number of rows of text. +* @param columns int giving number of columns of text. +* @return D Dimension of best size. +*/ +public Dimension areaSize( Font font, int rows, int columns ){ + double XFactor = 0.70 ; +// double YFactor = 1.42; + double YFactor = 1.65; + + Integer X = new Integer( font.getSize() ) ; + double x = XFactor*X.doubleValue()*columns ; + Integer Y = new Integer( font.getSize() ) ; + double y = YFactor*Y.doubleValue()*rows ; + Dimension D = new Dimension( (new Double(x)).intValue(), + (new Double(y)).intValue() ) ; + return D ; + } +//----------------------------------------------------------------------------- +/** +* Gives a convenient maximum size for a text area +* specified as a dimension.<p> +* +* @param font Font of text. +* @param d Dimnsion object giving number of rows and columns of text. +* @return D Dimension of best size. +*/ +public Dimension areaSize( Font font, Dimension d){ + double XFactor = 0.70 ; + double YFactor = 1.42; + + Integer X = new Integer( font.getSize() ) ; + double x = XFactor*X.doubleValue()*d.getWidth() ; + Integer Y = new Integer( font.getSize() ) ; + double y = YFactor*Y.doubleValue()*d.getHeight() ; + Dimension D = new Dimension( (new Double(x)).intValue(), + (new Double(y)).intValue() ) ; + return D ; + } +//----------------------------------------------------------------------------- +/** +* Removes the extension, including the ".", from a file name String.<p> +* +* If the Name doesn't contain a ".", the original Name is returned. +* +* @param Name String containing file name. +* @return String with extension and "." removed. +*/ +public static String removeExtension( String Name){ + + boolean HasDot = false ; + for (int k = 0; k < Name.length() ; k++){ + if(Name.charAt(k) == '.'){ + HasDot = true ; + } + } + if(!HasDot) return Name ; + + String NewName = "" ; + for (int k = Name.length()-1; k >= 0; k--){ + if( Name.charAt(k) == '.'){ + for (int j = 0; j < k ; j ++) { + NewName = NewName + Name.charAt(j) ; + } + break ; + } + } + if (NewName == "."){ + NewName = "" ; + } + return NewName ; + } +//----------------------------------------------------------------------------- +/** +* gets the extension, including the ".", from a file name String.<p> +* +* If the Name doesn't contain a ".", the original Name is returned. +* From Sun JFileChooser tutorial +* +* @param s String containing file name with extension. +* @return String giving the extension. +*/ +public static String getExtension( String s){ + + String ext = ""; + int i = s.lastIndexOf('.'); + + if (i > 0 && i < s.length() - 1) { + ext = s.substring(i+1).toLowerCase(); + } + return ext; + } +//----------------------------------------------------------------------------- +/** +* Prints the name of the calling location +* plus the current Thread to System.out.<p> +* +* @param Location String giving location of call. +*/ +public void printThread( String Location){ + Thread t = Thread.currentThread() ; + System.out.println("At " + Location + ", the current Thread is " + + t.getName() ); + return; + } +//----------------------------------------------------------------------------- +/** +* Test program.<p> +*/ +public static void main( String[] args) { + Fmt F = new Fmt() ; + String Name = "XYX.txt.spa" ; + System.out.println(Name) ; + System.out.println(F.removeExtension(Name)) ; + System.out.println(F.removeExtension(F.removeExtension(Name))) ; + } + } // End of class +//============================================================================== +//============================================================================== |