<commit_before>/*******************************************************************************
* Copyright (c) 2009 Centrum Wiskunde en Informatica (CWI)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    <NAME> - interfaces and implementation
*    <NAME> - rational support
*    <NAME> - added PI & E constants
*******************************************************************************/
package org.eclipse.imp.pdb.facts.impl.fast;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import org.eclipse.imp.pdb.facts.IBool;
import org.eclipse.imp.pdb.facts.IConstructor;
import org.eclipse.imp.pdb.facts.IDateTime;
import org.eclipse.imp.pdb.facts.IInteger;
import org.eclipse.imp.pdb.facts.IList;
import org.eclipse.imp.pdb.facts.IListWriter;
import org.eclipse.imp.pdb.facts.IMap;
import org.eclipse.imp.pdb.facts.IMapWriter;
import org.eclipse.imp.pdb.facts.INode;
import org.eclipse.imp.pdb.facts.IRational;
import org.eclipse.imp.pdb.facts.IReal;
import org.eclipse.imp.pdb.facts.IRelation;
import org.eclipse.imp.pdb.facts.IRelationWriter;
import org.eclipse.imp.pdb.facts.ISet;
import org.eclipse.imp.pdb.facts.ISetWriter;
import org.eclipse.imp.pdb.facts.ISourceLocation;
import org.eclipse.imp.pdb.facts.IString;
import org.eclipse.imp.pdb.facts.ITuple;
import org.eclipse.imp.pdb.facts.IValue;
import org.eclipse.imp.pdb.facts.IValueFactory;
import org.eclipse.imp.pdb.facts.exceptions.FactParseError;
import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException;
import org.eclipse.imp.pdb.facts.exceptions.UnexpectedElementTypeException;
import org.eclipse.imp.pdb.facts.type.Type;
import org.eclipse.imp.pdb.facts.type.TypeFactory;
import org.eclipse.imp.pdb.facts.util.ShareableHashMap;

/**
 * Implementation of IValueFactory.
 * 
 * @author <NAME>
 */
public class ValueFactory implements IValueFactory{
	private final static TypeFactory tf = TypeFactory.getInstance();
	
	private final static Type EMPTY_TUPLE_TYPE = TypeFactory.getInstance().tupleEmpty();
	
	private final static String INTEGER_MAX_STRING = "2147483647";
	private final static String NEGATIVE_INTEGER_MAX_STRING = "-2147483648";
	
	protected ValueFactory(){
		super();
	}

	private static class InstanceKeeper{
		public final static ValueFactory instance = new ValueFactory();
	}
	
	public static ValueFactory getInstance(){
		return InstanceKeeper.instance;
	}
	
	public IBool bool(boolean value){
		return BoolValue.getBoolValue(value);
	}
	
	public IInteger integer(int value){
		return new IntegerValue(value);
	}
	
	public IInteger integer(long value){
		if(((value & 0x000000007fffffffL) == value) || ((value & 0xffffffff80000000L) == 0xffffffff80000000L)){
			return integer((int) value);
		}else{
			byte[] valueData = new byte[8];
			valueData[0] = (byte) ((value >>> 56) & 0xff);
			valueData[1] = (byte) ((value >>> 48) & 0xff);
			valueData[2] = (byte) ((value >>> 40) & 0xff);
			valueData[3] = (byte) ((value >>> 32) & 0xff);
			valueData[4] = (byte) ((value >>> 24) & 0xff);
			valueData[5] = (byte) ((value >>> 16) & 0xff);
			valueData[6] = (byte) ((value >>> 8) & 0xff);
			valueData[7] = (byte) (value & 0xff);
			return integer(valueData);
		}
	}
	
	public IInteger integer(String integerValue){
		if(integerValue.startsWith("-")){
			if(integerValue.length() < 11 || (integerValue.length() == 11 && integerValue.compareTo(NEGATIVE_INTEGER_MAX_STRING) <= 0)){
				return new IntegerValue(Integer.parseInt(integerValue));
			}
			return new BigIntegerValue(new BigInteger(integerValue));
		}
		
		if(integerValue.length() < 10 || (integerValue.length() == 10 && integerValue.compareTo(INTEGER_MAX_STRING) <= 0)){
			return new IntegerValue(Integer.parseInt(integerValue));
		}
		return new BigIntegerValue(new BigInteger(integerValue));
	}
	
