public String getId(String name) {
    // Use the id directly if it is unique and the length is less than max
    if (name.length() <= maxHashLength && usedIds.add(name)) {
      return name;
    }

    // Pick the last bytes of hashcode and use hex format
    final String hexString = Integer.toHexString(name.hashCode());
    final String origId =
        hexString.length() <= maxHashLength
            ? hexString
            : hexString.substring(Math.max(0, hexString.length() - maxHashLength));
    String id = origId;
    int suffixNum = 2;
    while (!usedIds.add(id)) {
      // A duplicate!  Retry.
      id = origId + "-" + suffixNum++;
    }
    LOG.info("Name {} is mapped to id {}", name, id);
    return id;
  }
@Test
  public void testGetId() {
    final HashIdGenerator idGenerator = new HashIdGenerator();
    final Set<String> ids =
        ImmutableSet.of(
            idGenerator.getId(Count.perKey().getName()),
            idGenerator.getId(MapElements.into(null).getName()),
            idGenerator.getId(Count.globally().getName()),
            idGenerator.getId(Combine.perKey(mock(SerializableFunction.class)).getName()),
            idGenerator.getId(Min.perKey().getName()),
            idGenerator.getId(Max.globally().getName()));
    Assert.assertEquals(6, ids.size());
  }