Project

General

Profile

1
/**
2
 * 
3
 */
4
package eu.dnetlib.data.collective.transformation.engine.functions;
5

    
6
import java.util.Arrays;
7
import java.util.Collection;
8
import java.util.HashMap;
9
import java.util.LinkedList;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Queue;
13

    
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.springframework.util.StringUtils;
17

    
18
/**
19
 * @author js
20
 *
21
 */
22
public class Split extends AbstractTransformationFunction {
23

    
24
	public static final Log log = LogFactory.getLog(Split.class);
25
	public static final String paramInputExpr = "inputExpr";
26
	public static final String paramRegExpr = "regExpr";
27
	public static final String paramElementName = "elementName";
28
	
29
	private Map<String, Queue<String>> queueMap = new HashMap<String, Queue<String>>();
30
	
31
	/* (non-Javadoc)
32
	 * @see eu.dnetlib.data.collective.transformation.engine.functions.AbstractTransformationFunction#execute()
33
	 */
34
	@Override
35
	String execute() throws ProcessingException {
36
		// TODO Auto-generated method stub
37
		return null;
38
	}
39

    
40
	/**
41
	 * split a given list of values using a delimiter as regularExpression
42
	 * @param aInputValue
43
	 * @param aRegExpr
44
	 * @return the collection of all values splitted
45
	 */
46
	public Collection<String> executeAllValues(List<String> aInputValues, String aRegExpr) throws ProcessingException{
47
		Collection<String> result = new LinkedList<String>();
48
		for (String value: aInputValues){
49
			String[] values = StringUtils.tokenizeToStringArray(value, aRegExpr, true, true);				
50
			result.addAll(Arrays.asList(values));
51
		}			
52
		return result;
53
	}
54
	
55
	/**
56
	 * split a given list of values stored in an internal queue and return the element from the head of the queue (recursive)
57
	 * @param aInputValues
58
	 * @param aRegExpr
59
	 * @param aCallId
60
	 * @return
61
	 * @throws ProcessingException
62
	 */
63
	public String executeSingleValue(List<String> aInputValues, String aRegExpr, String aCallId) throws ProcessingException{
64
		if (!queueMap.containsKey(aCallId)){
65
			Queue<String> queue = new LinkedList<String>();
66
			queueMap.put(aCallId, queue);
67
			for (String value: aInputValues){
68
				String[] values = StringUtils.tokenizeToStringArray(value, aRegExpr, true, true);				
69
				queue.addAll(Arrays.asList(values));
70
			}			
71
		}
72
		String result = queueMap.get(aCallId).poll();
73
		if (result == null){
74
			queueMap.remove(aCallId);
75
		}
76
		return result;
77
	}
78
	
79
	public String executeSingleValue(String aCallId) throws ProcessingException{
80
		String result = queueMap.get(aCallId).poll();
81
		if (result == null){
82
			queueMap.remove(aCallId);
83
		}
84
		return result;
85
	}
86
}
(16-16/17)