	public IInteger integer(byte[] integerData){
		if(integerData.length <= 4){
			int value = 0;
			for(int i = integerData.length - 1, j = 0; i >= 0; i--, j++){
				value |= ((integerData[i] & 0xff) << (j * 8));
			}
			
			return new IntegerValue(value);
		}
		return new BigIntegerValue(new BigInteger(integerData));
	}
	
	public IInteger integer(BigInteger value){
		if(value.bitLength() > 31){
			return new BigIntegerValue(value);
		}
		return new IntegerValue(value.intValue());
	}
	
	public IRational rational(int a, int b) {
		return rational(integer(a), integer(b));
	}

	public IRational rational(long a, long b) {
		return rational(integer(a), integer(b));
	}

	public IRational rational(IInteger a, IInteger b) {
		return new RationalValue(a, b);
	}

	public IRational rational(String rat) throws NumberFormatException {
		if(rat.contains("r")) {
			String[] parts = rat.split("r");
			if (parts.length == 2) {
				return rational(integer(parts[0]), integer(parts[1]));
			}
			if (parts.length == 1) {
				return rational(integer(parts[0]), integer(1));
			}
			throw new NumberFormatException(rat);
		}
		else {
			return rational(integer(rat), integer(1));
		}
	}

	public IReal real(double value){
		return new BigDecimalValue(BigDecimal.valueOf(value));
	}
	
	public IReal real(String doubleValue){
		return new BigDecimalValue(new BigDecimal(doubleValue));
	}
	
	public IReal real(BigDecimal value){
		return new BigDecimalValue(value);
	}
	
	public IReal pi(int precision) {
		return BigDecimalValue.pi(precision);
	}
	
	public IReal e(int precision) {
		return BigDecimalValue.e(precision);
	}
	
	public IString string(String value){
		return new StringValue(value);
	}
	
	public ISourceLocation sourceLocation(URI uri, int offset, int length, int beginLine, int endLine, int beginCol, int endCol) {
		if (offset < Character.MAX_VALUE 
				&& length < Character.MAX_VALUE 
				&& beginLine < Byte.MAX_VALUE 
				&& endLine < Byte.MAX_VALUE 
				&& beginCol < Byte.MAX_VALUE 
				&& endCol < Byte.MAX_VALUE) {
			return new SourceLocationValues.Smallest(uri, (char) offset, (char) length, (byte) beginLine, (byte) endLine, (byte) beginCol, (byte) endCol);
		}
		else if (offset < Character.MAX_VALUE 
				&& length < Character.MAX_VALUE 
				&& beginLine < Character.MAX_VALUE 
				&& endLine < Character.MAX_VALUE 
				&& beginCol < Character.MAX_VALUE 
				&& endCol < Character.MAX_VALUE) {
			return new SourceLocationValues.MediumSized(uri, (char) offset, (char) length, (char) beginLine, (char) endLine, (char) beginCol, (char) endCol);
		}
		else if (beginLine < Character.MAX_VALUE 
				&& endLine < Character.MAX_VALUE 
				&& beginCol < Byte.MAX_VALUE 
				&& endCol < Byte.MAX_VALUE) {
			return new SourceLocationValues.ShortColumnsMediumLines(uri, offset, length, (char) beginLine, (char) endLine, (byte) beginCol, (byte) endCol);
		} 
		else if (beginCol < Byte.MAX_VALUE 
				&& endCol < Byte.MAX_VALUE) {
			return new SourceLocationValues.ShortColumns(uri,  offset, length, beginLine, endLine, (byte) beginCol, (byte) endCol);
		}
		
		return new SourceLocationValues.Largest(uri, offset, length, beginLine, endLine, beginCol, endCol);
	}
	
	public ISourceLocation sourceLocation(String path, int offset, int length, int beginLine, int endLine, int beginCol, int endCol){
    	try{
			return sourceLocation(new URI("file://" + path), offset, length, beginLine, endLine, beginCol, endCol);
		}catch(URISyntaxException e){
			throw new FactParseError("Illegal path syntax: " + path, e);
		}
    }
	
	public ISourceLocation sourceLocation(URI uri){
		return new SourceLocationValues.NoPositions(uri); 
	}
	
