Search in sources :

Example 81 with Formatter

use of java.util.Formatter in project android_frameworks_base by DirtyUnicorns.

the class DateUtils method formatElapsedTime.

/**
     * Formats an elapsed time in a format like "MM:SS" or "H:MM:SS" (using a form
     * suited to the current locale), similar to that used on the call-in-progress
     * screen.
     *
     * @param recycle {@link StringBuilder} to recycle, or null to use a temporary one.
     * @param elapsedSeconds the elapsed time in seconds.
     */
public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds) {
    // Break the elapsed seconds into hours, minutes, and seconds.
    long hours = 0;
    long minutes = 0;
    long seconds = 0;
    if (elapsedSeconds >= 3600) {
        hours = elapsedSeconds / 3600;
        elapsedSeconds -= hours * 3600;
    }
    if (elapsedSeconds >= 60) {
        minutes = elapsedSeconds / 60;
        elapsedSeconds -= minutes * 60;
    }
    seconds = elapsedSeconds;
    // Create a StringBuilder if we weren't given one to recycle.
    // TODO: if we cared, we could have a thread-local temporary StringBuilder.
    StringBuilder sb = recycle;
    if (sb == null) {
        sb = new StringBuilder(8);
    } else {
        sb.setLength(0);
    }
    // Format the broken-down time in a locale-appropriate way.
    // TODO: use icu4c when http://unicode.org/cldr/trac/ticket/3407 is fixed.
    Formatter f = new Formatter(sb, Locale.getDefault());
    initFormatStrings();
    if (hours > 0) {
        return f.format(sElapsedFormatHMMSS, hours, minutes, seconds).toString();
    } else {
        return f.format(sElapsedFormatMMSS, minutes, seconds).toString();
    }
}
Also used : Formatter(java.util.Formatter) RelativeDateTimeFormatter(libcore.icu.RelativeDateTimeFormatter)

Example 82 with Formatter

use of java.util.Formatter in project android_frameworks_base by DirtyUnicorns.

the class TimeFormatter method format.

/**
     * Format the specified {@code wallTime} using {@code pattern}. The output is returned.
     */
public String format(String pattern, ZoneInfo.WallTime wallTime, ZoneInfo zoneInfo) {
    try {
        StringBuilder stringBuilder = new StringBuilder();
        outputBuilder = stringBuilder;
        // This uses the US locale because number localization is handled separately (see below)
        // and locale sensitive strings are output directly using outputBuilder.
        numberFormatter = new Formatter(stringBuilder, Locale.US);
        formatInternal(pattern, wallTime, zoneInfo);
        String result = stringBuilder.toString();
        // in ASCII and not localized.
        if (localeData.zeroDigit != '0') {
            result = localizeDigits(result);
        }
        return result;
    } finally {
        outputBuilder = null;
        numberFormatter = null;
    }
}
Also used : Formatter(java.util.Formatter)

Example 83 with Formatter

use of java.util.Formatter in project metron by apache.

the class SaltyRowKeyBuilderTest method printBytes.

private void printBytes(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 2);
    Formatter formatter = new Formatter(sb);
    for (byte b : bytes) {
        formatter.format("%02x ", b);
    }
    System.out.println(sb.toString());
}
Also used : Formatter(java.util.Formatter)

Example 84 with Formatter

use of java.util.Formatter in project bnd by bndtools.

the class CommandLine method execute.

/**
	 * Execute a command in a target object with a set of options and arguments
	 * and returns help text if something fails. Errors are reported.
	 */
