Example usage for java.lang String intern

List of usage examples for java.lang String intern

Introduction

In this page you can find the example usage for java.lang String intern.

Prototype

public native String intern();

Source Link

Document

Returns a canonical representation for the string object.

Usage

From source file:io.warp10.continuum.gts.GTSHelper.java

/**
 * Transform a Metadata instance by 'intern'ing all of its strings.
 * /*  ww w .ja  va  2s.com*/
 * @param meta
 */
public static void internalizeStrings(Metadata meta) {
    String name = meta.getName();

    if (null != name) {
        meta.setName(name.intern());
    }

    //
    // The approach we take is to create a new instance of Map, filling
    // it with the interned keys and values and replaceing labels/attributes.
    //
    // This approach is safe as we do not modify the map underlying the entrySet
    // we iterate over.
    // 
    // Modifying the original map while itearating over it is prone to throwing ConcurrentModificationException
    // in the case when a bin contains multiple nodes since we MUST do a remove then a put to intern both
    // the key AND the value.
    //
    // The ideal solution would be to have a modified HashMap which exposes an 'internalize' method which would
    // call intern for every String in keys and values
    //

    if (meta.getLabelsSize() > 0) {
        Map<String, String> newlabels = new HashMap<String, String>();
        for (Entry<String, String> entry : meta.getLabels().entrySet()) {
            String key = entry.getKey().intern();
            String value = entry.getValue().intern();
            newlabels.put(key, value);
        }
        meta.setLabels(newlabels);
    }

    if (meta.getAttributesSize() > 0) {
        Map<String, String> newattributes = new HashMap<String, String>();
        for (Entry<String, String> entry : meta.getAttributes().entrySet()) {
            String key = entry.getKey().intern();
            String value = entry.getValue().intern();
            newattributes.put(key, value);
        }
        meta.setAttributes(newattributes);
    }
}

From source file:io.warp10.continuum.gts.GTSHelper.java

public static String gtsIdToString(long classId, long labelsId, boolean intern) {
    String s = new String("01234567");

    // This way we don't create a char array twice...
    char[] c = UnsafeString.getChars(s);

    long x = classId;
    long y = labelsId;

    for (int i = 3; i >= 0; i--) {
        c[i] = (char) (x & 0xffffL);
        c[i + 4] = (char) (y & 0xffffL);
        x >>>= 16;//from  w  w  w  . ja  v a 2s . c om
        y >>>= 16;
    }

    if (intern) {
        return s.intern();
    } else {
        return s;
    }
}

From source file:com.cloud.hypervisor.xen.resource.CitrixResourceBase.java