	public ISourceLocation sourceLocation(String path){
		return sourceLocation(path, -1, -1, -1, -1, -1, -1);
	}<commit_msg>avoid use of -1, instead make a source location without ints<commit_after>/*******************************************************************************
* Copyright (c) 2009 Centrum Wiskunde en Informatica (CWI)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    <NAME> - interfaces and implementation
*    <NAME> - rational support
*    <NAME> - added PI & E constants
*******************************************************************************/
package org.eclipse.imp.pdb.facts.impl.fast;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import org.eclipse.imp.pdb.facts.IBool;
import org.eclipse.imp.pdb.facts.IConstructor;
import org.eclipse.imp.pdb.facts.IDateTime;
import org.eclipse.imp.pdb.facts.IInteger;
import org.eclipse.imp.pdb.facts.IList;
import org.eclipse.imp.pdb.facts.IListWriter;
import org.eclipse.imp.pdb.facts.IMap;
import org.eclipse.imp.pdb.facts.IMapWriter;
import org.eclipse.imp.pdb.facts.INode;
import org.eclipse.imp.pdb.facts.IRational;
import org.eclipse.imp.pdb.facts.IReal;
import org.eclipse.imp.pdb.facts.IRelation;
import org.eclipse.imp.pdb.facts.IRelationWriter;
import org.eclipse.imp.pdb.facts.ISet;
import org.eclipse.imp.pdb.facts.ISetWriter;
import org.eclipse.imp.pdb.facts.ISourceLocation;
import org.eclipse.imp.pdb.facts.IString;
import org.eclipse.imp.pdb.facts.ITuple;
import org.eclipse.imp.pdb.facts.IValue;
import org.eclipse.imp.pdb.facts.IValueFactory;
import org.eclipse.imp.pdb.facts.exceptions.FactParseError;
import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException;
import org.eclipse.imp.pdb.facts.exceptions.UnexpectedElementTypeException;
import org.eclipse.imp.pdb.facts.type.Type;
import org.eclipse.imp.pdb.facts.type.TypeFactory;
import org.eclipse.imp.pdb.facts.util.ShareableHashMap;

/**
 * Implementation of IValueFactory.
 * 
 * @author <NAME>
 */
public class ValueFactory implements IValueFactory{
	private final static TypeFactory tf = TypeFactory.getInstance();
	
	private final static Type EMPTY_TUPLE_TYPE = TypeFactory.getInstance().tupleEmpty();
	
	private final static String INTEGER_MAX_STRING = "2147483647";
	private final static String NEGATIVE_INTEGER_MAX_STRING = "-2147483648";
	
	protected ValueFactory(){
		super();
	}

	private static class InstanceKeeper{
		public final static ValueFactory instance = new ValueFactory();
	}
	
	public static ValueFactory getInstance(){
		return InstanceKeeper.instance;
	}
	
	public IBool bool(boolean value){
		return BoolValue.getBoolValue(value);
	}
	
	public IInteger integer(int value){
		return new IntegerValue(value);
	}
	
	public IInteger integer(long value){
		if(((value & 0x000000007fffffffL) == value) || ((value & 0xffffffff80000000L) == 0xffffffff80000000L)){
			return integer((int) value);
		}else{
			byte[] valueData = new byte[8];
			valueData[0] = (byte) ((value >>> 56) & 0xff);
			valueData[1] = (byte) ((value >>> 48) & 0xff);
			valueData[2] = (byte) ((value >>> 40) & 0xff);
			valueData[3] = (byte) ((value >>> 32) & 0xff);
			valueData[4] = (byte) ((value >>> 24) & 0xff);
			valueData[5] = (byte) ((value >>> 16) & 0xff);
			valueData[6] = (byte) ((value >>> 8) & 0xff);
			valueData[7] = (byte) (value & 0xff);
			return integer(valueData);
		}
	}
	
	public IInteger integer(String integerValue){
		if(integerValue.startsWith("-")){
			if(integerValue.length() < 11 || (integerValue.length() == 11 && integerValue.compareTo(NEGATIVE_INTEGER_MAX_STRING) <= 0)){
				return new IntegerValue(Integer.parseInt(integerValue));
			}
			return new BigIntegerValue(new BigInteger(integerValue));
		}
		
		if(integerValue.length() < 10 || (integerValue.length() == 10 && integerValue.compareTo(INTEGER_MAX_STRING) <= 0)){
			return new IntegerValue(Integer.parseInt(integerValue));
		}
		return new BigIntegerValue(new BigInteger(integerValue));
	}
	
	public IInteger integer(byte[] integerData){
		if(integerData.length <= 4){
			int value = 0;
			for(int i = integerData.length - 1, j = 0; i >= 0; i--, j++){
				value |= ((integerData[i] & 0xff) << (j * 8));
			}
			
			return new IntegerValue(value);
		}
		return new BigIntegerValue(new BigInteger(integerData));
	}
	
