Util.java

Go to the documentation of this file.
00001 
00002 //                                                                      //
00003 //  Util.java                                                           //
00004 //                                                                      //
00005 //  util.Util -- miscellaneous useful operations                        //
00006 //  Last edited: September 18, 2001 at 6:55 pm                          //
00007 //                                                                      //
00008 //  (c) Copyright 2001 Rice University. All rights reserved.            //
00009 //                                                                      //
00011 
00012 
00013 
00014 
00015 package edu.rice.cs.hpc.data.util;
00016 
00017 
00018 import java.io.File;
00019 import java.io.FilenameFilter;
00020 import java.text.NumberFormat;
00021 import java.text.DecimalFormat;
00022 
00023 
00024 
00026 //  CLASS UTIL                                                          //
00028 
00036 public class Util
00037 {
00038 
00039 
00040 
00041 
00043 //  PRIVATE CONSTANTS                                                   //
00045 
00046 
00047 
00048 
00050 protected static final String SPACES = "                                        ";
00051 
00052 
00053 
00054 
00056 //  STRING CONVERSION                                                   //
00058 
00059 
00060 
00061 
00062 /*************************************************************************
00063  *  Returns the <code>boolean</code> corresponding to a given string.
00064  ************************************************************************/
00065     
00066 public static boolean booleanValue(String s)
00067 {
00068     return Boolean.valueOf(s).booleanValue();
00069 }
00070 
00071 
00072 
00073 
00075 //  TEXT FORMATTING                                                     //
00077 
00078 
00079 
00080 
00081 /*************************************************************************
00082  *  Returns a <code>String</code> of a given number of space characters.
00083  ************************************************************************/
00084     
00085 public static String spaces(int count)
00086 {
00087     Dialogs.Assert(count <= SPACES.length(), "request too long Util::spaces");
00088     
00089     return SPACES.substring(0, count);
00090 }
00091 
00092 
00093 
00094 
00095 /*************************************************************************
00096  *  Fits a string into a field of given width, right adjusted.
00097  ************************************************************************/
00098     
00099 public static String rightJustifiedField(String s, int fieldWidth)
00100 {
00101     String field;
00102     
00103     int padLeft = fieldWidth - s.length();
00104     if( padLeft > 0 )
00105         field = Util.spaces(padLeft) + s;
00106     else
00107         field = s.substring(0, fieldWidth);
00108 
00109     return field;
00110 }
00111 
00112 
00113 
00114 
00115 /*************************************************************************
00116  *  Returns a new <code>DecimalFormat</code> object with a given pattern.
00117  ************************************************************************/
00118     
00119 public static DecimalFormat makeDecimalFormatter(String pattern)
00120 {
00121     // make a formatter, checking that the locale allows this
00122     NumberFormat nf = NumberFormat.getInstance();
00123     Dialogs.Assert( nf instanceof DecimalFormat, "bad arg to Util::makeDecimalFormatter");
00124     DecimalFormat df = (DecimalFormat) nf;
00125     
00126     // apply the given pattern
00127     df.applyPattern(pattern);
00128     
00129     return df;
00130 }
00131 
00132 
00133 
00134 
00135 /*************************************************************************
00136  *  Formats an <code>int</code> in a given format and field width.
00137  *
00138  *  TODO: It might be possible to improve this method's implementation by
00139  *  using class <code>java.text.FieldPosition</code>.
00140  *
00141  ************************************************************************/
00142     
00143 public static String formatInt(int n, DecimalFormat formatter, int fieldWidth)
00144 {
00145     return Util.rightJustifiedField(formatter.format(n), fieldWidth);
00146 }
00147 
00148 
00149 
00150 
00151 /*************************************************************************
00152  *  Formats a <code>double</code> in a given format and field width.
00153  *
00154  *  TODO: It might be possible to improve this method's implementation by
00155  *  using class <code>java.text.FieldPosition</code>.
00156  *
00157  ************************************************************************/
00158     
00159 public static String formatDouble(double d, DecimalFormat formatter, int fieldWidth)
00160 {
00161     return Util.rightJustifiedField(formatter.format(d), fieldWidth);
00162 }
00163 
00164 
00172 public static class FileXMLFilter implements FilenameFilter {
00173     public boolean accept(File pathname, String sName) {
00174         int iLength = sName.length();
00175         if (iLength <4) // the file should contain at least four letters: ".xml"
00176             return false;
00177         String sExtension = (sName.substring(iLength-3, iLength)).toLowerCase();
00178         return (pathname.canRead() && sExtension.endsWith("xml"));
00179     }
00180 }
00181 
00182 
00187 public static class FileThreadsMetricFilter implements FilenameFilter {
00188     private String db_glob;
00189     
00190     public FileThreadsMetricFilter(String pattern) {
00191         db_glob = pattern.replace("*", ".*");
00192     }
00193     
00194     public boolean accept(File dir, String name) {
00195         
00196         boolean b = name.matches(db_glob);
00197         return b;
00198     }
00199     
00200 }
00201 
00202 public static File[] getListOfXMLFiles(String sDir) 
00203 {
00204     // find XML files in this directory
00205     File files = new File(sDir);
00206     // for debugging purpose, let have separate variable
00207     File filesXML[] = files.listFiles(new FileXMLFilter());
00208     return filesXML;
00209 }
00210 
00211 
00212 static public String getObjectID(Object o) {
00213     return Integer.toHexString(System.identityHashCode(o));
00214 }
00215 
00216 
00217 }
00218 
00219 
00220 
00221 
00222 
00223 
00224 
00225 

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1