<commit_before>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal.tasks.cache;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A builder for hash keys
 *
 * In order to avoid collisions we prepend the length of the next bytes to the underlying
 * hasher (see this <a href="http://crypto.stackexchange.com/a/10065">answer</a> on stackexchange.)
 */
public class DefaultTaskCacheKeyBuilder implements TaskCacheKeyBuilder {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTaskCacheKeyBuilder.class);
    private final Hasher hasher = Hashing.md5().newHasher();

    @Override
    public TaskCacheKeyBuilder putByte(byte b) {
        log("byte", b);
        hasher.putLong(1);
        hasher.putByte(b);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putBytes(byte[] bytes) {
        log("bytes", new ByteArrayToStringer(bytes));
        hasher.putLong(bytes.length);
        hasher.putBytes(bytes);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putBytes(byte[] bytes, int off, int len) {
        log("bytes", new ByteArrayToStringer(bytes, off, len));
        hasher.putLong(len);
        hasher.putBytes(bytes, off, len);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putInt(int i) {
        log("int", i);
        hasher.putLong(4);
        hasher.putInt(i);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putLong(long l) {
        log("long", l);
        hasher.putLong(8);
        hasher.putLong(l);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putDouble(double d) {
        log("double", d);
        hasher.putLong(8);
        hasher.putDouble(d);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putBoolean(boolean b) {
        log("boolean", b);
        hasher.putLong(1);
        hasher.putBoolean(b);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putString(CharSequence charSequence) {
        log("string", charSequence);
        hasher.putLong(charSequence.length());
        hasher.putString(charSequence, Charsets.UTF_8);
        return this;
    }

    @Override
    public TaskCacheKey build() {
        HashCode hashCode = hasher.hash();
        LOGGER.debug("Hash code generated: {}", hashCode);
        return new DefaultTaskCacheKey(hashCode);
    }

    private static class DefaultTaskCacheKey implements TaskCacheKey {
<commit_msg>Use putInt to add length to hash key<commit_after>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal.tasks.cache;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A builder for hash keys
 *
 * In order to avoid collisions we prepend the length of the next bytes to the underlying
 * hasher (see this <a href="http://crypto.stackexchange.com/a/10065">answer</a> on stackexchange.)
 */
public class DefaultTaskCacheKeyBuilder implements TaskCacheKeyBuilder {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTaskCacheKeyBuilder.class);
    private final Hasher hasher = Hashing.md5().newHasher();

    @Override
    public TaskCacheKeyBuilder putByte(byte b) {
        log("byte", b);
        hasher.putInt(1);
        hasher.putByte(b);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putBytes(byte[] bytes) {
        log("bytes", new ByteArrayToStringer(bytes));
        hasher.putInt(bytes.length);
        hasher.putBytes(bytes);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putBytes(byte[] bytes, int off, int len) {
        log("bytes", new ByteArrayToStringer(bytes, off, len));
        hasher.putInt(len);
        hasher.putBytes(bytes, off, len);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putInt(int i) {
        log("int", i);
        hasher.putInt(4);
        hasher.putInt(i);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putLong(long l) {
        log("long", l);
        hasher.putInt(8);
        hasher.putLong(l);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putDouble(double d) {
        log("double", d);
        hasher.putInt(8);
        hasher.putDouble(d);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putBoolean(boolean b) {
        log("boolean", b);
        hasher.putInt(1);
        hasher.putBoolean(b);
        return this;
    }

    @Override
    public TaskCacheKeyBuilder putString(CharSequence charSequence) {
        log("string", charSequence);
        hasher.putInt(charSequence.length());
        hasher.putString(charSequence, Charsets.UTF_8);
        return this;
    }

    @Override
    public TaskCacheKey build() {
        HashCode hashCode = hasher.hash();
        LOGGER.debug("Hash code generated: {}", hashCode);
        return new DefaultTaskCacheKey(hashCode);
    }

    private static class DefaultTaskCacheKey implements TaskCacheKey {

{'fraction_non_alphanumeric': 0.09970674486803519, 'fraction_numerical': 0.006791171477079796, 'mean_word_length': 2.93681652490887, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6102141', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1850, 'n_words': 533}