	public IInteger integer(BigInteger value){
		if(value.bitLength() > 31){
			return new BigIntegerValue(value);
		}
		return new IntegerValue(value.intValue());
	}
	
	public IRational rational(int a, int b) {
		return rational(integer(a), integer(b));
	}

	public IRational rational(long a, long b) {
		return rational(integer(a), integer(b));
	}

	public IRational rational(IInteger a, IInteger b) {
		return new RationalValue(a, b);
	}

	public IRational rational(String rat) throws NumberFormatException {
		if(rat.contains("r")) {
			String[] parts = rat.split("r");
			if (parts.length == 2) {
				return rational(integer(parts[0]), integer(parts[1]));
			}
			if (parts.length == 1) {
				return rational(integer(parts[0]), integer(1));
			}
			throw new NumberFormatException(rat);
		}
		else {
			return rational(integer(rat), integer(1));
		}
	}

	public IReal real(double value){
		return new BigDecimalValue(BigDecimal.valueOf(value));
	}
	
	public IReal real(String doubleValue){
		return new BigDecimalValue(new BigDecimal(doubleValue));
	}
	
	public IReal real(BigDecimal value){
		return new BigDecimalValue(value);
	}
	
	public IReal pi(int precision) {
		return BigDecimalValue.pi(precision);
	}
	
	public IReal e(int precision) {
		return BigDecimalValue.e(precision);
	}
	
	public IString string(String value){
		return new StringValue(value);
	}
	
	public ISourceLocation sourceLocation(URI uri, int offset, int length, int beginLine, int endLine, int beginCol, int endCol) {
		if (offset < Character.MAX_VALUE 
				&& length < Character.MAX_VALUE 
				&& beginLine < Byte.MAX_VALUE 
				&& endLine < Byte.MAX_VALUE 
				&& beginCol < Byte.MAX_VALUE 
				&& endCol < Byte.MAX_VALUE) {
			return new SourceLocationValues.Smallest(uri, (char) offset, (char) length, (byte) beginLine, (byte) endLine, (byte) beginCol, (byte) endCol);
		}
		else if (offset < Character.MAX_VALUE 
				&& length < Character.MAX_VALUE 
				&& beginLine < Character.MAX_VALUE 
				&& endLine < Character.MAX_VALUE 
				&& beginCol < Character.MAX_VALUE 
				&& endCol < Character.MAX_VALUE) {
			return new SourceLocationValues.MediumSized(uri, (char) offset, (char) length, (char) beginLine, (char) endLine, (char) beginCol, (char) endCol);
		}
		else if (beginLine < Character.MAX_VALUE 
				&& endLine < Character.MAX_VALUE 
				&& beginCol < Byte.MAX_VALUE 
				&& endCol < Byte.MAX_VALUE) {
			return new SourceLocationValues.ShortColumnsMediumLines(uri, offset, length, (char) beginLine, (char) endLine, (byte) beginCol, (byte) endCol);
		} 
		else if (beginCol < Byte.MAX_VALUE 
				&& endCol < Byte.MAX_VALUE) {
			return new SourceLocationValues.ShortColumns(uri,  offset, length, beginLine, endLine, (byte) beginCol, (byte) endCol);
		}
		
		return new SourceLocationValues.Largest(uri, offset, length, beginLine, endLine, beginCol, endCol);
	}
	
	public ISourceLocation sourceLocation(String path, int offset, int length, int beginLine, int endLine, int beginCol, int endCol){
    	try{
			return sourceLocation(new URI("file://" + path), offset, length, beginLine, endLine, beginCol, endCol);
		}catch(URISyntaxException e){
			throw new FactParseError("Illegal path syntax: " + path, e);
		}
    }
	
	public ISourceLocation sourceLocation(URI uri){
		return new SourceLocationValues.NoPositions(uri); 
	}
	
	public ISourceLocation sourceLocation(String path){
		try {
			return sourceLocation(new URI("file://" + path));
		} catch (URISyntaxException e) {
			throw new FactParseError("Illegal path syntax: " + path, e);
		}
	}
{'fraction_non_alphanumeric': 0.1476510067114094, 'fraction_numerical': 0.014625807268583006, 'mean_word_length': 4.589171974522293, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6957997', 'n_tokens_mistral': 5692, 'n_tokens_neox': 4988, 'n_words': 1199}