Example usage for com.google.common.hash Hasher putBoolean

List of usage examples for com.google.common.hash Hasher putBoolean

Introduction

In this page you can find the example usage for com.google.common.hash Hasher putBoolean.

Prototype

@Override
Hasher putBoolean(boolean b);

Source Link

Document

Equivalent to putByte(b ?

Usage

From source file:com.facebook.buck.json.JsonObjectHashing.java

/**
 * Given a {@link Hasher} and a parsed BUCK file object, updates the Hasher
 * with the contents of the JSON object.
 *///from w  w  w  .  j a va 2s  . c  o  m
public static void hashJsonObject(Hasher hasher, @Nullable Object obj) {
    if (obj instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) obj;
        ImmutableSortedMap.Builder<String, Optional<Object>> sortedMapBuilder = ImmutableSortedMap
                .naturalOrder();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            Object key = entry.getKey();
            if (!(key instanceof String)) {
                throw new RuntimeException(String.format(
                        "Keys of JSON maps are expected to be strings. Actual type: %s, contents: %s",
                        key.getClass().getName(), key));
            }
            Object value = entry.getValue();
            if (value != null) {
                sortedMapBuilder.put((String) key, Optional.of(value));
            }
        }
        ImmutableSortedMap<String, Optional<Object>> sortedMap = sortedMapBuilder.build();
        hasher.putInt(HashedObjectType.MAP.ordinal());
        hasher.putInt(sortedMap.size());
        for (Map.Entry<String, Optional<Object>> entry : sortedMap.entrySet()) {
            hashJsonObject(hasher, entry.getKey());
            if (entry.getValue().isPresent()) {
                hashJsonObject(hasher, entry.getValue().get());
            } else {
                hashJsonObject(hasher, null);
            }
        }
    } else if (obj instanceof Collection) {
        Collection<?> collection = (Collection<?>) obj;
        hasher.putInt(HashedObjectType.LIST.ordinal());
        hasher.putInt(collection.size());
        for (Object collectionEntry : collection) {
            hashJsonObject(hasher, collectionEntry);
        }
    } else if (obj instanceof String) {
        hasher.putInt(HashedObjectType.STRING.ordinal());
        byte[] stringBytes = ((String) obj).getBytes(Charsets.UTF_8);
        hasher.putInt(stringBytes.length);
        hasher.putBytes(stringBytes);
    } else if (obj instanceof Boolean) {
        hasher.putInt(HashedObjectType.BOOLEAN.ordinal());
        hasher.putBoolean((boolean) obj);
    } else if (obj instanceof Number) {
        // This is gross, but it mimics the logic originally in RawParser.
        Number number = (Number) obj;
        if (number.longValue() == number.doubleValue()) {
            hasher.putInt(HashedObjectType.LONG.ordinal());
            hasher.putLong(number.longValue());
        } else {
            hasher.putInt(HashedObjectType.DOUBLE.ordinal());
            hasher.putDouble(number.doubleValue());
        }
    } else if (obj instanceof Void || obj == null) {
        hasher.putInt(HashedObjectType.NULL.ordinal());
    } else {
        throw new RuntimeException(String.format("Unsupported object %s (class %s)", obj, obj.getClass()));
    }
}

From source file:org.dllearner.reasoning.MaterializableFastInstanceChecker.java

private void loadOrDematerialize() {
    if (useCaching) {
        File cacheDir = new File("cache");
        cacheDir.mkdirs();//from w w  w. j  a  va2s.c o m
        HashFunction hf = Hashing.md5();
        Hasher hasher = hf.newHasher();
        hasher.putBoolean(materializeExistentialRestrictions);
        hasher.putBoolean(handlePunning);
        for (OWLOntology ont : rc.getOWLAPIOntologies()) {
            hasher.putInt(ont.getLogicalAxioms().hashCode());
            hasher.putInt(ont.getAxioms().hashCode());
        }
        String filename = hasher.hash().toString() + ".obj";

        File cacheFile = new File(cacheDir, filename);
        if (cacheFile.exists()) {
            logger.debug("Loading materialization from disk...");
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cacheFile))) {
                Materialization mat = (Materialization) ois.readObject();
                classInstancesPos = mat.classInstancesPos;
                classInstancesNeg = mat.classInstancesNeg;
                opPos = mat.opPos;
                dpPos = mat.dpPos;
                bdPos = mat.bdPos;
                bdNeg = mat.bdNeg;
                dd = mat.dd;
                id = mat.id;
                sd = mat.sd;
            } catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            }
            logger.debug("done.");
        } else {
            dematerialize();
            Materialization mat = new Materialization();
            mat.classInstancesPos = classInstancesPos;
            mat.classInstancesNeg = classInstancesNeg;
            mat.opPos = opPos;
            mat.dpPos = dpPos;
            mat.bdPos = bdPos;
            mat.bdNeg = bdNeg;
            mat.dd = dd;
            mat.id = id;
            mat.sd = sd;
            try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cacheFile))) {
                oos.writeObject(mat);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        dematerialize();
    }
}

