From d2d3e5d2a86cf91a9b38fabe76dc919c77c62852 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 25 Apr 2013 14:48:24 +0200 Subject: [PATCH] Java cleanup, use generics Change-Id: I164e0f8386558641497258cc0a1d731e81a51c15 --- .../net/adaptivebox/global/GlobalFile.java | 556 +++++++++--------- .../net/adaptivebox/global/GlobalString.java | 10 +- 2 files changed, 282 insertions(+), 284 deletions(-) diff --git a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/GlobalFile.java b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/GlobalFile.java index 3dc20c6266d9..14449e2fc078 100644 --- a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/GlobalFile.java +++ b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/GlobalFile.java @@ -1,277 +1,279 @@ -/** - * Description: Global package for file operations. - * - * @ Author Create/Modi Note - * Xiaofeng Xie Jun 15, 2002 - * - * @version 1.0 - * @Since MAOS1.0 - */ - - -package net.adaptivebox.global; - -import java.io.*; -import java.util.*; - -public class GlobalFile { - -// used by the createTempDir to give an index of temp number. - private static int counter = -1; - -/** - * Create a temp directory in the given directory. - * @param prefix the prefix for the directory. - * @param directory the directory that the temp dirctory placed. - * @return If a temp directory is created, return a File Object, else - * return null. - */ - public static File createTempDir(String prefix, String directory) throws IOException { - File f = null; - String tempDir; - boolean isCreated = false; - do { - if (counter == -1) { - counter = new Random().nextInt() & 0xffff; - } - counter++; - if (prefix == null) throw new NullPointerException(); - if (prefix.length() < 3) - throw new IllegalArgumentException("Prefix string too short"); - if (directory == null) { - tempDir = prefix+counter; - } else { - tempDir = getFileLocation(directory,prefix+counter); - } - f = new File(tempDir); - isCreated = f.mkdir(); - } while (!isCreated); - return f; - } - -/** - * Add the given text string to the end of a given file. - * @param inStr The string to be added. - * @param fileStr the name of the file to be added. - */ - public static void addStringToFile(String inStr, String fileStr) throws Exception { - - RandomAccessFile raFile = new RandomAccessFile(fileStr,"rw"); - raFile.seek(raFile.length()); - raFile.writeBytes(inStr); - raFile.close(); - - -// String oldFileStr = getStringFromFile(fileStr); -// String newFileStr = inStr; -// if (oldFileStr != null) { -// newFileStr = oldFileStr+inStr; -// } -// saveStringToFile(newFileStr,fileStr); - - } - - public static Object loadObjectFromFile(String fileName) throws Exception { - FileInputStream fis = new FileInputStream(fileName); - ObjectInputStream ois = new ObjectInputStream(fis); - Object obj = ois.readObject(); - ois.close(); - return obj; - } - - public static void saveObjectToFile(String fileName, Object obj) throws Exception { - FileOutputStream ostream = new FileOutputStream(fileName); - ObjectOutputStream p = new ObjectOutputStream(ostream); - p.writeObject(obj); - p.flush(); - ostream.close(); - } - -/** - * Save the given text string to a given file. - * @param inStr The string to be saved. - * @param fileStr the name of the file to be saved. - */ - public static void saveStringToFile(String inStr, String fileStr) throws Exception{ - new File(new File(fileStr).getParent()).mkdirs(); - FileOutputStream pspOutputStream = new FileOutputStream(new File(fileStr)); - pspOutputStream.write(inStr.getBytes()); - pspOutputStream.close(); - } - -/** - * Load text string from a given file. - * @param fileStr the name of the file to be loaded. - * @return A text string that is the content of the file. if the given file is - * not exist, then return null. - */ - public static String getStringFromFile(String fileStr) throws Exception { - String getStr = null; - FileInputStream pspInputStream = new FileInputStream(fileStr); - byte[] pspFileBuffer = new byte[pspInputStream.available()]; - pspInputStream.read(pspFileBuffer); - pspInputStream.close(); - getStr = new String(pspFileBuffer); - return(getStr); - } - -/** - * Load curve data from a specified file. - * @param fileStr the name of the file to be loaded. - * @return A Vector that include the curve data. - */ - public static Vector getCurveDataFromFile(String fileName) { - File file = new File(fileName); - if(!file.exists()){ - //showMessage(); - return null; - } - //open data file - FileInputStream inStream = null; - BufferedReader inReader = null; - try{ - inStream = new FileInputStream(file); - inReader = new BufferedReader(new InputStreamReader(inStream)); - }catch(Exception e){ - //showMessage(); - return null;//Data file open error. - } - Vector xaxes = new Vector(1,1); - Vector yaxes = new Vector(1,1); - try{ - StringTokenizer st; - String s; - boolean start = false; - while(inReader.ready()!=false){ - st = new StringTokenizer(inReader.readLine()); - over:{ - while(!st.hasMoreTokens()){//Justify blank lines. - if(inReader.ready()!=false){ - st = new StringTokenizer(inReader.readLine()); - }else - break over; - } - s = st.nextToken(); - if((!start)&&(!s.startsWith("@"))) - break over; - if(!start){ - start = true; - break over; - } - if(s.startsWith("#")||s.startsWith("$")||s.startsWith("/")) break over;//Justify comment line. - Double xaxis = null; - Double yaxis = null; - try{ - xaxis = Double.valueOf(s); - xaxes.addElement(xaxis); - }catch(Exception e){ - //showMessage(); - inReader.close(); - inStream.close(); - return null;//Data file data format error. - } - s = st.nextToken(); - try{ - yaxis = Double.valueOf(s); - yaxes.addElement(yaxis); - }catch(Exception e){ - //showMessage(); - inReader.close(); - inStream.close(); - return null;//Data file data format error. - } - } - } - }catch(Exception e){ - //showMessage(); - return null;//Uncertain data file error. - } - Vector curveData = new Vector(); - curveData.addElement(xaxes); - curveData.addElement(yaxes); - return(curveData); - } - -/** - * Get a full path of a given file name and a directory name. - * @param fileName the name of the file. - * @param dir the name of directory. - * @return The full path. - */ - public static String getFileLocation(String dir, String fileName) { - String realDir = dir; - while (realDir.length()>0 && (realDir.endsWith("/")||realDir.endsWith("\\"))) { - realDir = dir.substring(0, dir.length()-1); - } - return realDir+BasicTag.FILE_SEP_TAG+fileName; - } - - public static String getFileName(String nameBody, String suffix) { - if (suffix==null || suffix.trim().length()==0) { - return nameBody; - } - String fileName = nameBody; - if(nameBody.endsWith(".")) { - return fileName+suffix; - } else { - return nameBody+"."+suffix; - } - } - - public static String getFileLocation(String dir, String fileNameBody, String fileNameSuffix) { - String filename = getFileName(fileNameBody, fileNameSuffix); - return getFileLocation(dir, filename); - } - - public static void clear(String fileStr) throws Exception { - File file = new File(fileStr); - if(file.isFile()) { - file.delete(); - return; - } - String[] fileNames = file.list(); - if (fileNames==null) { - return; - } - for (int i=0; i> getCurveDataFromFile(String fileName) { + File file = new File(fileName); + if(!file.exists()){ + //showMessage(); + return null; + } + //open data file + FileInputStream inStream = null; + BufferedReader inReader = null; + try{ + inStream = new FileInputStream(file); + inReader = new BufferedReader(new InputStreamReader(inStream)); + }catch(Exception e){ + //showMessage(); + return null;//Data file open error. + } + ArrayList xaxes = new ArrayList(1); + ArrayList yaxes = new ArrayList(1); + try{ + StringTokenizer st; + String s; + boolean start = false; + while(inReader.ready()!=false){ + st = new StringTokenizer(inReader.readLine()); + over:{ + while(!st.hasMoreTokens()){//Justify blank lines. + if(inReader.ready()!=false){ + st = new StringTokenizer(inReader.readLine()); + }else + break over; + } + s = st.nextToken(); + if((!start)&&(!s.startsWith("@"))) + break over; + if(!start){ + start = true; + break over; + } + if(s.startsWith("#")||s.startsWith("$")||s.startsWith("/")) break over;//Justify comment line. + Double xaxis = null; + Double yaxis = null; + try{ + xaxis = Double.valueOf(s); + xaxes.add(xaxis); + }catch(NumberFormatException e){ + //showMessage(); + inReader.close(); + inStream.close(); + return null;//Data file data format error. + } + s = st.nextToken(); + try{ + yaxis = Double.valueOf(s); + yaxes.add(yaxis); + }catch(NumberFormatException e){ + //showMessage(); + inReader.close(); + inStream.close(); + return null;//Data file data format error. + } + } + } + }catch(Exception e){ + //showMessage(); + return null;//Uncertain data file error. + } + ArrayList> curveData = new ArrayList>(2); + curveData.add(xaxes); + curveData.add(yaxes); + return curveData; + } + +/** + * Get a full path of a given file name and a directory name. + * @param fileName the name of the file. + * @param dir the name of directory. + * @return The full path. + */ + public static String getFileLocation(String dir, String fileName) { + String realDir = dir; + while (realDir.length()>0 && (realDir.endsWith("/")||realDir.endsWith("\\"))) { + realDir = dir.substring(0, dir.length()-1); + } + return realDir+BasicTag.FILE_SEP_TAG+fileName; + } + + public static String getFileName(String nameBody, String suffix) { + if (suffix==null || suffix.trim().length()==0) { + return nameBody; + } + String fileName = nameBody; + if(nameBody.endsWith(".")) { + return fileName+suffix; + } else { + return nameBody+"."+suffix; + } + } + + public static String getFileLocation(String dir, String fileNameBody, String fileNameSuffix) { + String filename = getFileName(fileNameBody, fileNameSuffix); + return getFileLocation(dir, filename); + } + + public static void clear(String fileStr) throws Exception { + File file = new File(fileStr); + if(file.isFile()) { + file.delete(); + return; + } + String[] fileNames = file.list(); + if (fileNames==null) { + return; + } + for (int i=0; i v = new ArrayList(); StringTokenizer t = new StringTokenizer(input, tokenKey); - String cmd[]; while (t.hasMoreTokens()) - v.addElement(t.nextToken()); - cmd = new String[v.size()]; - for (int i = 0; i < cmd.length; i++) - cmd[i] = (String) v.elementAt(i); - return cmd; + v.add(t.nextToken()); + return v.toArray(new String[v.size()]); } public static String[] getMeaningfulLines(String srcStr) throws Exception {