<commit_before>package com.mindoo.domino.jna.internal;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import com.mindoo.domino.jna.NotesItem;
import com.mindoo.domino.jna.NotesTimeDate;
import com.mindoo.domino.jna.structs.NotesItemValueTableStruct;
import com.mindoo.domino.jna.structs.NotesNumberPairStruct;
import com.mindoo.domino.jna.structs.NotesRangeStruct;
import com.mindoo.domino.jna.structs.NotesTableItemStruct;
import com.mindoo.domino.jna.structs.NotesTimeDatePairStruct;
import com.mindoo.domino.jna.structs.NotesTimeDateStruct;
import com.mindoo.domino.jna.utils.NotesDateTimeUtils;
import com.mindoo.domino.jna.utils.NotesStringUtils;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;

public class NotesSearchKeyEncoder {

	/**
	 * Produces the keybuffer for NIFFindByKey
	 * 
	 * @param keys array of String, Double, Integer, Calendar, Date, Calendar[] (with two elements lower/upper), Date[] (with two elements lower/upper)
	 * @return buffer with encoded keys
	 * @throws Exception in case of errors
	 */
	public static Memory b32_encodeKeys(Object[] keys) throws Exception {
		return b64_encodeKeys(keys);
	}
	
	/**
	 * Produces the keybuffer for NIFFindByKey
	 * 
	 * @param keys array of String, Double, Integer, Calendar, Date, Calendar[] (with two elements lower/upper), Date[] (with two elements lower/upper)
	 * @return buffer with encoded keys
	 * @throws Exception in case of errors
	 */
	public static Memory b64_encodeKeys(Object[] keys) throws Exception {
		ByteArrayOutputStream metaDataByteOut = new ByteArrayOutputStream();

		ByteArrayOutputStream valueDataByteOut = new ByteArrayOutputStream();
		
		for (int i=0; i<keys.length; i++) {
			if (keys[i] == null) {
				throw new NullPointerException("Keys cannot be null. keys="+new ArrayList<Object>(Arrays.asList(keys)));
			}
		}
		
		//write placeholder for the total buffer size and the number of items, will be filled later
		metaDataByteOut.write(0);
		metaDataByteOut.write(0);
		
		metaDataByteOut.write(0);
		metaDataByteOut.write(0);
		

		for (int i=0; i<keys.length; i++) {
			Object currKey = keys[i];
			
			if (currKey instanceof String) {
				addStringKey(metaDataByteOut, valueDataByteOut, (String) currKey);
			}
			else if (currKey instanceof Double) {
				addNumberKey(metaDataByteOut, valueDataByteOut, ((Double) currKey).doubleValue());
			}
			else if (currKey instanceof Integer) {
				addNumberKey(metaDataByteOut, valueDataByteOut, ((Integer) currKey).doubleValue());
			}
			else if (currKey instanceof Date) {
				Calendar cal = Calendar.getInstance();
				cal.setTime((Date) currKey);
				addCalendarKey(metaDataByteOut, valueDataByteOut, cal);
			}
			else if (currKey instanceof Calendar) {
				addCalendarKey(metaDataByteOut, valueDataByteOut, (Calendar) currKey);
			}
			else if (currKey instanceof Date[]) {
				Date[] dateArr = (Date[]) currKey;
				Calendar[] calArr = new Calendar[dateArr.length];
				for (int j=0; j<dateArr.length; j++) {
					calArr[j] = Calendar.getInstance();
					calArr[j].setTime(dateArr[j]);
				}
				
				addCalendarRangeKey(metaDataByteOut, valueDataByteOut, calArr);
			}
			else if (currKey instanceof Calendar[]) {
				//date range
				addCalendarRangeKey(metaDataByteOut, valueDataByteOut, (Calendar[]) currKey);
			}
//			else if (currKey instanceof Interval) {
//				Interval interval = (Interval) currKey;
//				DateTime startDateTime = interval.getStart();
//				DateTime endDateTime = interval.getEnd();
//				
//				addCalendarRangeKey(metaDataByteOut, valueDataByteOut, new Calendar[] {startDateTime.toCalendar(Locale.getDefault()), endDateTime.toCalendar(Locale.getDefault())});
//			}
			else if (currKey instanceof double[]) {
				//looks like this does not work (the C API documentation says it does not work either)
				addNumberRangeKey(metaDataByteOut, valueDataByteOut, (double[]) currKey);
			}
			else if (currKey instanceof Double[]) {
				Double[] objArr = (Double[]) currKey;
				double[] doubleArr = new double[objArr.length];
				for (int j=0; j<objArr.length; j++) {
					if (objArr[j] != null) {
						doubleArr[j] = objArr[j].doubleValue();
					}
				}
				//looks like this does not work (the C API documentation says it does not work either)
				addNumberRangeKey(metaDataByteOut, valueDataByteOut, doubleArr);
			}
			else {
				throw new IllegalArgumentException("Unknown key type: "+currKey+", class="+(currKey==null ? "null" : currKey.getClass().getName()));
			}
		}		
		
		byte[] metaDataByteArr = metaDataByteOut.toByteArray();
		byte[] valueDataByteArr = valueDataByteOut.toByteArray();
		
		Memory mem = new Memory(metaDataByteArr.length + valueDataByteArr.length);
		
		int offset = 0;
		for (int i=0; i<metaDataByteArr.length; i++) {
			mem.setByte(offset, metaDataByteArr[i]);
			offset++;
		}
		<commit_msg>Added support for more lookup key types<commit_after>package com.mindoo.domino.jna.internal;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import com.mindoo.domino.jna.NotesItem;
import com.mindoo.domino.jna.NotesTimeDate;
import com.mindoo.domino.jna.structs.NotesItemValueTableStruct;
import com.mindoo.domino.jna.structs.NotesNumberPairStruct;
import com.mindoo.domino.jna.structs.NotesRangeStruct;
import com.mindoo.domino.jna.structs.NotesTableItemStruct;
import com.mindoo.domino.jna.structs.NotesTimeDatePairStruct;
import com.mindoo.domino.jna.structs.NotesTimeDateStruct;
import com.mindoo.domino.jna.utils.NotesDateTimeUtils;
import com.mindoo.domino.jna.utils.NotesStringUtils;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;

public class NotesSearchKeyEncoder {

	/**
	 * Produces the keybuffer for NIFFindByKey
	 * 
	 * @param keys array of String, Double, Integer, Calendar, Date, Calendar[] (with two elements lower/upper), Date[] (with two elements lower/upper)
	 * @return buffer with encoded keys
	 * @throws Exception in case of errors
	 */
	public static Memory b32_encodeKeys(Object[] keys) throws Exception {
		return b64_encodeKeys(keys);
	}
	
	/**
	 * Produces the keybuffer for NIFFindByKey
	 * 
	 * @param keys array of String, Double, Integer, Calendar, Date, Calendar[] (with two elements lower/upper), Date[] (with two elements lower/upper)
	 * @return buffer with encoded keys
	 * @throws Exception in case of errors
	 */
	public static Memory b64_encodeKeys(Object[] keys) throws Exception {
		ByteArrayOutputStream metaDataByteOut = new ByteArrayOutputStream();

		ByteArrayOutputStream valueDataByteOut = new ByteArrayOutputStream();
		
		for (int i=0; i<keys.length; i++) {
			if (keys[i] == null) {
				throw new NullPointerException("Keys cannot be null. keys="+new ArrayList<Object>(Arrays.asList(keys)));
			}
		}
		
		//write placeholder for the total buffer size and the number of items, will be filled later
		metaDataByteOut.write(0);
		metaDataByteOut.write(0);
		
		metaDataByteOut.write(0);
		metaDataByteOut.write(0);
		

		for (int i=0; i<keys.length; i++) {
			Object currKey = keys[i];
			
			if (currKey instanceof String) {
				addStringKey(metaDataByteOut, valueDataByteOut, (String) currKey);
			}
			else if (currKey instanceof Double) {
				addNumberKey(metaDataByteOut, valueDataByteOut, ((Double) currKey).doubleValue());
			}
			else if (currKey instanceof Float) {
				addNumberKey(metaDataByteOut, valueDataByteOut, ((Float) currKey).doubleValue());
			}
			else if (currKey instanceof Integer) {
				addNumberKey(metaDataByteOut, valueDataByteOut, ((Integer) currKey).doubleValue());
			}
			else if (currKey instanceof Long) {
				addNumberKey(metaDataByteOut, valueDataByteOut, ((Long) currKey).doubleValue());
			}
			else if (currKey instanceof Date) {
				Calendar cal = Calendar.getInstance();
				cal.setTime((Date) currKey);
				addCalendarKey(metaDataByteOut, valueDataByteOut, cal);
			}
			else if (currKey instanceof Calendar) {
				addCalendarKey(metaDataByteOut, valueDataByteOut, (Calendar) currKey);
			}
			else if (currKey instanceof Date[]) {
				Date[] dateArr = (Date[]) currKey;
				Calendar[] calArr = new Calendar[dateArr.length];
				for (int j=0; j<dateArr.length; j++) {
					calArr[j] = Calendar.getInstance();
					calArr[j].setTime(dateArr[j]);
				}
				
				addCalendarRangeKey(metaDataByteOut, valueDataByteOut, calArr);
			}
			else if (currKey instanceof Calendar[]) {
				//date range
				addCalendarRangeKey(metaDataByteOut, valueDataByteOut, (Calendar[]) currKey);
			}
			else if (currKey instanceof double[]) {
				//looks like this does not work (the C API documentation says it does not work either)
				addNumberRangeKey(metaDataByteOut, valueDataByteOut, (double[]) currKey);
			}
			else if (currKey instanceof Double[]) {
				Double[] objArr = (Double[]) currKey;
				double[] doubleArr = new double[objArr.length];
				for (int j=0; j<objArr.length; j++) {
					if (objArr[j] != null) {
						doubleArr[j] = objArr[j].doubleValue();
					}
				}
				//looks like this does not work (the C API documentation says it does not work either)
				addNumberRangeKey(metaDataByteOut, valueDataByteOut, doubleArr);
			}
			else {
				throw new IllegalArgumentException("Unknown key type: "+currKey+", class="+(currKey==null ? "null" : currKey.getClass().getName()));
			}
		}		
		
		byte[] metaDataByteArr = metaDataByteOut.toByteArray();
		byte[] valueDataByteArr = valueDataByteOut.toByteArray();
		
		Memory mem = new Memory(metaDataByteArr.length + valueDataByteArr.length);
		
		int offset = 0;
		for (int i=0; i<metaDataByteArr.length; i++) {
			mem.setByte(offset, metaDataByteArr[i]);
			offset++;
		}
		
{'fraction_non_alphanumeric': 0.1179959100204499, 'fraction_numerical': 0.0032719836400817996, 'mean_word_length': 4.356516976998905, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '422656', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3020, 'n_words': 769}