From source file:org.dllearner.reasoning.FastInstanceChecker.java

private void loadOrDematerialize() {
    if (useCaching) {
        File cacheDir = new File("cache");
        cacheDir.mkdirs();/* w  w w  .  jav a 2s.c o m*/
        HashFunction hf = Hashing.md5();
        Hasher hasher = hf.newHasher();
        hasher.putBoolean(materializeExistentialRestrictions);
        hasher.putBoolean(handlePunning);
        for (OWLOntology ont : rc.getOWLAPIOntologies()) {
            hasher.putInt(ont.getLogicalAxioms().hashCode());
            hasher.putInt(ont.getAxioms().hashCode());
        }
        String filename = hasher.hash().toString() + ".obj";

        File cacheFile = new File(cacheDir, filename);
        if (cacheFile.exists()) {
            logger.debug("Loading materialization from disk...");
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cacheFile))) {
                Materialization mat = (Materialization) ois.readObject();
                classInstancesPos = mat.classInstancesPos;
                classInstancesNeg = mat.classInstancesNeg;
                opPos = mat.opPos;
                dpPos = mat.dpPos;
                bdPos = mat.bdPos;
                bdNeg = mat.bdNeg;
                dd = mat.dd;
                id = mat.id;
                sd = mat.sd;
            } catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            }
            logger.debug("done.");
        } else {
            materialize();
            Materialization mat = new Materialization();
            mat.classInstancesPos = classInstancesPos;
            mat.classInstancesNeg = classInstancesNeg;
            mat.opPos = opPos;
            mat.dpPos = dpPos;
            mat.bdPos = bdPos;
            mat.bdNeg = bdNeg;
            mat.dd = dd;
            mat.id = id;
            mat.sd = sd;
            try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cacheFile))) {
                oos.writeObject(mat);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        materialize();
    }
}

From source file:org.dllearner.reasoning.ClosedWorldReasoner.java

private void loadOrDematerialize() {
    if (useMaterializationCaching) {
        File cacheDir = new File("cache");
        cacheDir.mkdirs();//w w w  .jav  a2s .  co  m
        HashFunction hf = Hashing.md5();
        Hasher hasher = hf.newHasher();
        hasher.putBoolean(materializeExistentialRestrictions);
        hasher.putBoolean(handlePunning);
        for (OWLOntology ont : Collections.singleton(baseReasoner.getOntology())) {
            hasher.putInt(ont.getLogicalAxioms().hashCode());
            hasher.putInt(ont.getAxioms().hashCode());
        }
        String filename = hasher.hash().toString() + ".obj";

        File cacheFile = new File(cacheDir, filename);
        if (cacheFile.exists()) {
            logger.debug("Loading materialization from disk...");
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cacheFile))) {
                Materialization mat = (Materialization) ois.readObject();
                classInstancesPos = mat.classInstancesPos;
                classInstancesNeg = mat.classInstancesNeg;
                opPos = mat.opPos;
                dpPos = mat.dpPos;
                bdPos = mat.bdPos;
                bdNeg = mat.bdNeg;
                dd = mat.dd;
                id = mat.id;
                sd = mat.sd;
            } catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            }
            logger.debug("done.");
        } else {
            materialize();
            Materialization mat = new Materialization();
            mat.classInstancesPos = classInstancesPos;
            mat.classInstancesNeg = classInstancesNeg;
            mat.opPos = opPos;
            mat.dpPos = dpPos;
            mat.bdPos = bdPos;
            mat.bdNeg = bdNeg;
            mat.dd = dd;
            mat.id = id;
            mat.sd = sd;
            try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cacheFile))) {
                oos.writeObject(mat);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        materialize();
    }
}