<commit_before>/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 <NAME>, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.engine;

import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.commons.codec.digest.DigestUtils;
import org.cinchapi.common.annotate.DoNotInvoke;
import org.cinchapi.common.annotate.PackagePrivate;
import org.cinchapi.common.cache.ReferenceCache;
import org.cinchapi.common.io.Byteable;
import org.cinchapi.common.multithread.Lock;
import org.cinchapi.common.multithread.Lockable;
import org.cinchapi.common.multithread.Lockables;

import com.google.common.base.Preconditions;

/**
 * A {@code Representation} is a lightweight delegate for the purpose of
 * providing {@link Lockable} functionality. This is useful in cases when the
 * resource that needs to be locked isn't represented by a single Object, but is
 * still a distinct concept that is possibly made up of multiple Objects.
 * <p>
 * <em><strong>Note:</strong> It is not possible to reconstruct Objects back 
 * from a Representation.</em>
 * </p>
 * 
 * @author jnelson
 */
@PackagePrivate
class Representation implements Lockable, Byteable {

	/**
	 * Return a {@code Representation} encoded in {@code buffer}.
	 * 
	 * @param buffer
	 * @return the Representation
	 */
	public static Representation fromByteBuffer(ByteBuffer buffer) {
		buffer.rewind();
		Representation representation = cache.get(buffer);
		if(representation == null) {
			representation = new Representation(buffer);
			cache.put(representation, buffer);
		}
		return representation;
	}

	/**
	 * Return a {@code Representation} for the {@code objects}.
	 * 
	 * @param objects
	 * @return the Representation
	 */
	public static Representation forObjects(Object... objects) {
		ByteBuffer buffer = ByteBuffer.wrap(DigestUtils.md5(Arrays
				.toString(objects)));
		buffer.rewind();
		Representation representation = cache.get(buffer);
		if(representation == null) {
			representation = new Representation(buffer);
			cache.put(representation, buffer);
		}
		return representation;
	}

	private static final ReferenceCache<Representation> cache = new ReferenceCache<Representation>();

	/**
	 * The representation is identified by an md5 hash
	 */
	private final ByteBuffer bytes;

	/**
	 * Construct a new instance.
	 * 
	 * @param bytes
	 */
	@DoNotInvoke
	public Representation(ByteBuffer bytes) {
		Preconditions.checkArgument(bytes.capacity() == 16);
		this.bytes = bytes;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Representation) {
			Representation other = (Representation) obj;
			return getBytes().equals(other.getBytes());
		}
		return false;
	}

	@Override
	public ByteBuffer getBytes() {
		bytes.rewind();
		return bytes;

	}

	@Override
	public int hashCode() {
		return getBytes().hashCode();
	}

	@Override
	public Lock readLock() {
		return Lockables.readLock(this);
	}

	@Override
	public int size() {
		return getBytes().capacity();
	}

	@Override
	public Lock writeLock() {
		return Lockables.writeLock(this);
	}

}<commit_msg>Added a toString() method to Representation.java<commit_after>/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 <NAME>, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.engine;

import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.cinchapi.common.annotate.DoNotInvoke;
import org.cinchapi.common.annotate.PackagePrivate;
import org.cinchapi.common.cache.ReferenceCache;
import org.cinchapi.common.io.ByteBuffers;
import org.cinchapi.common.io.Byteable;
import org.cinchapi.common.multithread.Lock;
import org.cinchapi.common.multithread.Lockable;
import org.cinchapi.common.multithread.Lockables;

import com.google.common.base.Preconditions;

/**
 * A {@code Representation} is a lightweight delegate for the purpose of
 * providing {@link Lockable} functionality. This is useful in cases when the
 * resource that needs to be locked isn't represented by a single Object, but is
 * still a distinct concept that is possibly made up of multiple Objects.
 * <p>
 * <em><strong>Note:</strong> It is not possible to reconstruct Objects back 
 * from a Representation.</em>
 * </p>
 * 
 * @author jnelson
 */
@PackagePrivate
class Representation implements Lockable, Byteable {

	/**
	 * Return a {@code Representation} encoded in {@code buffer}.
	 * 
	 * @param buffer
	 * @return the Representation
	 */
	public static Representation fromByteBuffer(ByteBuffer buffer) {
		buffer.rewind();
		Representation representation = cache.get(buffer);
		if(representation == null) {
			representation = new Representation(buffer);
			cache.put(representation, buffer);
		}
		return representation;
	}

	/**
	 * Return a {@code Representation} for the {@code objects}.
	 * 
	 * @param objects
	 * @return the Representation
	 */
	public static Representation forObjects(Object... objects) {
		ByteBuffer buffer = ByteBuffer.wrap(DigestUtils.md5(Arrays
				.toString(objects)));
		buffer.rewind();
		Representation representation = cache.get(buffer);
		if(representation == null) {
			representation = new Representation(buffer);
			cache.put(representation, buffer);
		}
		return representation;
	}

	private static final ReferenceCache<Representation> cache = new ReferenceCache<Representation>();

	/**
	 * The representation is identified by an md5 hash
	 */
	private final ByteBuffer bytes;

	/**
	 * Construct a new instance.
	 * 
	 * @param bytes
	 */
	@DoNotInvoke
	public Representation(ByteBuffer bytes) {
		Preconditions.checkArgument(bytes.capacity() == 16);
		this.bytes = bytes;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Representation) {
			Representation other = (Representation) obj;
			return getBytes().equals(other.getBytes());
		}
		return false;
	}

	@Override
	public ByteBuffer getBytes() {
		bytes.rewind();
		return bytes;

	}

	@Override
	public int hashCode() {
		return getBytes().hashCode();
	}

	@Override
	public Lock readLock() {
		return Lockables.readLock(this);
	}

	@Override
	public int size() {
		return getBytes().capacity();
	}

	@Override
	public String toString() {
		return Hex.encodeHexString(ByteBuffers.toByteArray(getBytes()));
	}

	@Override
	public Lock writeLock() {
		return Lockables.writeLock(this);
	}

}
{'fraction_non_alphanumeric': 0.0957621166003278, 'fraction_numerical': 0.0018730976352142356, 'mean_word_length': 4.326059850374065, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2565862', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2318, 'n_words': 843}