public String execute(Object target, String cmd, List<String> input) throws Exception {
    if (cmd.equals("help")) {
        StringBuilder sb = new StringBuilder();
        Formatter f = new Formatter(sb);
        if (input.isEmpty())
            help(f, target);
        else {
            for (String s : input) {
                help(f, target, s);
            }
        }
        f.flush();
        justif.wrap(sb);
        return sb.toString();
    }
    //
    // Find the appropriate method
    //
    List<String> arguments = new ArrayList<String>(input);
    Map<String, Method> commands = getCommands(target);
    Method m = commands.get(cmd);
    if (m == null) {
        msg.NoSuchCommand_(cmd);
        return help(target, null, null);
    }
    //
    // Parse the options
    //
    Class<? extends Options> optionClass = (Class<? extends Options>) m.getParameterTypes()[0];
    Options options = getOptions(optionClass, arguments);
    if (options == null) {
        // had some error, already reported
        return help(target, cmd, null);
    }
    // Check if we have an @Arguments annotation that
    // provides patterns for the remainder arguments
    Arguments argumentsAnnotation = optionClass.getAnnotation(Arguments.class);
    if (argumentsAnnotation != null) {
        String[] patterns = argumentsAnnotation.arg();
        if (patterns.length == 0 && arguments.size() > 0) {
            msg.TooManyArguments_(arguments);
            return help(target, cmd, null);
        }
        // Match the patterns to the given command line
        int i = 0;
        for (; i < patterns.length; i++) {
            String pattern = patterns[i];
            boolean optional = pattern.matches("\\[.*\\]");
            if (pattern.contains("...")) {
                i = Integer.MAX_VALUE;
                break;
            }
            if (i >= arguments.size()) {
                if (!optional) {
                    msg.MissingArgument_(patterns[i]);
                    return help(target, cmd, optionClass);
                }
            }
        }
        if (i < arguments.size()) {
            msg.TooManyArguments_(arguments);
            return help(target, cmd, optionClass);
        }
    }
    if (reporter.getErrors().size() == 0) {
        m.setAccessible(true);
        result = m.invoke(target, options);
        return null;
    }
    return help(target, cmd, optionClass);
}
Also used : MarkdownFormatter(aQute.lib.markdown.MarkdownFormatter) Formatter(java.util.Formatter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 85 with Formatter

use of java.util.Formatter in project bnd by bndtools.

the class AgentServer method update.

@Override
public String update(Map<String, String> bundles) throws InterruptedException {
    refresh.await();
    Formatter out = new Formatter();
    if (bundles == null) {
        bundles = Collections.emptyMap();
    }
    Set<String> toBeDeleted = new HashSet<String>(installed.keySet());
    toBeDeleted.removeAll(bundles.keySet());
    LinkedHashSet<String> toBeInstalled = new LinkedHashSet<String>(bundles.keySet());
    toBeInstalled.removeAll(installed.keySet());
    Map<String, String> changed = new HashMap<String, String>(bundles);
    changed.values().removeAll(installed.values());
    changed.keySet().removeAll(toBeInstalled);
    Set<String> affected = new HashSet<String>(toBeDeleted);
    affected.addAll(changed.keySet());
    LinkedHashSet<Bundle> toBeStarted = new LinkedHashSet<Bundle>();
    for (String location : affected) {
        Bundle b = getBundle(location);
        if (b == null) {
            out.format("Could not location bundle %s to stop it", location);
            continue;
        }
        try {
            if (isActive(b))
                toBeStarted.add(b);
            b.stop();
        } catch (Exception e) {
            printStack(e);
            out.format("Trying to stop bundle %s : %s", b, e);
        }
    }
    for (String location : toBeDeleted) {
        Bundle b = getBundle(location);
        if (b == null) {
            out.format("Could not find bundle %s to uninstall it", location);
            continue;
        }
        try {
            b.uninstall();
            installed.remove(location);
            toBeStarted.remove(b);
        } catch (Exception e) {
            printStack(e);
            out.format("Trying to uninstall %s: %s", location, e);
        }
    }
    for (String location : toBeInstalled) {
        String sha = bundles.get(location);
        try {
            InputStream in = cache.getStream(sha, source);
            if (in == null) {
                out.format("Could not find file with sha %s for bundle %s", sha, location);
                continue;
            }
            Bundle b = context.installBundle(location, in);
            installed.put(location, sha);
            toBeStarted.add(b);
        } catch (Exception e) {
            printStack(e);
            out.format("Trying to install %s: %s", location, e);
        }
    }
    for (Entry<String, String> e : changed.entrySet()) {
        String location = e.getKey();
        String sha = e.getValue();
        try {
            InputStream in = cache.getStream(sha, source);
            if (in == null) {
                out.format("Cannot find file for sha %s to update %s", sha, location);
                continue;
            }
            Bundle bundle = getBundle(location);
            if (bundle == null) {
                out.format("No such bundle for location %s while trying to update it", location);
                continue;
            }
            if (bundle.getState() == Bundle.UNINSTALLED)
                context.installBundle(location, in);
            else
                bundle.update(in);
        } catch (Exception e1) {
            printStack(e1);
            out.format("Trying to update %s: %s", location, e);
        }
    }
    for (Bundle b : toBeStarted) {
        try {
            b.start();
        } catch (Exception e1) {
            printStack(e1);
            out.format("Trying to start %s: %s", b, e1);
        }
    }
    String result = out.toString();
    out.close();
    if (result.length() == 0) {
        refresh(true);
        return null;
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Formatter(java.util.Formatter) Bundle(org.osgi.framework.Bundle) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Formatter (java.util.Formatter)558 ArrayList (java.util.ArrayList)26 File (java.io.File)25 IOException (java.io.IOException)25 Date (java.util.Date)22 Test (org.junit.Test)19 HashMap (java.util.HashMap)16 Map (java.util.Map)16 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 MessageDigest (java.security.MessageDigest)14 PrintWriter (java.io.PrintWriter)13 Justif (aQute.lib.justif.Justif)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 Locale (java.util.Locale)11 BigInteger (java.math.BigInteger)10 PrintStream (java.io.PrintStream)9 Calendar (java.util.Calendar)7 LayoutBuilder (android.text.StaticLayoutTest.LayoutBuilder)6 View (android.view.View)6