📄 textutils.java
字号:
char c = Character.toLowerCase(pp.charAt(i)); if (c == 'g' || c == 'k' || c == 'm' || c == 'u' || c == 'n' || c == 'p' || c == 'f') { return true; } } else if (pp.substring(i).toLowerCase().equals("meg")) return true; return false; } /** * Method to describe a time value as a String. * @param milliseconds the time span in milli-seconds. * @return a String describing the time span with the * format: days : hours : minutes : seconds */ public static String getElapsedTime(long milliseconds) { if (milliseconds < 60000) { // less than a minute: show fractions of a second return (milliseconds / 1000.0) + " secs"; } StringBuffer buf = new StringBuffer(); int seconds = (int)milliseconds/1000; if (seconds < 0) seconds = 0; int days = seconds/86400; if (days > 0) buf.append(days + " days, "); seconds = seconds - (days*86400); int hours = seconds/3600; if (hours > 0) buf.append(hours + " hrs, "); seconds = seconds - (hours*3600); int minutes = seconds/60; if (minutes > 0) buf.append(minutes + " mins, "); seconds = seconds - (minutes*60); buf.append(seconds + " secs"); return buf.toString(); } /** * Method to find a string inside of another string. * @param string the main string being searched. * @param search the string being located in the main string. * @param startingPos the starting position in the main string to look (0 to search the whole string). * @param caseSensitive true to do a case-sensitive search. * @param reverse true to search from the back of the string. * @return the position of the search string. Returns negative if the string is not found. */ public static int findStringInString(String string, String search, int startingPos, boolean caseSensitive, boolean reverse) { if (caseSensitive) { // case-sensitive search int i = 0; if (reverse) i = string.lastIndexOf(search, startingPos); else i = string.indexOf(search, startingPos); return i; } // case-insensitive search if (startingPos > 0) string = string.substring(startingPos); String stringLC = canonicString(string); String searchLC = canonicString(search); int i = 0; if (reverse) i = stringLC.lastIndexOf(searchLC); else i = stringLC.indexOf(searchLC); if (i >= 0) i += startingPos; return i; } /** * Method to break a line into keywords, separated by white space or comma * @param line the string to tokenize. * @param delim the delimiters. * @return an array of Strings for each keyword on the line. */ public static String [] parseString(String line, String delim) { StringTokenizer st = new StringTokenizer(line, delim); int total = st.countTokens(); String [] strings = new String[total]; for(int i=0; i<total; i++) strings[i] = st.nextToken().trim(); return strings; } /** * Unit is a typesafe enum class that describes a unit scale (metric factors of 10). */ public static class UnitScale { private final String name;// private final String description; private final int index; private final String postFix; private final Number multiplier; private UnitScale(String name, String description, int index, String postFix, Number multiplier) { this.name = name;// this.description = description; this.index = index; this.postFix = postFix; this.multiplier = multiplier; } /** * Method to return the name of this UnitScale. * The name can be prepended to a type, for example the name "Milli" can be put in front of "Meter". * @return the name of this UnitScale. */ public String getName() { return name; } /** * Method to convert this UnitScale to an integer. * Used when storing these as preferences. * @return the index of this UnitScale. */ public int getIndex() { return index; } /** * Get the string representing the postfix associated with this unit scale * @return the post fix string */ public String getPostFix() { return postFix; } /** * Get the multiplier value associated with this unit scale. * @return the multiplier. May be an Integer (values >= 1) or a Double (values <= 1) */ public Number getMultiplier() { return multiplier; } /** * Method to convert the index value to a UnitScale. * Used when storing these as preferences. * @param index the index of the UnitScale. * @return the indexed UnitScale. */ public static UnitScale findFromIndex(int index) { return allUnits[index - UNIT_BASE]; } /** * Method to return a list of all scales. * @return an array of all scales. */ public static UnitScale [] getUnitScales() { return allUnits; } /** * Returns a printable version of this Unit. * @return a printable version of this Unit. */ public String toString() { return name; } /** The largest unit value. */ private static final int UNIT_BASE = -3; /** The smallest unit value. */ private static final int UNIT_END = 5; /** Describes giga scale (1 billion). */ public static final UnitScale GIGA = new UnitScale("Giga", "giga: x 1000000000", -3, "G", new Integer(1000000000)); /** Describes mega scale (1 million). */ public static final UnitScale MEGA = new UnitScale("Mega", "mega: x 1000000", -2, "meg", new Integer(1000000)); /** Describes kilo scale (1 thousand). */ public static final UnitScale KILO = new UnitScale("Kilo", "kilo: x 1000", -1, "k", new Integer(1000)); /** Describes unit scale (1). */ public static final UnitScale NONE = new UnitScale("", "-: x 1", 0, "", new Integer(1)); /** Describes milli scale (1 thousandth). */ public static final UnitScale MILLI = new UnitScale("Milli", "milli: x 10 ^ -3", 1, "m", new Double(0.001)); /** Describes micro scale (1 millionth). */ public static final UnitScale MICRO = new UnitScale("Micro", "micro: x 10 ^ -6", 2, "u", new Double(0.000001)); /** Describes nano scale (1 billionth). */ public static final UnitScale NANO = new UnitScale("Nano", "nano: x 10 ^ -9", 3, "n", new Double(0.000000001)); /** Describes pico scale (10 to the -12th). */ public static final UnitScale PICO = new UnitScale("Pico", "pico: x 10 ^ -12", 4, "p", new Double(0.000000000001)); /** Describes femto scale (10 to the -15th). */ public static final UnitScale FEMTO = new UnitScale("Femto", "femto: x 10 ^ -15", 5, "f", new Double(0.000000000000001)); /** Describes atto scale (10 to the -18th). */ public static final UnitScale ATTO = new UnitScale("Atto", "atto: x 10 ^ -18", 6, "a", new Double(0.000000000000000001)); /** Describes zepto scale (10 to the -21st). */ public static final UnitScale ZEPTO = new UnitScale("Zepto", "zepto: x 10 ^ -21", 7, "z", new Double(0.000000000000000000001)); /** Describes yocto scale (10 to the -24th). */ public static final UnitScale YOCTO = new UnitScale("Yocto", "yocto: x 10 ^ -24", 8, "y", new Double(0.000000000000000000000001)); private final static UnitScale [] allUnits = { GIGA, MEGA, KILO, NONE, MILLI, MICRO, NANO, PICO, FEMTO, ATTO, ZEPTO, YOCTO }; } /** * Method to convert a database coordinate into real spacing. * @param value the database coordinate to convert. * @param tech the technology to use for conversion (provides a real scaling). * @param unitScale the type of unit desired. * @return the database coordinate in the desired units. * For example, if the given technology has a scale of 200 nanometers per unit, * and the value 7 is given, then that is 1.4 microns (1400 nanometers). * If the desired units are UnitScale.MICRO, then the returned value will be 1.4. */ public static double convertDistance(double value, Technology tech, UnitScale unitScale) { double scale = tech.getScale(); double distanceScale = 0.000000001 / unitScale.getMultiplier().doubleValue() * scale; return value * distanceScale; } /** * Method to convert real spacing into a database coordinate. * @param value the real distance to convert. * @param tech the technology to use for conversion (provides a real scaling). * @param unitScale the type of unit desired. * @return the real spacing in the database units. * For example, if the given technology has a scale of 200 nanometers per unit, * and the value 1.6 is given with the scale UnitScale.MICRO, then that is 1.6 microns (1600 nanometers). * Since the technology has 200 nanometers per unit, this converts to 8 units. */ public static double convertFromDistance(double value, Technology tech, UnitScale unitScale) { double scale = tech.getScale(); double distanceScale = 0.000000001 / unitScale.getMultiplier().doubleValue() * scale; return value / distanceScale; } /** * Method to express "value" as a string in "unittype" electrical units. * The scale of the units is in "unitscale". */// public static String displayedUnits(double value, TextDescriptor.Unit unitType, UnitScale unitScale)// {// String postFix = "";// if (unitScale == UnitScale.GIGA)// {// value /= 1000000000.0f;// postFix = "g";// } else if (unitScale == UnitScale.MEGA)// {// value /= 1000000.0f;// postFix = "meg"; // SPICE wants "x"// } else if (unitScale == UnitScale.KILO)// {// value /= 1000.0f;// postFix = "k";// } else if (unitScale == UnitScale.MILLI)// {// value *= 1000.0f;// postFix = "m";// } else if (unitScale == UnitScale.MICRO)// {// value *= 1000000.0f;// postFix = "u";// } else if (unitScale == UnitScale.NANO)// {// value *= 1000000000.0f;// postFix = "n";// } else if (unitScale == UnitScale.PICO)// {// value *= 1000000000000.0f;// postFix = "p";// } else if (unitScale == UnitScale.FEMTO)// {// value *= 1000000000000000.0f;// postFix = "f";// }// return value + postFix;//// return formatDoublePostFix(value);// } /** * Method to convert a floating point value to a string, given that it is a particular type of unit. * Each unit has a default scale. For example, if Capacitance value 0.0000012 is being converted, and * Capacitance is currently using microFarads, then the result will be "1.2m". * If, however, capacitance is currently using milliFarads, the result will be 0.0012u". * @param value the floating point value. * @param units the type of unit. * @return a string describing that value in the current unit. */ public static String makeUnits(double value, TextDescriptor.Unit units) { if (units == TextDescriptor.Unit.NONE) { // SMR removed the 2nd parameter to show only 3 digits// return formatDouble(value, 0); return formatDouble(value); }// if (units == TextDescriptor.Unit.RESISTANCE)// return displayedUnits(value, units, User.getResistanceUnits());// if (units == TextDescriptor.Unit.CAPACITANCE)// return displayedUnits(value, units, User.getCapacitanceUnits());// if (units == TextDescriptor.Unit.INDUCTANCE)// return displayedUnits(value, units, User.getInductanceUnits());// if (units == TextDescriptor.Unit.CURRENT)// return displayedUnits(value, units, User.getAmperageUnits());// if (units == TextDescriptor.Unit.VOLTAGE)// return displayedUnits(value, units, User.getVoltageUnits());// if (units == TextDescriptor.Unit.TIME)// return displayedUnits(value, units, User.getTimeUnits()); return (formatDoublePostFix(value));// // shouldn't get here// return "?"; } /** * Try to parse the user input String s into a Number. Conversion into the following formats * is tried in order. If a conversion is successful, that object is returned. * If no conversions are successful, this throws a NumberFormatException. * This method removes any UnitScale postfix, and scales the number accordingly. * No characters in the string are ignored - the string in its entirety (sans removed postfix) must be * able to be parsed into the Number by the usual Integer.parseInt(), Double.parseDouble() methods. * <P>Formats: Integer, Long, Double * @param s the string to parse * @return a Number that represents the string in its entirety * @throws NumberFormatException if the String is not a parsable Number. */ public static Number parsePostFixNumber(String s) throws NumberFormatException { // remove character denoting multiplier at end, if any // remove commas that denote 1000's separators s = s.replaceAll(",", ""); Number n = null; // the number Number m = null; // the multiplier for (int i=0; i<UnitScale.allUnits.length; i++) { UnitScale u = UnitScale.allUnits[i]; String postfix = u.getPostFix(); if (postfix.equals("")) continue; // ignore the NONE suffix case if (postfix.length() >= s.length()) continue; // postfix is same length or longer than string String sSuffix = s.substring(s.length()-postfix.length(), s.length()); if (sSuffix.equalsIgnoreCase(postfix)) { m = u.getMultiplier(); String sub = s.substring(0, s.length()-postfix.length()); // try to converst substring to a number try { n = parseNumber(sub); break; } catch (NumberFormatException e) { m = null; // try again } } } // if no valid postfix found, just parse number if (n == null) n = parseNumber(s); if (m != null) { if ((m instanceof Integer) && (m.intValue() == 1)) return n; if ((n instanceof Integer) && (m instanceof Integer)) { return new Integer(n.intValue() * m.intValue()); } if ((n instanceof Long) && (m instanceof Integer)) { return new Long(n.longValue() * m.longValue()); } return new Double(n.doubleValue() * m.doubleValue()); } return n; } /** * Try to parse the user input String s into a Number. Conversion into the following formats * is tried in order. If a conversion is successful, that object is returned. * If no conversions are successful, this throws a NumberFormatException. * No characters in the string are ignored - the string in its entirety must be * able to be parsed into the Number by the usual Integer.parseInt(), Double.parseDouble() methods. * <P>Formats: Integer, Long, Double * @param s the string to parse * @return a Number that represents the string in its entirety * @throws NumberFormatException if the String is not a parsable Number. */ private static Number parseNumber(String s) throws NumberFormatException { Number n = null; try { n = new Integer(s); } catch (NumberFormatException e) { // elib format does not know what a Long is //try { // n = new Long(s); //} catch (NumberFormatException ee) { try { n = new Double(s); } catch (NumberFormatException eee) {} //} } if (n == null) { NumberFormatException e = new NumberFormatException(s + "cannot be parsed into a Number"); throw e; } return n; } /** * Method to print a very long string. * The string is broken sensibly. */ public static void printLongString(String str) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -