bump product version to 4.1.6.2
[LibreOffice.git] / nlpsolver / ThirdParty / EvolutionarySolver / src / net / adaptivebox / global / GlobalString.java
blob28b40231eb306592dd425fc98b28228abb86bf1f
1 /**
2 * Description: operations for the a text string.
4 * @ Author Create/Modi Note
5 * Xiaofeng Xie Feb 22, 2001
6 * Xiaofeng Xie May 12, 2004
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * Please acknowledge the author(s) if you use this code in any way.
20 * @version 1.0
21 * @Since MAOS1.0
24 package net.adaptivebox.global;
26 import java.io.*;
27 import java.util.*;
29 public class GlobalString {
30 public static final String NEGLECT_TAG = "#$@";
31 public static final String EQUAL_TAG = "=";
33 /**
34 * Tokenize a String with given key.
35 * @param input the String to be tokenized.
36 * @param tokenKey the delimiters.
37 * @return a String array that include the elements of input string that
38 * divided by the tokenKey.
40 public static String[] tokenize(String input , String tokenKey) {
41 ArrayList<String> v = new ArrayList<String>();
42 StringTokenizer t = new StringTokenizer(input, tokenKey);
43 while (t.hasMoreTokens())
44 v.add(t.nextToken());
45 return v.toArray(new String[v.size()]);
48 public static String[] getMeaningfulLines(String srcStr) throws Exception {
49 return getMeaningfulLines(srcStr, NEGLECT_TAG);
52 public static String getMeaningfulLine(BufferedReader outReader) throws Exception {
53 return getMeaningfulLine(outReader, NEGLECT_TAG);
56 public static int getCharLoc(char data, String str) {
57 for(int i=0; i<str.length(); i++) {
58 if(str.charAt(i)==data) return i;
60 return -1;
62 public static String trim(String origStr, String discardStr) {
63 String str = origStr;
64 do {
65 if(str.length()==0) return str;
66 if(getCharLoc(str.charAt(0), discardStr)!=-1) str = str.substring(1);
67 else if(getCharLoc(str.charAt(str.length()-1), discardStr)!=-1) str = str.substring(0, str.length()-1);
68 else {return str;}
69 } while(true);
72 public static boolean getFirstCharExist(String str, String chars) throws Exception {
73 int neglectFirstCharLength = chars.length();
74 for(int i=0; i<neglectFirstCharLength; i++) {
75 if(str.startsWith(chars.substring(i, i+1))) {
76 return true;
79 return false;
82 public static String getMeaningfulLine(BufferedReader outReader, String neglectFirstChars) throws Exception {
83 String str;
84 boolean isNeglect = true;
85 int i = 0;
86 do {
87 str = outReader.readLine();
88 if (str==null) {
89 return null;
91 str = trim(str, " \t");
92 if(str.length()>0) {
93 isNeglect = getFirstCharExist(str, neglectFirstChars);
95 } while (isNeglect);
96 return str;
99 public static String[] getMeaningfulLines(String srcStr, String neglectFirstChars) throws Exception {
100 StringReader outStringReader = new StringReader(srcStr);
101 BufferedReader outReader = new BufferedReader(outStringReader);
102 ArrayList<String> origData = new ArrayList<String>();
103 while(true) {
104 String str = getMeaningfulLine(outReader, neglectFirstChars);
105 if (str==null) {
106 break;
108 origData.add(str);
110 return convert1DVectorToStringArray(origData);
114 * convert vector to 1D String array
116 public static String[] convert1DVectorToStringArray(ArrayList<String> toToConvert) {
117 if (toToConvert==null) return null;
118 String[] objs = new String[toToConvert.size()];
119 for (int i=0; i<toToConvert.size(); i++) {
120 objs[i] = getObjString(toToConvert.get(i));
122 return(objs);
125 public static String getObjString(Object nObj) {
126 if(nObj instanceof String) return (String)nObj;
127 return nObj.toString();
130 static public int toInteger(Object oVal) throws Exception {
131 if(oVal==null) throw new Exception("Null string");
132 return new Integer(oVal.toString()).intValue();
135 static public double toDouble(Object oVal) throws Exception {
136 if(oVal==null) throw new Exception("Null string");
137 return new Double(oVal.toString()).doubleValue();
140 public static Object toObject(String key) throws Exception{
141 Class cls = Class.forName(key);
142 return cls.newInstance();