package com.apag.p2plus.p2core; /** * Package: P2Plus.P2Core * Class: Convert.java * --------------------------------------------------------------- * Copyright AP-AG, 1999-2004 * --------------------------------------------------------------- * State: Design * 30.04.1999 TB generated * 15.06.1999 TB toDouble() erlaubt auch ',' statt '.' * 16.08.1999 TB roundMoney(), roundQuantity() * toDouble irgnoriert führende und folgende SPACEs * 09.11.1999 TB stringToArray() * 18.02.2000 TB round() schafft auch sehr große/kleine (mit E) Zahlen * 06.04.2000 TB round() schafft auch negative Zahlen * 07.04.2000 TB round() Fehler bei riesengroßen Zahlen behoben * 16.11.2000 AV round() neu implementiert * 07.12.2000 TB toFloat() * 13.12.2000 TB toInt() * 18.12.2000 AV toShort(), * toXXX(null) liefert 0 zurück * 08.01.2001 TB roundMoney() kann eine Währung mitgegeben * werden und benutzt die Rundung von Hauswährung, wenn * nichts mitgegeben wird * 23.01.2001 AV stringToArray2(): Berücksichtigt Leerstrings * beim Zerlegen des Strings in Teilstrings * 28.03.2001 AV toLong() * 08.06.2001 TB toBoolean() * 16.08.2001 TB alle toXXX(): "" -> 0 * 17.08.2001 TB Bugfix in stringToArray2() * 29.08.2001 NH charTabToArray Tabs werden durch Leerzeichen ersetzt * 10.10.2001 AV main()-Methode erweitert, Bugs in round() gefixt * 04.07.2002 TB stringToObject() * 31.07.2002 TB round2(), roundSchema() * 29.01.2003 TB FIX: AF360213 stringToHashSet() * 06.02.2003 TB FIX: AF360213 round(): Bei fraction=0 wird der Originalwert * zurückgegeben * 09.04.2003 TB binaryToHexString(), hexStringToBinary(), * stringToObject(): Binary-Unterstützung * 21.03.2005 rkl roundUp() rundet immer auf * 15.02.2007 HSI stringToArray(String, String, int) * 15.01.2008 HSI FEATURE: #176619 arrayToString(), arrayToSQLString() * 12.02.2008 HSI setToString(), setToSQLString() * 26.06.2008 ISE MapToString(), stringToTreeMap() * --------------------------------------------------------------- */ import java.math.*; import java.util.*; // import com.ms.wfc.ui.*; import com.apag.p2plus.p2core.p2exceptions.*; import com.apag.p2plus.p2masterdata.*; import com.apag.p2plus.p2objects.*; /** * Diese Klasse beinhaltet Methoden zum Konvertieren und Manipulieren * verschiedener Datentypen. */ public class Convert { /** main-Methode zu Testzwecken. * @param args Kommandozeilenargumente * @throws P2Exception bei einem Fehler */ public static void main(String[] args) throws P2Exception { double res1; float res2; int res3; short res4; Tool.waitReturn(); res1 = round(12345.6789, 0.001); if(res1 == 12345.679) { System.out.println("12345.6789 0.001 -> 12345.679 OK"); } else { System.out.println("Error: round(12345.6789, 0.001) liefert " + res1 + " anstatt 12345.679."); } res1 = roundUp(12344.351, 10.00); if(res1 == 12350) { System.out.println("12344.351 10.0 ROUND_UP -> 12350.0 OK"); } else { System.out.println("Error: roundUp(12344.351, 10.0) liefert " + res1 + " anstatt 12350.0"); } res1 = round(12345.6789, 0.01); if(res1 == 12345.68) { System.out.println("12345.6789 0.01 -> 12345.68 OK"); } else { System.out.println("Error: round(12345.6789, 0.01) liefert " + res1 + " anstatt 12345.68."); } res1 = round(12345.6789, 0.1); if(res1 == 12345.7) { System.out.println("12345.6789 0.1 -> 12345.7 OK"); } else { System.out.println("Error: round(12345.6789, 0.1) liefert " + res1 + " anstatt 12345.7."); } res1 = round(12345.6789, 1); if(res1 == 12346) { System.out.println("12345.6789 1 -> 12346 OK"); } else { System.out.println("Error: round(12345.6789, 1) liefert " + res1 + " anstatt 12346."); } res1 = round(12345.6789, 10); if(res1 == 12350) { System.out.println("12345.6789 10 -> 12350 OK"); } else { System.out.println("Error: round(12345.6789, 10) liefert " + res1 + " anstatt 12350."); } res1 = round(12345.6789, 100); if(res1 == 12300) { System.out.println("12345.6789 100 -> 12300 OK"); } else { System.out.println("Error: round(12345.6789, 100) liefert " + res1 + " anstatt 12300."); } res1 = round(12345.6789, 1000); if(res1 == 12000) { System.out.println("12345.6789 1000 -> 12000 OK"); } else { System.out.println("Error: round(12345.6789, 1000) liefert " + res1 + " anstatt 12000."); } res1 = round(2157.40, 5.0); if(res1 == 2155) { System.out.println(" 2157.40 5.0 -> 2155 OK"); } else { System.out.println("Error: round(2157.40, 5.0) liefert " + res1 + " anstatt 2155."); } res1 = round(2158.40, 3.0); if(res1 == 2157) { System.out.println(" 2158.40 3.0 -> 2157 OK"); } else { System.out.println("Error: round(2158.40, 3.0) liefert " + res1 + " anstatt 2157."); } res1 = round(2158.80, 3.0); if(res1 == 2160) { System.out.println(" 2158.80 3.0 -> 2160 OK"); } else { System.out.println("Error: round(2158.80, 3.0) liefert " + res1 + " anstatt 2160."); } res1 = round(970d * 1d / 100d, 0.01); if(res1 == 9.7) { System.out.println("970 * 1 / 100 0.01 -> 9.7 OK"); } else { System.out.println("Error: round(970d * 1d / 100d, 0.01) liefert " + res1 + " anstatt 9.7."); } res1 = round(30d, 0.01); if(res1 == 30) { System.out.println("30 0.01 -> 30 OK"); } else { System.out.println("Error: round(30d, 0.01) liefert " + res1 + " anstatt 30."); } res1 = round(1.905, 0.01); if(res1 == 1.91) { System.out.println("1.905 0.01 -> 1.91 OK"); } else { System.out.println("Error: round(1.905, 0.01) liefert " + res1 + " anstatt 1.91."); } res1 = round(10.795, 0.01); if(res1 == 10.8) { System.out.println("10.795 0.01 -> 10.8 OK"); } else { System.out.println("Error: round(10.795, 0.01) liefert " + res1 + " anstatt 10.8."); } res1 = round(86.474970534888, 0.01); if(res1 == 86.47) { System.out.println("86.474970534888 0.01 -> 86.47 OK"); } else { System.out.println("Error: round(86.474970534888, 0.01) liefert " + res1 + " anstatt 86.47."); } res1 = round(86.474999999999, 0.01); if(res1 == 86.47) { System.out.println("86.474999999999 0.01 -> 86.47 OK"); } else { System.out.println("Error: round(86.474999999999, 0.01) liefert " + res1 + " anstatt 86.47."); } res1 = round(86.4755, 0.001); if(res1 == 86.476) { System.out.println("86.4755 0.001 -> 86.476 OK"); } else { System.out.println("Error: round(86.4755, 0.001) liefert " + res1 + " anstatt 86.476."); } res1 = round(0.000123456789, 0.000001); if(res1 == 0.000123) { System.out.println("0.000123456789 0.000001 -> 0.000123 OK"); } else { System.out.println("Error: round(0.000123456789, 0.000001) liefert " + res1 + " anstatt 0.000123."); } res1 = round(0.12, 0.05); if(res1 == 0.10) { System.out.println("0.12 0.05 -> 0.10 OK"); } else { System.out.println("Error: round(0.12, 0.05) liefert " + res1 + " anstatt 0.10."); } res1 = round(0.56, 0.05); if(res1 == 0.55) { System.out.println("0.56 0.05 -> 0.55 OK"); } else { System.out.println("Error: round(0.56, 0.05) liefert " + res1 + " anstatt 0.55."); } res1 = round(123, 50); if(res1 == 100) { System.out.println("123 50 -> 100 OK"); } else { System.out.println("Error: round(123, 50) liefert " + res1 + " anstatt 100."); } res1 = round(123, 20); if(res1 == 120) { System.out.println("123 20 -> 120 OK"); } else { System.out.println("Error: round(123, 20) liefert " + res1 + " anstatt 120."); } System.out.println("round2():"); res1 = round2(123.456, 2); if(res1 == 123.46) { System.out.println("123.456 2 -> 123.46 OK"); } else { System.out.println("Error: round2(123.456, 2) liefert " + res1 + " anstatt 123.46"); } System.out.println("roundSchema():"); res1 = roundSchema("artikel", "netto", 123.456); if(res1 == 123.46) { System.out.println("123.456 2 -> 123.46 OK"); } else { System.out.println("Error: roundSchema(\"artikel\", \"netto\", 123.456) liefert " + res1 + " anstatt 123.46"); } System.out.println("toBoolean():"); if(Convert.toBoolean("true")) { System.out.println("Convert.toBoolean(\"true\") -> true OK"); } else { System.out.println("Error: Convert.toBoolean(\"true\") liefert false anstatt true."); } if(!Convert.toBoolean("false")) { System.out.println("Convert.toBoolean(\"false\") -> false OK"); } else { System.out.println("Error: Convert.toBoolean(\"false\") liefert true anstatt false."); } /* String s = "abc\nxyz"; System.out.println(s.replace('\n', ',')); res1 = Convert.toDouble(null); res2 = Convert.toFloat(null); res3 = Convert.toInt(null); res4 = Convert.toShort(null); */ /* System.out.println("\nroundMoney(12345678.123456789, null) -> " + roundMoney(12345678.123456789, null)); System.out.println("roundMoney(12345678.0, null) -> " + roundMoney(12345678.0, null)); System.out.println("roundMoney(1450.0 * 10000.0, null) -> " + roundMoney(1450.0 * 10000.0, null)); */ System.exit(0); } // main /** * Werte runden.
* Beispiele: *
   * 12345.6789,  0.01  -> 12345.68
   * 12345.6789, 10.0   -> 12350.0
   *  2157.40     5.0   ->  2155.0
   * 
* * @param value zu rundender Wert * @param fraction Rundungsziel * @return gerundeter Wert */ public static double round(double value, double fraction) { int scale; BigDecimal bigFraction, bigValue; String help; int index, exponent = 0; String exp; if(fraction == 0) { return(value); } // // Workaround für JDK 1.1 // help = Double.toString(value); index = help.indexOf('E'); if(index != -1) { exp = help.substring(index + 1); help = help.substring(0, index); try { exponent = toInt(exp); } catch(P2FormatException fe) { } bigValue = new BigDecimal(help); bigValue = bigValue.movePointRight(exponent); } else { bigValue = new BigDecimal(Double.toString(value)); } help = Double.toString(fraction); index = help.indexOf('E'); if(index != -1) { exp = help.substring(index + 1); help = help.substring(0, index); try { exponent = toInt(exp); } catch(P2FormatException fe) { } bigFraction = new BigDecimal(help); bigFraction = bigFraction.movePointRight(exponent); } else { bigFraction = new BigDecimal(Double.toString(fraction)); } // // Ende Workaround für JDK 1.1 // if(fraction < 1) { // Anzahl der Nachkommastellen ermitteln scale = (int) Math.ceil(Math.log(1 / fraction) / Math.log(10)); } else { scale = 0; } bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.setScale(0, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); return(bigValue.doubleValue()); } // round /** * Werte runden.
* Beispiele: *
   * 12345.6789,  0.01  -> 12345.68
   * 12345.6789, 10.0   -> 12350.0
   *  2157.40     5.0   ->  2155.0
   * 
* * @param value zu rundender Wert * @param scale Anzahl Nachkommastellen * @return gerundeter Wert */ public static double round2(double value, int scale) { BigDecimal bigFraction, bigValue; String help; int index, exponent = 0; String exp; // // Workaround für JDK 1.1 // help = Double.toString(value); index = help.indexOf('E'); if(index != -1) { exp = help.substring(index + 1); help = help.substring(0, index); try { exponent = toInt(exp); } catch(P2FormatException fe) { } bigValue = new BigDecimal(help); bigValue = bigValue.movePointRight(exponent); } else { bigValue = new BigDecimal(Double.toString(value)); } // // Ende Workaround für JDK 1.1 // bigFraction = new BigDecimal("1"); bigFraction = bigFraction.setScale(scale, BigDecimal.ROUND_HALF_UP); bigFraction = bigFraction.divide(new BigDecimal(Math.pow(10, scale)), BigDecimal.ROUND_HALF_UP); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.setScale(0, BigDecimal.ROUND_HALF_UP); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_HALF_UP); return(bigValue.doubleValue()); } // round2 /** Werte runden.
* Beispiele: *
   * 12345.6789,  0.01  -> 12345.68
   * 12345.6789, 10.0   -> 12350.0
   *  2157.40     5.0   ->  2155.0
   * 
* @return gerundeter Wert * @param tableName Name der Tabelle * @param fieldName Name des Feldes * @param value zu rundender Wert * @throws P2DBException bei einem DB-Fehler */ public static double roundSchema(String tableName, String fieldName, double value) throws P2DBException { return(round2(value, ((SchemaField)SchemaCache.getTable(tableName).getFields().get(fieldName.toUpperCase())).getScale())); } // roundSchema /** Rundet entsprechend der Währung. * @return gerundeter Wert * @param value zu rundender Wert * @param waehrung Währung oder NULL (dann wird die Rundung aus der * Hauswährung verwendet) * @throws P2Exception bei einem Fehler */ public static double roundMoney(double value, String waehrung) throws P2Exception { ObjectManager manager; WaehrungObject waehrungObj; double ret; manager = new ObjectManager(); try { if(waehrung != null) { waehrungObj = WaehrungFactory.search(manager, waehrung, ObjectConstants.READ_LOCK, true); } else { waehrungObj = WaehrungUtil.getBaseCurrency(manager, ObjectConstants.READ_LOCK); } ret = round(value, waehrungObj.getRound()); } finally { manager.release(); } return(ret); } // roundMoney /** * Rundet zur Zeit auf 2 Nachkommastellen. * * @param value zu rundender Wert * @param type Typ des Werts (reserviert) * @return gerundeter Wert */ public static double roundQuantity(double value, String type) { return(round(value, 0.01)); } // roundQuantity /** * Wandelt einen String in ein double. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. Als Dezimaltrenner darf "," oder "." * verwendet werden. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static double toDouble(String text) throws P2FormatException { try { if(text != null && text.length()!=0) { // ',' -> '.' text = text.trim().replace(',', '.'); return(Double.valueOf(text).doubleValue()); } else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTODOUBLE|" + text); } } // toDouble /** * Wandelt einen String in ein float. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. Als Dezimaltrenner darf "," oder "." * verwendet werden. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static float toFloat(String text) throws P2FormatException { try { if(text != null && text.length()!=0) { // ',' -> '.' text = text.trim().replace(',', '.'); return(Float.valueOf(text).floatValue()); } else return(0f); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOFLOAT|" + text); } } // toFloat /** * Wandelt einen String in ein int. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static int toInt(String text) throws P2FormatException { try { if(text != null && text.length()!=0) return(Integer.parseInt(text.trim())); else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOINT|" + text); } } // toInt /** * Wandelt einen String in ein short. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static short toShort(String text) throws P2FormatException { try { if(text != null && text.length()!=0) return(Short.parseShort(text.trim())); else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOINT|" + text); } } // toShort /** * Wandelt einen String in ein long. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return Zahl * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static long toLong(String text) throws P2FormatException { try { if(text != null && text.length()!=0) return(Long.parseLong(text.trim())); else return(0); } catch(NumberFormatException nfe) { throw new P2FormatException("C_CONVERTSTRINGTOLONG|" + text); } } // toLong /** * Wandelt einen String in ein boolean. Voraussetzung ist, * daß der String richtig formatiert ist und keine SPACEs * oder Buchstaben enthält. Führende und folgende SPACEs * werden ignoriert. * * @param text zu konvertierender String * @return true oder false * @exception P2FormatException wenn die Konvertierung fehlschlägt */ public static boolean toBoolean(String text) throws P2FormatException { if(text != null && text.length()!=0) return(Boolean.valueOf(text.trim()).booleanValue()); else return(false); } // toBoolean /** * Wandelt eine mit SPACEs, TABs und CRLF in einem String * aufgebaute Tabelle um in ein 2-dimensionales String-Array. *
* Beispiel:
* "1 100\r\n10 95\r\n"
* [0][0] = "1"
* [0][1] = "100"
* [1][0] = "10"
* [1][1] = "95"

* * Die Anzahl der Spalten wird benötigt, damit mit SPACEs * getrennte Wörter in der letzten Spalte möglich sind und * leere Stellen mit "" belegt werden können. * * @param field String mit der zu konvertierenden "Tabelle" * @param maxCols Anzahl der Spalten in der Tabelle * @return 2-dimensionales String-Array */ public static String[][] charTabToArray(String field, int maxCols) { StringTokenizer stRow, stCol; String row, ret[][]; int maxRows, currentRow, currentCol, i; maxRows = 0; for(stRow = new StringTokenizer(field, "\n\r"); stRow.hasMoreTokens(); stRow.nextToken()) maxRows++; ret = new String[maxRows][maxCols]; currentRow = 0; for(stRow = new StringTokenizer(field, "\n\r"); stRow.hasMoreTokens(); ) { row = stRow.nextToken(); row = Text.replaceTabs(row); currentCol = 0; for(stCol = new StringTokenizer(row, " "); stCol.hasMoreTokens(); ) { if(currentCol < maxCols) ret[currentRow][currentCol] = stCol.nextToken(); else ret[currentRow][maxCols - 1] += " " + stCol.nextToken(); currentCol++; } for(i = currentCol; i < maxCols; i++) ret[currentRow][i] = ""; currentRow++; } return(ret); } // charTabToArray /** * Zerlegt einen String in Teilstrings anhand eines Separators. * Leerstring werden dabei überlesen. Die Teilstrings werden * in einem Array abgelegt. * * @param s String * @param separator Separator * @return Array */ public static String[] stringToArray(String s, String separator) { String[] array; StringTokenizer st; int col = 0; for(st = new StringTokenizer(s, separator); st.hasMoreTokens(); ) { st.nextToken(); col++; } array = new String[col]; col = 0; for(st = new StringTokenizer(s, separator); st.hasMoreTokens(); ) { array[col] = st.nextToken(); col++; } return(array); } // stringToArray /** * Zerlegt einen String in Teilstrings anhand eines Separators. * Leerstrings werden dabei berücksichtigt. Die Teilstrings * werden in einem Array abgelegt. * * @param s String * @param separator Separator * @return Array */ public static String[] stringToArray2(String s, String separator) { String data, lastData; String[] array; StringTokenizer st; int col = 0; if(s == null || s.length() == 0) return(new String[1]); lastData = null; data = null; for(st = new StringTokenizer(s, separator, true); st.hasMoreTokens(); ) { data = st.nextToken(); if(data.equals(separator) && (lastData == null || lastData.equals(separator))) { // null-Wert data = null; } else { lastData = data; } if((data == null) || (!data.equals(separator))) { col++; } } if(data == null || data.equals(separator)) col++; array = new String[col]; col = 0; lastData = null; data = null; for(st = new StringTokenizer(s, separator, true); st.hasMoreTokens(); ) { data = st.nextToken(); if(data.equals(separator) && (lastData == null || lastData.equals(separator))) { // null-Wert data = null; } else { lastData = data; } if((data == null) || (!data.equals(separator))) { array[col] = data; col++; } } return(array); } // stringToArray2 /** * Konvertiert einen String in ein Objekt eines bestimmten Typs. * * @param value String-Wert * @param type Typ * @return verlangtes Objekt oder null * @throws P2IllegalException bei wenn die Konvertierung nicht unterstützt wird * @throws P2FormatException bei einem Konvertierungsfehler */ public static Object stringToObject(String value, Class type) throws P2IllegalException, P2FormatException { if(value == null) { return(null); } else if(type == String.class) { return(value); } else if(type == Integer.class || type == int.class) { return(new Integer(Convert.toInt(value))); } else if(type == Short.class || type == short.class) { return(new Short(Convert.toShort(value))); } else if(type == Double.class || type == double.class) { return(new Double(Convert.toDouble(value))); } else if(type == Float.class || type == float.class) { return(new Float(Convert.toFloat(value))); } else if(type == Boolean.class || type == boolean.class) { return(Boolean.valueOf(Convert.toBoolean(value))); } else if(type == P2Time.class) { return(new P2Time(value)); } else if(type == byte[].class) { return(Convert.hexStringToBinary(value)); } else { throw new P2IllegalException("C_NOTTREATEDJAVACLASS|" + type); } } // stringToObject /** * Wandelt eine Aufzählung in einem String um in ein HashSet. * * @param s String * @param regEx Elementtrenner * @return HashSet */ public static HashSet stringToHashSet(String s, String regEx) { return(new HashSet(Arrays.asList(s.split(regEx, -1)))); } // stringToHashSet /** * Wandelt einen binären String in einen hexadezimalen String um. * * @param binary Binärstring * @return hexadezimaler String */ public static String binaryToHexString(byte[] binary) { StringBuffer ret; String tempString; int i; if(binary == null) { return(null); } ret = new StringBuffer(); for(i = 0; i < binary.length; i++) { tempString = Integer.toHexString(binary[i] & 0xff); if(tempString.length() == 1) ret.append('0'); ret.append(tempString); } return(ret.toString()); } // binaryToHexString /** * Wandelt einen hexadezimalen String in einen binären String um. * * @param text hexadezimaler String * @return Binärstring */ public static byte[] hexStringToBinary(String text) { byte[] ret; int i; if(text == null) { return(null); } ret = new byte[text.length() / 2]; for(i = 0; i < text.length(); i += 2) { ret[i / 2] = (byte)Integer.parseInt(text.substring(i, i + 2), 16); } return(ret); } // hexStringToBinary /** * Werte aufrunden.
* Beispiele: *
   * 12344.350, 0.01  -> 12344.35
   * 12344.351, 0.01  -> 12344.36
   * 12344.351, 0.10  -> 12344.40
   * 12344.351, 1.00  -> 12344.40
   * 12344.351, 1.00  -> 12345.00
   * 12344.351, 10.00  -> 12350.00 
   * 
* @param value zu rundender Wert * @param fraction Rundungsziel * @return gerundeter Wert */ public static double roundUp(double value, double fraction) { int scale; BigDecimal bigFraction, bigValue; if(fraction == 0) { return(value); } bigValue = new BigDecimal(Double.toString(value)); bigFraction = new BigDecimal(Double.toString(fraction)); if(fraction < 1) { // Anzahl der Nachkommastellen ermitteln scale = (int) Math.ceil(Math.log(1 / fraction) / Math.log(10)); } else { scale = 0; } bigValue = bigValue.setScale(scale,BigDecimal.ROUND_UP); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_UP); bigValue = bigValue.setScale(0, BigDecimal.ROUND_UP); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_UP); return(bigValue.doubleValue()); } // roundUp /** * FIX: F420102 #171162 * Werte abrunden.
* Beispiele: *
   * 12344.350, 0.01  -> 12344.35
   * 12344.359, 0.01  -> 12344.35
   * 12344.351, 0.10  -> 12344.30
   * 12344.351, 10.00  -> 12340.00 
   * 
* @param value zu rundender Wert * @param fraction Rundungsziel * @return gerundeter Wert */ public static double roundDown(double value, double fraction) { int scale; BigDecimal bigFraction, bigValue; if(fraction == 0) { return(value); } bigValue = new BigDecimal(Double.toString(value)); bigFraction = new BigDecimal(Double.toString(fraction)); if(fraction < 1) { // Anzahl der Nachkommastellen ermitteln scale = (int) Math.ceil(Math.log(1 / fraction) / Math.log(10)); } else { scale = 0; } bigValue = bigValue.setScale(scale,BigDecimal.ROUND_DOWN); bigValue = bigValue.divide(bigFraction, BigDecimal.ROUND_DOWN); bigValue = bigValue.setScale(0, BigDecimal.ROUND_DOWN); bigValue = bigValue.multiply(bigFraction); bigValue = bigValue.setScale(scale, BigDecimal.ROUND_DOWN); return(bigValue.doubleValue()); } // roundDown /** * Zerlegt einen String in Teilstrings anhand eines Separators. * Leerstring werden dabei überlesen. Die Teilstrings werden in einem Array * abgelegt. *
Jeder Teilstring enthält maximal die Anzahl an Tokens, die in * maxTokens angegeben ist. *

Bsp.: * * * * * * * * * * * * * * * * * * * * * *
        s:    "1,2,3,4,5"
        separator:    ","
        maxTokens:    2
        Ergebnis:    { "1,2", "3,4", "5" }
* * @param s String * @param separator Separator * @param maxTokens maximale Anzahl an Tokens pro Teilstring * @return Array * @throws P2ParameterException falls maxTokens < 1 */ public static String[] stringToArray(String s, String separator, int maxTokens) throws P2ParameterException { String[] array; StringTokenizer stTok; StringBuffer sb; int count, loop, i, j; if(maxTokens < 1) { throw new P2ParameterException("A_PARAMERR|maxTokens|stringToArray()|" + maxTokens); } stTok = new StringTokenizer(s, separator); count = stTok.countTokens(); loop = count / maxTokens; if(count % maxTokens != 0) { loop++; } array = new String[loop]; for(i = 0; i < loop; i++) { sb = new StringBuffer(); for (j = 1; j <= maxTokens && stTok.hasMoreTokens(); j++) { sb.append(stTok.nextToken()).append(separator); } // letzten Separator entfernen sb.deleteCharAt(sb.length() - 1); array[i] = sb.toString(); } return array; } // stringToArray /** * Konvertiert ein Array in einen String anhand eines Separators und einer * optionalen Zeichenkette. * * @param array Array * @param separator Separator * @param wrapper optionale Zeichenkette, die um jedes einzelne Element des * Arrays gesetzt wird * @return String *

Bsp.: * * * * * * * * * * * * * * * * * * * * * *
        array:    { "A", "ABC", "XY" }
        separator:    ","
        wrapper:    "'"
        Ergebnis:    'A','ABC','XY'
*/ public static String arrayToString(String[] array, String separator, String wrapper) { StringBuffer stB; int i; stB = new StringBuffer(); for(i = 0; i < array.length; i++) { if(wrapper != null) { stB.append(wrapper) ; } stB.append(array[i]); if(wrapper != null) { stB.append(wrapper) ; } if(i < array.length - 1) { stB.append(separator); } } return(stB.toString()); } // arrayToString /** * Konvertiert ein Array in einen String. Als Separator wird Constants.COMMA * verwendet und als Wrapper Constants.QUOTE. * * @param array Array * @return String, der als Bedingung in einer SQL-Anweisung verwendet werden * kann *

Bsp.: * * * * * * * * * * * *
        array:    { "A", "ABC", "XY" }
        Ergebnis:    'A','ABC','XY'
*/ public static String arrayToSQLString(String[] array) { return(arrayToString(array, Constants.COMMA, Constants.QUOTE)); } // arrayToSQLString /** * Konvertiert ein Set in einen String anhand eines Separators und einer * optionalen Zeichenkette. Die Reihenfolge ist zufällig. * * @param set Set * @param separator Separator * @param wrapper optionale Zeichenkette, die um jedes einzelne Element des * Sets gesetzt wird * @return String */ public static String setToString(Set set, String separator, String wrapper) { String[] array; array = new String[set.size()]; set.toArray(array); return arrayToString(array, separator, wrapper); } // setToString /** * Konvertiert ein Set in einen String. Als Separator wird Constants.COMMA * verwendet und als Wrapper Constants.QUOTE. Die Reihenfolge ist zufällig. * * @param set Set * @return String im Format 'A','ABC','XY' */ public static String setToSQLString(Set set) { return(setToString(set, Constants.COMMA, Constants.QUOTE)); } // setToSQLString /** * Konvertiert ein Map in einen String anhand * eines Separators und eines Zwischenzeichens. * * @param map Map * @param separator Separator (trennt Key und Value) * @param wrapper Zeichen (wird nach jedem Paar Key-Value gesetzt) * @return String */ public static String mapToString(Map map, String separator, String wrapper, boolean num) { StringBuffer stB; P2Time key; stB = new StringBuffer(); for(Map.Entry entry : map.entrySet()) { key = (P2Time)entry.getKey(); if (!num) { stB.append(key.toSQLDate()+separator+(Double)entry.getValue()+wrapper); } else { stB.append(key.toSQLDate()+separator+((Double)entry.getValue()).intValue()+wrapper); } } return(stB.toString()); } // mapToString /** * Konvertiert ein String in ein TreeMap * anhand eines Separators und eines Zwischenzeichens. * * @param map TreeMap * @param separator Separator (trennt Key und Value) * @param wrapper Zeichen (trennt Key-Value Paare) * @return String */ public static TreeMap stringToTreeMap(String str, String separator, String wrapper) throws P2Exception { TreeMap map; String[] strArray, strArray1; int i; map = new TreeMap(); if (str != null && str.length() > 0) { strArray = str.split(wrapper); for (i = 0 ;i < strArray.length; i ++){ strArray1 = strArray[i].split(separator); if (strArray1.length > 1) { if (strArray1[1] != null && (strArray1[1].trim()).length() > 0) { if (strArray1[0] != null && (strArray1[0].trim()).length() > 0) { map.put(new P2Time(strArray1[0]), new Double(Double.parseDouble(strArray1[1]))); } } } } } return(map); } // stringToTreeMap } // Convert ----- Classpath: --------------------------------------------- bootPath: D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\resources.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\rt.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\sunrsasign.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\jsse.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\jce.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\charsets.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\classes;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\ext\dnsns.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\ext\localedata.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\ext\sunjce_provider.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\ext\sunmscapi.jar;D:\Program Files (x86)\Java\jdk1.6.0_14\jre\lib\ext\sunpkcs11.jar classPath: D:\P2plus\Ortems\AppServer\lib\activation.jar;D:\P2plus\Ortems\AppServer\lib\apcommon.jar;D:\P2plus\Ortems\AppServer\lib\axis.jar;D:\P2plus\Ortems\AppServer\lib\commons-discovery.jar;D:\P2plus\Ortems\AppServer\lib\commons-httpclient.jar;D:\P2plus\Ortems\AppServer\lib\commons-io-1.3.2.jar;D:\P2plus\Ortems\AppServer\lib\commons-logging.jar;D:\P2plus\Ortems\AppServer\lib\comwrapper.jar;D:\P2plus\Ortems\AppServer\lib\css.jar;D:\P2plus\Ortems\AppServer\lib\icemh.jar;D:\P2plus\Ortems\AppServer\lib\izmcomjni.jar;D:\P2plus\Ortems\AppServer\lib\jaxrpc.jar;D:\P2plus\Ortems\AppServer\lib\jdom.jar;D:\P2plus\Ortems\AppServer\lib\js.jar;D:\P2plus\Ortems\AppServer\lib\jtds.jar;D:\P2plus\Ortems\AppServer\lib\junit.jar;D:\P2plus\Ortems\AppServer\lib\log4j.jar;D:\P2plus\Ortems\AppServer\lib\mail.jar;D:\P2plus\Ortems\AppServer\lib\mysql-connector-java-5.0.7-bin.jar;D:\P2plus\Ortems\AppServer\lib\rome-0.9.jar;D:\P2plus\Ortems\AppServer\lib\saaj.jar;D:\P2plus\Ortems\AppServer\lib\simultan70.jar;D:\P2plus\Ortems\AppServer\lib\simultan80.jar;D:\P2plus\Ortems\AppServer\lib\svnjavahl.jar;D:\P2plus\Ortems\AppServer\lib\swing-layout.jar;D:\P2plus\Ortems\AppServer\lib\tools.jar;D:\P2plus\Ortems\AppServer\lib\Una2000.jar;D:\P2plus\Ortems\AppServer\lib\varial270.jar;D:\P2plus\Ortems\AppServer\lib\webdavlib.jar;D:\P2plus\Ortems\AppServer\lib\wsdl4j.jar;D:\P2plus\Ortems\AppServer\lib\xalan.jar;D:\P2plus\Ortems\AppServer\lib\xercesImpl.jar;D:\P2plus\Ortems\AppServer\lib\xmlParserAPIs.jar;D:\P2plus\Ortems\AppServer\lib\serializer.jar;D:\P2plus\Ortems\AppServer\lib\p2plusprivate.jar sourcePath: D:\P2plus\Ortems\AppServer\P2Java ----- Original exception --------------------------------------------- java.lang.OutOfMemoryError: Java heap space at com.sun.tools.javac.util.Position$LineMapImpl.build(Position.java:153) at com.sun.tools.javac.util.Position.makeLineMap(Position.java:77) at com.sun.tools.javac.parser.DocCommentScanner.getLineMap(DocCommentScanner.java:451) at com.sun.tools.javac.parser.JavacParser.parseCompilationUnit(JavacParser.java:2286) at com.sun.tools.javac.parser.EndPosParser.parseCompilationUnit(EndPosParser.java:85) at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:581) at com.sun.tools.javac.main.JavaCompiler.complete(JavaCompiler.java:718) at com.sun.tools.javac.jvm.ClassReader.fillIn(ClassReader.java:1943) at com.sun.tools.javac.jvm.ClassReader.complete(ClassReader.java:1842) at com.sun.tools.javac.code.Symbol.complete(Symbol.java:405) at com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:802) at com.sun.tools.javac.jvm.ClassReader.loadClass(ClassReader.java:2026) at com.sun.tools.javac.comp.Resolve.loadClass(Resolve.java:872) at com.sun.tools.javac.comp.Resolve.findIdentInPackage(Resolve.java:1042) at com.sun.tools.javac.comp.Attr.selectSym(Attr.java:2012) at com.sun.tools.javac.comp.Attr.visitSelect(Attr.java:1924) at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1655) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:381) at com.sun.tools.javac.comp.Attr.attribType(Attr.java:411) at com.sun.tools.javac.comp.MemberEnter.attribImportType(MemberEnter.java:826) at com.sun.tools.javac.comp.MemberEnter.visitImport(MemberEnter.java:597) at com.sun.tools.javac.tree.JCTree$JCImport.accept(JCTree.java:503) at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:427) at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:439) at com.sun.tools.javac.comp.MemberEnter.visitTopLevel(MemberEnter.java:560) at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:450) at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:427) at com.sun.tools.javac.comp.MemberEnter.complete(MemberEnter.java:974) at com.sun.tools.javac.code.Symbol.complete(Symbol.java:405) at com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:802) at com.sun.tools.javac.comp.Enter.complete(Enter.java:646) at com.sun.tools.javac.main.JavaCompiler.complete(JavaCompiler.java:732)