/** * Get current value as double. * * @return double value * @throws NumberFormatException when current value cannot be parsed as double * value. */ public double getDouble() throws NumberFormatException { return FormatUtil.parseDouble(input, start, end); }
/** * Formats the float array f with ',' as separator and 2 fraction digits. * * @param f the float array to be formatted * @return a String representing the float array f */ public static String format(float[] f) { return format(f, ", ", NF2); }
/** * Formats the double array d with ', ' as separator and 2 fraction digits. * * @param d the double array to be formatted * @return a String representing the double array d */ public static String format(double[] d) { return formatTo(new StringBuilder(), d, ", ").toString(); }
/** * Get current value as long. * * @return double value * @throws NumberFormatException when current value cannot be parsed as long * value. */ public long getLongBase10() throws NumberFormatException { return FormatUtil.parseLongBase10(input, start, end); }
/** * Returns a usage message, explaining all known options * * @param options Options to show in usage. * @return a usage message explaining all known options */ public static String usage(Collection<TrackedParameter> options) { StringBuilder usage = new StringBuilder(); if(!REFERENCE_VERSION.equals(VERSION)) { usage.append("ELKI build: " + VERSION + NEWLINE + NEWLINE); } usage.append(REFERENCE); // Collect options usage.append(NEWLINE).append("Parameters:").append(NEWLINE); OptionUtil.formatForConsole(usage, FormatUtil.getConsoleWidth(), " ", options); // FIXME: re-add global constraints! return usage.toString(); }
/** * Simple writing helper with no indentation. * * @param buf Buffer to write to * @param width Width to use for linewraps * @param data Data to write. * @param indent Indentation */ public static void println(StringBuilder buf, int width, String data, String indent) { for(String line : FormatUtil.splitAtLastBlank(data, width - indent.length())) { buf.append(indent); buf.append(line); if(!line.endsWith(FormatUtil.NEWLINE)) { buf.append(FormatUtil.NEWLINE); } } }
int bp = nextPosition(s.indexOf(' ', width), s.indexOf(NEWLINE, width)); if(bp >= 0) { return bp;
/** * Splits the specified string at the last blank before width. If there is no * blank before the given width, it is split at the next. * * @param s the string to be split * @param width int * @return string fragments */ public static List<String> splitAtLastBlank(String s, int width) { List<String> chunks = new ArrayList<>(); String tmp = s; while(tmp.length() > 0) { int index = findSplitpoint(tmp, width); // store first part chunks.add(tmp.substring(0, index)); // skip whitespace at beginning of line while(index < tmp.length() && tmp.charAt(index) == ' ') { index += 1; } // remove a newline if(index < tmp.length() && tmp.regionMatches(index, NEWLINE, 0, NEWLINE.length())) { index += NEWLINE.length(); } if(index >= tmp.length()) { break; } tmp = tmp.substring(index); } return chunks; }
@Override protected Integer parseValue(Object obj) throws ParameterException { if(obj instanceof Integer) { return (Integer) obj; } try { final String s = obj.toString(); return (int) FormatUtil.parseLongBase10(s, 0, s.length()); } catch(NullPointerException e) { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires an integer value, read: " + obj + "!\n"); } catch(NumberFormatException e) { throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires an integer value, read: " + obj + "!\n"); } }
/** * Print the description for the given parameter */ private static void printDescription(Class<?> descriptionClass) { if(descriptionClass != null) { LoggingConfiguration.setVerbose(Level.VERBOSE); LOG.verbose(OptionUtil.describeParameterizable(new StringBuilder(), descriptionClass, FormatUtil.getConsoleWidth(), " ").toString()); } }
/** * Parse a double from a character sequence. * * In contrast to Javas {@link Double#parseDouble}, this will <em>not</em> * create an object and thus is expected to put less load on the garbage * collector. It will accept some more spellings of NaN and infinity, thus * removing the need for checking for these independently. * * @param str String * @return Double value */ public static double parseDouble(final CharSequence str) { return parseDouble(str, 0, str.length()); }
/** * Returns a string representation of the object. * * @return a string representation of the object. */ @Override public String toString() { return FormatUtil.format(b, FormatUtil.NF4); } }
/** * Formats the int array a for printing purposes. * * @param a the int array to be formatted * @param sep the separator between the single values of the array, e.g. ',' * @return a String representing the int array a */ public static String format(int[] a, String sep) { return (a == null) ? "null" : (a.length == 0) ? "" : // formatTo(new StringBuilder(), a, sep).toString(); }
@Override public double getTextSize(String key) { Double lw = getCached(key, TEXT_SIZE, Double.class); if (lw == null) { try { lw = Double.valueOf(FormatUtil.parseDouble(getPropertyValue(key, TEXT_SIZE)) * SCALE); } catch (NullPointerException e) { throw new AbortException("Missing/invalid value in style library: " + key + '.' + TEXT_SIZE); } } return lw.doubleValue(); }
/** * Returns a string representation of this vector without adding extra * whitespace. * * @return a string representation of this vector. */ public final String toStringNoWhitespace() { return "[" + FormatUtil.format(elements, ",") + "]"; }
/** * Formats the double array d with the specified separator. * * @param d the double array to be formatted * @param sep the separator between the single values of the double array, * e.g. ',' * @return a String representing the double array d */ public static String format(double[] d, String sep) { if(d.length == 0) { return ""; } return formatTo(new StringBuilder(), d, sep).toString(); }
@Override protected double[] parseValue(Object obj) throws ParameterException { if(obj instanceof double[]) { return double[].class.cast(obj); } if(obj instanceof String) { String[] values = SPLIT.split((String) obj); double[] doubleValue = new double[values.length]; for(int i = 0; i < values.length; i++) { doubleValue[i++] = FormatUtil.parseDouble(values[i]); } return doubleValue; } if(obj instanceof Double) { return new double[] { (Double) obj }; } throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a list of Double values!"); }
/** * Formats the double array d with the specified number format. * * @param d the double array to be formatted * @param nf the number format to be used for formatting * @return a String representing the double array d */ public static String format(double[] d, NumberFormat nf) { return format(d, " ", nf); }