Example usage for java.lang String hashCode

List of usage examples for java.lang String hashCode

Introduction

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

Prototype

public int hashCode() 

Source Link

Document

Returns a hash code for this string.

Usage

From source file:automenta.climatenet.ImportKML.java

public static String getSerial(String layer, int serial) {
    return (Base64.getEncoder().encodeToString(
            BigInteger.valueOf(serial).add(BigInteger.valueOf(layer.hashCode()).shiftRight(32)).toByteArray()));
}

From source file:msuresh.raftdistdb.RaftClient.java

public static void GetValue(String name, String key) throws FileNotFoundException {
    if (key == null || key.isEmpty()) {
        return;/*from   w w w  .  ja  v a  2  s  .c om*/
    }
    File configFile = new File(Constants.STATE_LOCATION + name + ".info");
    if (!configFile.exists() || configFile.isDirectory()) {
        FileNotFoundException ex = new FileNotFoundException();
        throw ex;
    }
    try {
        System.out.println("Getting key .. Hold on.");
        String content = new Scanner(configFile).useDelimiter("\\Z").next();
        JSONObject config = (JSONObject) (new JSONParser()).parse(content);
        Long numPart = (Long) config.get("countPartitions");
        Integer shardId = key.hashCode() % numPart.intValue();
        JSONArray memberJson = (JSONArray) config.get(shardId.toString());
        List<Address> members = new ArrayList<>();
        for (int i = 0; i < memberJson.size(); i++) {
            JSONObject obj = (JSONObject) memberJson.get(i);
            Long port = (Long) obj.get("port");
            String address = (String) obj.get("address");
            members.add(new Address(address, port.intValue()));
        }
        CopycatClient client = CopycatClient.builder(members).withTransport(new NettyTransport()).build();
        client.open().join();
        Object str = client.submit(new GetQuery(key)).get();
        System.out.println("For the key : " + key + ", the database returned the value : " + (String) str);
        client.close().join();
        while (client.isOpen()) {
            Thread.sleep(1000);
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}

From source file:msuresh.raftdistdb.RaftClient.java

public static void SetValue(String name, String key, String value) throws FileNotFoundException {
    if (key == null || key.isEmpty()) {
        return;//from   w  ww  .j av  a2 s.co m
    }
    File configFile = new File(Constants.STATE_LOCATION + name + ".info");
    if (!configFile.exists() || configFile.isDirectory()) {
        FileNotFoundException ex = new FileNotFoundException();
        throw ex;
    }
    try {
        System.out.println("Adding key .. hold on..");
        String content = new Scanner(configFile).useDelimiter("\\Z").next();
        JSONObject config = (JSONObject) (new JSONParser()).parse(content);
        Long numPart = (Long) config.get("countPartitions");
        Integer shardId = key.hashCode() % numPart.intValue();
        JSONArray memberJson = (JSONArray) config.get(shardId.toString());
        List<Address> members = new ArrayList<>();
        for (int i = 0; i < memberJson.size(); i++) {
            JSONObject obj = (JSONObject) memberJson.get(i);
            Long port = (Long) obj.get("port");
            String address = (String) obj.get("address");
            members.add(new Address(address, port.intValue()));
        }
        CopycatClient client = CopycatClient.builder(members).withTransport(new NettyTransport()).build();
        client.open().join();
        client.submit(new PutCommand(key, value)).get();
        client.close().join();
        while (client.isOpen()) {
            Thread.sleep(1000);
        }
        System.out.println("key " + key + " with value : " + value + " has been added to the cluster");
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}

From source file:com.eurotong.orderhelperandroid.Common.java

public static String GetDeviceUniqueID() {
    //http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id/2853253#2853253
    String android_id = Secure.getString(MyApplication.getAppContext().getContentResolver(), Secure.ANDROID_ID);

    long id = -1;
    long idMod = 0;
    String idString = "UNKNOWN";
    if (android_id != null) {
        id = abs(android_id.hashCode()); //could get negative value
        idString = Long.toString(id);
        idString = idString.concat("0000000000").substring(0, 10);
        id = Long.parseLong(idString);
        idMod = id % Define.MOD;/*ww  w  .  j  a  v  a2 s  .  c om*/
        String idModString = Long.toString(idMod);
        idModString = "00".concat(idModString);
        idModString = idModString.substring(idModString.length() - 2, idModString.length());
        idString = Long.toString(id) + idModString;
        //id=id / 10000;
    }
    return idString;
}

From source file:com.microsoft.azure.util.AzureUtil.java

/**
 * Creates a new VM base name given the input parameters.
 * @param templateName Template name//from  ww  w  .  j  a  v a2s  .com
 * @param osType Type of OS
 * @param numberOfVmsToCreate Number of VMs that will be created
 *       (which is added to the suffix of the VM name by azure)
 * @return 
 */
public static String getVMBaseName(String templateName, String deploymentName, String osType, int numberOfVMs) {
    if (!isValidTemplateName(templateName)) {
        throw new IllegalArgumentException("Invalid template name");
    }

    // For VM names, we use a simpler form.  VM names are pretty short 
    int numberOfDigits = (int) Math.floor(Math.log10((double) numberOfVMs)) + 1;
    // Get the hash of the deployment name
    Integer deploymentHashCode = deploymentName.hashCode();
    // Convert the int into a hex string and do a substring
    String shortenedDeploymentHash = Integer.toHexString(deploymentHashCode).substring(0,
            Constants.VM_NAME_HASH_LENGTH - 1);
    return String.format("%s%s",
            getShortenedTemplateName(templateName, osType, Constants.VM_NAME_HASH_LENGTH, numberOfDigits),
            shortenedDeploymentHash);
}

From source file:im.r_c.android.fusioncache.DiskCache.java

/**
 * A hashing method that changes a string (like a URL) into a hash
 * suitable for using as a disk filename.
 *///from  ww w  .  ja  v  a 2  s . c  om
private static String hashKeyForDisk(String key) {
    String cacheKey;
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}

From source file:Main.java

/**
 * Return pseudo unique ID//from w  w  w.j  a  v a 2 s  .  co  m
 * @return ID
 */
public static String getUniquePsuedoID() {
    // If all else fails, if the user does have lower than API 9 (lower
    // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
    // returns 'null', then simply the ID returned will be solely based
    // off their Android device information. This is where the collisions
    // can happen.
    // Thanks http://www.pocketmagic.net/?p=1662!
    // Try not to use DISPLAY, HOST or ID - these items could change.
    // If there are collisions, there will be overlapping data
    String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10)
            + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10)
            + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);

    // Thanks to @Roman SL!
    // http://stackoverflow.com/a/4789483/950427
    // Only devices with API >= 9 have android.os.Build.SERIAL
    // http://developer.android.com/reference/android/os/Build.html#SERIAL
    // If a user upgrades software or roots their phone, there will be a duplicate entry
    String serial = null;
    try {
        serial = android.os.Build.class.getField("SERIAL").get(null).toString();

        // Go ahead and return the serial for api => 9
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    } catch (Exception e) {
        // String needs to be initialized
        serial = "serial"; // some value
    }

    // Thanks @Joe!
    // http://stackoverflow.com/a/2853253/950427
    // Finally, combine the values we have found by using the UUID class to create a unique identifier
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}

