Java Code Examples for com.google.common.hash.Hasher#putChar()

The following examples show how to use com.google.common.hash.Hasher#putChar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NullHasherTest.java    From elastic-load-balancing-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasics() {
    Hasher hasher = NullHasher.INSTANCE;
    assertEquals(0, hasher.hash().asInt());
    hasher.putBoolean(false);
    hasher.putByte((byte) 3);
    hasher.putBytes(new byte[0]);
    hasher.putBytes(null, 3, 3);
    hasher.putChar('c');
    hasher.putDouble(3.3);
    hasher.putFloat(3.4f);
    hasher.putInt(7);
    hasher.putLong(3);
    hasher.putObject(null, null);
    hasher.putShort((short) 7);
    hasher.putString(null, null);
    hasher.putUnencodedChars(null);
}
 
Example 2
Source File: DefaultInferredElementFragmentProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Computes a hash code for the given {@link #getEClass(EObject) EClass} and {@link #getQualifiedName(EObject) qualified name}.
 *
 * @param eClass
 *          EClass to base hash on, must not be {@code null}
 * @param name
 *          qualified name of inferred model element, can be {@code null}
 * @return hash code, never {@code null}
 */
protected HashCode computeHash(final EClass eClass, final QualifiedName name) {
  byte[] eClassUriBytes = eClassToUriBytesMap.get(eClass);
  if (eClassUriBytes == null) {
    eClassUriBytes = EcoreUtil.getURI(eClass).toString().getBytes(Charsets.UTF_8);
    eClassToUriBytesMap.put(eClass, eClassUriBytes);
  }

  Hasher hasher = hashFunction.newHasher(HASHER_CAPACITY);
  hasher.putBytes(eClassUriBytes);

  if (name != null) {
    hasher.putChar('/');

    for (int j = 0; j < name.getSegmentCount(); j++) {
      hasher.putUnencodedChars(name.getSegment(j)).putChar('.');
    }
  }

  return hasher.hash();
}
 
Example 3
Source File: FastPaths.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the Path to the hasher as unencoded chars. Roughly equivalent to {@code
 * hasher.putUnencodedChars(path.toString())}.
 */
public static Hasher hashPathFast(Hasher hasher, Path path) {
  if (!(path instanceof BuckUnixPath)) {
    return hasher.putUnencodedChars(path.toString());
  }
  if (path.isAbsolute()) {
    hasher.putChar('/');
  }
  for (int i = 0; i < path.getNameCount(); i++) {
    if (i != 0) {
      hasher.putChar('/');
    }
    hasher.putUnencodedChars(FastPaths.getNameString(path, i));
  }
  return hasher;
}
 
Example 4
Source File: AbstractNonStreamingHashFunctionTest.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
private static void assertPutString(char[] chars) {
  Hasher h1 = new NonStreamingVersion().newHasher();
  Hasher h2 = new NonStreamingVersion().newHasher();
  String s = new String(chars);
  // this is the correct implementation of the spec
  for (int i = 0; i < s.length(); i++) {
    h1.putChar(s.charAt(i));
  }
  h2.putUnencodedChars(s);
  assertEquals(h1.hash(), h2.hash());
}
 
Example 5
Source File: Path.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Return a string representation, as hexadecimal digits, of some hash of the directory.
 *
 * <p>The hash itself is computed according to the design document
 * https://github.com/bazelbuild/proposals/blob/master/designs/2018-07-13-repository-hashing.md
 * and takes enough information into account, to detect the typical non-reproducibility
 * of source-like repository rules, while leaving out what will change from invocation to
 * invocation of a repository rule (in particular file owners) and can reasonably be ignored
 * when considering if a repository is "the same source tree".
 *
 * @return a string representation of the bash of the directory
 * @throws IOException if the digest could not be computed for any reason
 */
public String getDirectoryDigest() throws IOException {
  List<String> entries = new ArrayList<String>(fileSystem.getDirectoryEntries(this));
  Collections.sort(entries);
  Hasher hasher = fileSystem.getDigestFunction().getHashFunction().newHasher();
  for (String entry : entries) {
    Path path = this.getChild(entry);
    FileStatus stat = path.stat(Symlinks.NOFOLLOW);
    hasher.putUnencodedChars(entry);
    if (stat.isFile()) {
      if (path.isExecutable()) {
        hasher.putChar('x');
      } else {
        hasher.putChar('-');
      }
      hasher.putBytes(path.getDigest());
    } else if (stat.isDirectory()) {
      hasher.putChar('d').putUnencodedChars(path.getDirectoryDigest());
    } else if (stat.isSymbolicLink()) {
      PathFragment link = path.readSymbolicLink();
      if (link.isAbsolute()) {
        try {
          Path resolved = path.resolveSymbolicLinks();
          if (resolved.isFile()) {
            if (resolved.isExecutable()) {
              hasher.putChar('x');
            } else {
              hasher.putChar('-');
            }
            hasher.putBytes(resolved.getDigest());
          } else {
            // link to a non-file: include the link itself in the hash
            hasher.putChar('l').putUnencodedChars(link.toString());
          }
        } catch (IOException e) {
          // dangling link: include the link itself in the hash
          hasher.putChar('l').putUnencodedChars(link.toString());
        }
      } else {
        // relative link: include the link itself in the hash
        hasher.putChar('l').putUnencodedChars(link.toString());
      }
    } else {
      // Neither file, nor directory, nor symlink. So do not include further information
      // in the hash, asuming it will not be used during the BUILD anyway.
      hasher.putChar('s');
    }
  }
  return hasher.hash().toString();
}