package com.boundary.sdk.event;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Common properties between RawEvent and Event
 * 
 * @author davidg
 *
 */
public class BaseEvent {
	
	final static int MAXIMUM_STRING_LENGTH = 255;
	
	private static Logger LOG = LoggerFactory.getLogger(BaseEvent.class);
	
	/**
	 * Default constructor for BaseEvent
	 */
	public BaseEvent() {
		
	}
	
	/**
	 * Helper method that truncates {@link String}
	 * to the maximum allowed by the Boundary Event API
	 * 
	 * @param str
	 * @return String
	 */
	public static String truncateToMaximumLength(String str) {
		int length = Math.min(str.length(),MAXIMUM_STRING_LENGTH);
		String truncStr = str.substring(0, length);
		if (length < str.length() ) {
			LOG.warn("String truncated from {} to {}", str,truncStr);
		}
		return truncStr;
	}
	
	/**
	 * Helper method that truncates an {@link ArrayList} of {@link String}
	 * to the maximum allowed by the Boundary Event API
	 * 
	 * @param array
	 * @return {@link ArrayList} of {@link String}
	 */
	public static ArrayList<String> truncateToMaximumLength(ArrayList<String> array) {
		ArrayList<String> truncatedArray = new ArrayList<String>(array.size());
		for (String s : array) {
			truncatedArray.add(truncateToMaximumLength(s));
		}
		return truncatedArray;
	}
	
	/**
	 * Helper method that truncates an {@link Object} if it is a {@link String}
	 * to the maximum allowed by the Boundary Event API
	 * @param obj
	 * @return {@link Object}
	 */
	public static Object truncateToMaximumLength(Object obj) {
		String s = "";
		Object truncatedObj = obj;
		if (obj instanceof String) {
			s = truncateToMaximumLength((String)obj);
			truncatedObj = s;
		}
		return truncatedObj;
	}
	
	/**
	 * Helper method that truncates a {@link LinkedHashMap} with keys of {@link String}
	 * and values of {@link Object} that are instances of {@link String}
	 * to the maximum allowed by the Boundary Event API
	 * 
	 * @param prop
	 * @return {@link LinkedHashMap} with key {@link String} and value {@link Object}
	 */
	public static LinkedHashMap<String,Object> truncateToMaximumLength(LinkedHashMap<String,Object> prop) {
		LinkedHashMap<String,Object> truncatedProp = new LinkedHashMap<String,Object>(prop.size());
		
		for (String key : prop.keySet()) {
		    truncatedProp.put(key, truncateToMaximumLength(prop.get(key)));
		}
		return truncatedProp;
	}
}

@@ -578,16 +578,43 @@
 aram str
+ %7B@link String%7D to truncate
 %0A%09 * @re
@@ -618,22 +618,30 @@
 @return 
+%7B@link 
 String
+%7D
 %0A%09 */%0A%09p
@@ -1077,16 +1077,69 @@
 am array
+ An %7B@link ArrayList%7D of %7B@link String%7Ds to truncate.
 %0A%09 * @re
@@ -1603,16 +1603,58 @@
 aram obj
+ %7B@link Object%7D instance that is a string.
 %0A%09 * @re
@@ -2123,32 +2123,32 @@
  Event API%0A%09 * %0A
-
 %09 * @param prop%0A
@@ -2146,16 +2146,78 @@
 ram prop
+ %7B@LinkHashMap%7D that contains the %7B@link String%7Ds to truncate.
 %0A%09 * @re

Remove errors from javadoc compiler caused by using JDK 8