From source file:com.redhat.rhn.manager.profile.test.ProfileManagerTest.java

public static PackageListItem createItem(String evrString, int nameId) {
    PackageListItem pli = new PackageListItem();
    String[] evr = StringUtils.split(evrString, "-");
    pli.setName(evr[0]);//from  w w w . ja  v a 2 s.  c om
    pli.setVersion(evr[1]);
    pli.setRelease(evr[2]);
    pli.setEvrId(new Long(evrString.hashCode()));
    pli.setIdCombo(nameId + "|" + evrString.hashCode());
    pli.setEvr(evrString);
    pli.setNameId(new Long(nameId));
    return pli;
}

From source file:com.redhat.rhn.manager.profile.test.ProfileManagerTest.java

public static PackageListItem createPackageListItem(String evrString, int nameId) {
    PackageListItem pli = new PackageListItem();
    String[] evr = StringUtils.split(evrString, "-");
    pli.setName(evr[0]);/*from   www.  j a va  2  s .  co  m*/
    pli.setVersion(evr[1]);
    pli.setRelease(evr[2]);
    pli.setEvrId(new Long(evrString.hashCode()));
    pli.setIdCombo(nameId + "|" + evrString.hashCode());
    pli.setEvr(evrString);
    pli.setNameId(new Long(nameId));
    PackageArch pa = PackageFactory.lookupPackageArchByLabel("x86_64");
    pli.setArch(pa.getLabel());
    pli.setArchId(pa.getId());
    return pli;
}

From source file:Main.java

/**
 * Return pseudo unique ID//from  w w  w .j a va 2s. com
 * 
 * @return ID
 */
public static String getUniquePsuedoID() {
    // If all else fails, if the user does have lower than API 9 (lower
    // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
    // returns 'null', then simply the ID returned will be solely based
    // off their Android device information. This is where the collisions
    // can happen.
    // Thanks http://www.pocketmagic.net/?p=1662!
    // Try not to use DISPLAY, HOST or ID - these items could change.
    // If there are collisions, there will be overlapping data
    String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10)
            + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10)
            + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);

    // Thanks to @Roman SL!
    // http://stackoverflow.com/a/4789483/950427
    // Only devices with API >= 9 have android.os.Build.SERIAL
    // http://developer.android.com/reference/android/os/Build.html#SERIAL
    // If a user upgrades software or roots their phone, there will be a
    // duplicate entry
    String serial = null;
    try {
        serial = android.os.Build.class.getField("SERIAL").get(null).toString();

        // Go ahead and return the serial for api => 9
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    } catch (Exception e) {
        // String needs to be initialized
        serial = "serial"; // some value
    }

    // Thanks @Joe!
    // http://stackoverflow.com/a/2853253/950427
    // Finally, combine the values we have found by using the UUID class to
    // create a unique identifier
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}