protected SR getIscsiSR(Connection conn, String srNameLabel, String target, String path,
        String chapInitiatorUsername, String chapInitiatorPassword, Boolean[] created) {
    synchronized (srNameLabel.intern()) {
        Map<String, String> deviceConfig = new HashMap<String, String>();
        try {//from   www.j av  a2  s. c om
            if (path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }

            String tmp[] = path.split("/");
            if (tmp.length != 3) {
                String msg = "Wrong iscsi path " + path + " it should be /targetIQN/LUN";
                s_logger.warn(msg);
                throw new CloudRuntimeException(msg);
            }
            String targetiqn = tmp[1].trim();
            String lunid = tmp[2].trim();
            String scsiid = "";

            Set<SR> srs = SR.getByNameLabel(conn, srNameLabel);
            for (SR sr : srs) {
                if (!SRType.LVMOISCSI.equals(sr.getType(conn))) {
                    continue;
                }
                Set<PBD> pbds = sr.getPBDs(conn);
                if (pbds.isEmpty()) {
                    continue;
                }
                PBD pbd = pbds.iterator().next();
                Map<String, String> dc = pbd.getDeviceConfig(conn);
                if (dc == null) {
                    continue;
                }
                if (dc.get("target") == null) {
                    continue;
                }
                if (dc.get("targetIQN") == null) {
                    continue;
                }
                if (dc.get("lunid") == null) {
                    continue;
                }
                if (target.equals(dc.get("target")) && targetiqn.equals(dc.get("targetIQN"))
                        && lunid.equals(dc.get("lunid"))) {
                    throw new CloudRuntimeException("There is a SR using the same configuration target:"
                            + dc.get("target") + ",  targetIQN:" + dc.get("targetIQN") + ", lunid:"
                            + dc.get("lunid") + " for pool " + srNameLabel + "on host:" + _host.uuid);
                }
            }
            deviceConfig.put("target", target);
            deviceConfig.put("targetIQN", targetiqn);

            if (StringUtils.isNotBlank(chapInitiatorUsername)
                    && StringUtils.isNotBlank(chapInitiatorPassword)) {
                deviceConfig.put("chapuser", chapInitiatorUsername);
                deviceConfig.put("chappassword", chapInitiatorPassword);
            }

            Host host = Host.getByUuid(conn, _host.uuid);
            Map<String, String> smConfig = new HashMap<String, String>();
            String type = SRType.LVMOISCSI.toString();
            SR sr = null;
            try {
                sr = SR.create(conn, host, deviceConfig, new Long(0), srNameLabel, srNameLabel, type, "user",
                        true, smConfig);
            } catch (XenAPIException e) {
                String errmsg = e.toString();
                if (errmsg.contains("SR_BACKEND_FAILURE_107")) {
                    String lun[] = errmsg.split("<LUN>");
                    boolean found = false;
                    for (int i = 1; i < lun.length; i++) {
                        int blunindex = lun[i].indexOf("<LUNid>") + 7;
                        int elunindex = lun[i].indexOf("</LUNid>");
                        String ilun = lun[i].substring(blunindex, elunindex);
                        ilun = ilun.trim();
                        if (ilun.equals(lunid)) {
                            int bscsiindex = lun[i].indexOf("<SCSIid>") + 8;
                            int escsiindex = lun[i].indexOf("</SCSIid>");
                            scsiid = lun[i].substring(bscsiindex, escsiindex);
                            scsiid = scsiid.trim();
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        String msg = "can not find LUN " + lunid + " in " + errmsg;
                        s_logger.warn(msg);
                        throw new CloudRuntimeException(msg);
                    }
                } else {
                    String msg = "Unable to create Iscsi SR  " + deviceConfig + " due to  " + e.toString();
                    s_logger.warn(msg, e);
                    throw new CloudRuntimeException(msg, e);
                }
            }
            deviceConfig.put("SCSIid", scsiid);

            String result = SR.probe(conn, host, deviceConfig, type, smConfig);
            String pooluuid = null;
            if (result.indexOf("<UUID>") != -1) {
                pooluuid = result.substring(result.indexOf("<UUID>") + 6, result.indexOf("</UUID>")).trim();
            }

            if (pooluuid == null || pooluuid.length() != 36) {
                sr = SR.create(conn, host, deviceConfig, new Long(0), srNameLabel, srNameLabel, type, "user",
                        true, smConfig);

                created[0] = true; // note that the SR was created (as opposed to introduced)
            } else {
                sr = SR.introduce(conn, pooluuid, srNameLabel, srNameLabel, type, "user", true, smConfig);

                Set<Host> setHosts = Host.getAll(conn);

                for (Host currentHost : setHosts) {
                    PBD.Record rec = new PBD.Record();

                    rec.deviceConfig = deviceConfig;
                    rec.host = currentHost;
                    rec.SR = sr;

                    PBD pbd = PBD.create(conn, rec);

                    pbd.plug(conn);
                }
            }
            sr.scan(conn);
            return sr;
        } catch (XenAPIException e) {
            String msg = "Unable to create Iscsi SR  " + deviceConfig + " due to  " + e.toString();
            s_logger.warn(msg, e);
            throw new CloudRuntimeException(msg, e);
        } catch (Exception e) {
            String msg = "Unable to create Iscsi SR  " + deviceConfig + " due to  " + e.getMessage();
            s_logger.warn(msg, e);
            throw new CloudRuntimeException(msg, e);
        }
    }
}

From source file:org.thdl.tib.input.TibetanConverter.java

/** Runs the converter without exiting the program.
 *  @return the exit code. *//*from  ww  w  .  ja  v a 2  s . co  m*/
public static int realMain(String[] args, PrintStream out) {
    try {
        boolean convertTmwToTmwMode = false;
        boolean convertToUnicodeMode = false;
        boolean convertToTMMode = false;
        boolean convertACIPToUniMode = false;
        boolean convertACIPToTMWMode = false;
        boolean convertWylieToUniMode = false;
        boolean convertWylieToTMWMode = false;
        boolean convertToTMWMode = false;
        boolean convertToWylieRTFMode = false;
        boolean convertToWylieTextMode = false;
        boolean convertToACIPRTFMode = false;
        boolean convertToACIPTextMode = false;
        boolean convertUniToWylieTextMode = false;
        boolean findSomeNonTMWMode = false;
        boolean findAllNonTMWMode = false;
        boolean findSomeNonTMMode = false;
        boolean findAllNonTMMode = false;

        boolean colors = false;
        boolean shortMessages = false;

        String warningLevel = null;

        // Process arguments:
        final int numArgs = 8;
        if ((args.length != 1 && args.length != numArgs)
                || (args.length == 1 && !(args[0].equals("-v") || args[0].equals("--version")))
                || (args.length == numArgs && (!(args[numArgs - 8].equals("--colors"))
                        || !((colors = args[numArgs - 7].equals("yes")) || args[numArgs - 7].equals("no"))
                        || !(args[numArgs - 6].equals("--warning-level"))
                        || !((warningLevel = args[numArgs - 5]).equals("Most") || warningLevel.equals("Some")
                                || warningLevel.equals("All") || warningLevel.equals("None"))
                        || !(args[numArgs - 4].equals("--acip-to-tibetan-warning-and-error-messages"))
                        || !((shortMessages = args[numArgs - 3].equals("short"))
                                || args[numArgs - 3].equals("long"))
                        || !((findAllNonTMWMode = args[numArgs - 2].equals("--find-all-non-tmw"))
                                || (convertTmwToTmwMode = args[numArgs - 2].equals("--tmw-to-tmw-for-testing"))
                                || (convertToTMMode = args[numArgs - 2].equals("--to-tibetan-machine"))
                                || (convertUniToWylieTextMode = args[numArgs - 2]
                                        .equals("--utf8-text-to-ewts-text"))
                                || (convertToTMWMode = args[numArgs - 2].equals("--to-tibetan-machine-web"))
                                || (convertACIPToUniMode = args[numArgs - 2].equals("--acip-to-unicode"))
                                || (convertACIPToTMWMode = args[numArgs - 2].equals("--acip-to-tmw"))
                                || (convertWylieToUniMode = args[numArgs - 2].equals("--wylie-to-unicode"))
                                || (convertWylieToTMWMode = args[numArgs - 2].equals("--wylie-to-tmw"))
                                || (convertToUnicodeMode = args[numArgs - 2].equals("--to-unicode"))
                                || (convertToWylieRTFMode = args[numArgs - 2].equals("--to-wylie"))
                                || (convertToWylieTextMode = args[numArgs - 2].equals("--to-wylie-text"))
                                || (convertToACIPRTFMode = args[numArgs - 2].equals("--to-acip"))
                                || (convertToACIPTextMode = args[numArgs - 2].equals("--to-acip-text"))
                                || (findSomeNonTMWMode = args[numArgs - 2].equals("--find-some-non-tmw"))
                                || (findSomeNonTMMode = args[numArgs - 2].equals("--find-some-non-tm"))
                                || (findAllNonTMMode = args[numArgs - 2].equals("--find-all-non-tm")))))) {
            if (args.length != numArgs) {
                out.println("");
                out.println("Wrong number of arguments; needs " + numArgs + " arguments.");
                out.println("");
            }

            out.println("TibetanConverter --colors yes|no");
            out.println("                 --warning-level None|Some|Most|All");
            out.println("                 --acip-to-tibetan-warning-and-error-messages short|long"); // TODO(DLC)[EWTS->Tibetan]: misnomer, ewts and acip both are affected
            out.println("                 --find-all-non-tmw | --find-some-non-tmw");
            out.println("                   | --tmw-to-tmw-for-testing");
            out.println("                   | --to-tibetan-machine | --to-tibetan-machine-web");
            out.println("                   | --to-unicode | --to-wylie | --to-acip");
            out.println("                   | --to-wylie-text | --to-acip-text");
            out.println("                   | --wylie-to-unicode | --wylie-to-tmw");
            out.println("                   | --acip-to-unicode | --acip-to-tmw RTF_file|TXT_file");
            out.println(" | TibetanConverter [--version | -v | --help | -h]");
            out.println("");
            out.println("Distributed under the terms of the THDL Open Community License Version 1.0.");
            out.println("");
            out.println("Usage:");
            out.println(" -v | --version for version info");
            out.println("");
            out.println(" -h | --help for this message");
            out.println("");
            out.println(" --wylie-to-unicode to convert an EWTS text file to a Unicode");
            out.println("");
            out.println(" --wylie-to-tmw to convert an EWTS text file to TibetanMachineWeb");
            out.println("");
            out.println(" --to-tibetan-machine to convert TibetanMachineWeb to TibetanMachine");
            out.println("");
            out.println(" --to-unicode to convert TibetanMachineWeb to Unicode");
            out.println("");
            out.println(" --to-tibetan-machine-web to convert TibetanMachine to TibetanMachineWeb");
            out.println("");
            out.println(" --to-wylie to convert TibetanMachineWeb to THDL Extended Wylie in RTF");
            out.println("");
            out.println(" --to-wylie-text to convert TibetanMachineWeb to THDL Extended Wylie in text");
            out.println("");
            out.println(" --to-acip to convert TibetanMachineWeb to ACIP in RTF");
            out.println("");
            out.println(" --to-acip-text to convert TibetanMachineWeb to ACIP in text");
            out.println("");
            out.println(" --acip-to-unicode to convert ACIP text file to Unicode text file");
            out.println("");
            out.println(" --acip-to-tmw to convert ACIP text file to Tibetan Machine Web RTF File.");
            out.println("");
            out.println(" --find-all-non-tmw to locate all characters in the input document that are");
            out.println("   not in Tibetan Machine Web fonts, exit zero if and only if none found");
            out.println("");
            out.println(" --find-some-non-tmw to locate all distinct characters in the input document");
            out.println("   not in Tibetan Machine Web fonts, exit zero if and only if none found");
            out.println("");
            out.println(" --find-all-non-tm to locate all characters in the input document that are");
            out.println("   not in Tibetan Machine fonts, exit zero if and only if none found");
            out.println("");
            out.println(" --find-some-non-tm to locate all distinct characters in the input document");
            out.println("   not in Tibetan Machine fonts, exit zero if and only if none found");
            out.println("");
            out.println("");
            out.println("In --to... and --acip-to... modes, needs one argument, the name of the");
            out.println("TibetanMachineWeb RTF file (for --to-wylie, --to-wylie-text, --to-acip-text,");
            out.println("--to-acip, --to-unicode, and --to-tibetan-machine) or the name of");
            out.println("the TibetanMachine RTF file (for --to-tibetan-machine-web) or the name of the");
            out.println("ACIP text file (for --acip-to-unicode or --acip-to-tmw).  Writes the");
            out.println("result to standard output (after dealing with the curly brace problem if");
            out.println("the input is TibetanMachineWeb).  Exit code is zero on success, 42 if some");
            out.println("glyphs couldn't be converted (in which case the output is just those glyphs),");
            out.println("44 if a TMW->Wylie conversion ran into some glyphs that couldn't be");
            out.println("converted, in which case ugly error messages like");
            out.println("   \"<<[[JSKAD_TMW_TO_WYLIE_ERROR_NO_SUCH_WYLIE: Cannot convert DuffCode...\"");
            out.println("are in your document waiting for your personal attention,");
            out.println("43 if not even one glyph found was eligible for this conversion, which means");
            out.println("that you probably selected the wrong conversion or the wrong document, or ");
            out.println("nonzero on some other error.");
            // TODO(dchandler): describe 47 48 50 etc.
            out.println("");
            out.println("You may find it helpful to use `--find-some-non-tmw' mode (or");
            out.println("`--find-some-non-tm' mode for Tibetan Machine input) before doing a");
            out.println("conversion so that you have confidence in the conversion's correctness.");
            out.println("");
            out.println("When using short error and warning messages for ACIP->Tibetan conversions,");
            out.println("i.e. when '--acip-to-tibetan-warning-and-error-messages short' is given,");
            out.println("the output will contain error and warning numbers.  The following are the");
            out.println("long forms of each warning and error:");
            out.println("");
            org.thdl.tib.text.ttt.ErrorsAndWarnings.printErrorAndWarningDescriptions(out);
            return 77;
        }
        if (args[0].equals("--version") || args[0].equals("-v")) {
            out.println("TibetanConverter version 0.84");
            out.println("Compiled at " + ThdlVersion.getTimeOfCompilation());
            return 77;
        }
        String inputRtfPath = args[args.length - 1];

        InputStream in;
        if (inputRtfPath.equals("-"))
            in = System.in;
        else
            in = new FileInputStream(inputRtfPath);

        String conversionTag = null;
        if (findAllNonTMWMode) {
            conversionTag = FIND_ALL_NON_TMW;
        } else if (findSomeNonTMWMode) {
            conversionTag = FIND_SOME_NON_TMW;
        } else if (findSomeNonTMMode) {
            conversionTag = FIND_SOME_NON_TM;
        } else if (findAllNonTMMode) {
            conversionTag = FIND_ALL_NON_TM;
        } else { // conversion {to Wylie or TM} mode
            if (convertToWylieRTFMode) {
                conversionTag = TMW_TO_WYLIE;
            } else if (convertToWylieTextMode) {
                conversionTag = TMW_TO_WYLIE_TEXT;
            } else if (convertUniToWylieTextMode) {
                conversionTag = UNI_TO_WYLIE_TEXT;
            } else if (convertToACIPRTFMode) {
                conversionTag = TMW_TO_ACIP;
            } else if (convertToACIPTextMode) {
                conversionTag = TMW_TO_ACIP_TEXT;
            } else if (convertToUnicodeMode) {
                conversionTag = TMW_TO_UNI;
            } else if (convertTmwToTmwMode) {
                conversionTag = TMW_TO_SAME_TMW;
            } else if (convertToTMWMode) {
                conversionTag = TM_TO_TMW;
            } else if (convertACIPToUniMode) {
                conversionTag = ACIP_TO_UNI_TEXT;
            } else if (convertACIPToTMWMode) {
                conversionTag = ACIP_TO_TMW;
            } else if (convertWylieToUniMode) {
                conversionTag = WYLIE_TO_UNI_TEXT;
            } else if (convertWylieToTMWMode) {
                conversionTag = WYLIE_TO_TMW;
            } else {
                ThdlDebug.verify(convertToTMMode);
                conversionTag = TMW_TO_TM;
            }
        }
        return reallyConvert(in, out, conversionTag, warningLevel.intern(), shortMessages, colors);
    } catch (ThdlLazyException e) {
        out.println("TibetanConverter has a BUG:");
        e.getRealException().printStackTrace(out);
        System.err.println("TibetanConverter has a BUG:");
        e.getRealException().printStackTrace(System.err);
        return 7;
    } catch (IOException e) {
        e.printStackTrace(out);
        e.printStackTrace(System.err);
        return 4;
    } catch (OutOfMemoryError e) {
        e.printStackTrace(out);
        e.printStackTrace(System.err);
        throw e;
    }
}