/*
* Created on Feb 10, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package de.desy.tine.stringUtils;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import de.desy.tine.types.NAME16;
import de.desy.tine.types.NAME32;
import de.desy.tine.types.NAME64;
/**
* \internal
*
* A set of utilities for String collections.
*
* @author jwlg
*/
public class StringToName
{
/**
* Disable constructor; pure utility class.
*/
private StringToName()
{
}
/**
* Converts a collection which contains strings to a Name16 array.
*
* @param inputSet
* Set of Strings
* @return Name16 array or null if the collection is empty.
*/
public static NAME16[] stringSetToName16(Collection<String> inputSet)
{
NAME16[] rv = null;
int len = inputSet.size();
Iterator<String> it;
if (len > 0)
{
rv = new NAME16[len];
it = inputSet.iterator();
for (int i = 0; (i < len) && it.hasNext(); i++)
{
rv[i] = new NAME16((String) it.next());
}
}
return rv;
}
/**
* Converts a collection which contains strings to a Name32 array.
*
* @param inputSet
* Set of Strings
* @return Name32 array or null if the collection is empty.
*/
public static NAME32[] stringSetToName32(Collection<String> inputSet)
{
NAME32[] rv = null;
int len = inputSet.size();
Iterator<String> it;
if (len > 0)
{
rv = new NAME32[len];
it = inputSet.iterator();
for (int i = 0; (i < len) && it.hasNext(); i++)
{
rv[i] = new NAME32((String) it.next());
}
}
return rv;
}
/**
* Converts a collection which contains strings to a Name64 array.
*
* @param inputSet
* Set of Strings
* @return Name64 array or null if the collection is empty.
*/
public static NAME64[] stringSetToName64(Collection<String> inputSet)
{
NAME64[] rv = null;
int len = inputSet.size();
Iterator<String> it;
if (len > 0)
{
rv = new NAME64[len];
it = inputSet.iterator();
for (int i = 0; (i < len) && it.hasNext(); i++)
{
rv[i] = new NAME64((String) it.next());
}
}
return rv;
}
/**
* Converts a string array which to a Name16 array.
*
* @param inputArray
* Array of Strings
* @return Name16 array or null if the array is empty.
*/
public static NAME16[] stringArrayToName16(String[] inputArray)
{
NAME16[] rv = null;
if (inputArray == null) return null;
int len = inputArray.length;
if (len > 0)
{
rv = new NAME16[len];
for (int i = 0; i < len; i++)
{
rv[i] = new NAME16((String) inputArray[i]);
}
}
return rv;
}
/**
* Converts a string array which to a Name64 array.
*
* @param inputArray
* Array of Strings
* @return Name64 array or null if the array is empty.
*/
public static NAME64[] stringArrayToName64(String[] inputArray)
{
NAME64[] rv = null;
int len = inputArray.length;
if (len > 0)
{
rv = new NAME64[len];
for (int i = 0; i < len; i++)
{
rv[i] = new NAME64((String) inputArray[i]);
}
}
return rv;
}
/**
* Converts a string array which to a Name32 array.
*
* @param inputArray
* Array of Strings
* @return Name32 array or null if the array is empty.
*/
public static NAME32[] stringArrayToName32(String[] inputArray)
{
NAME32[] rv = null;
int len = inputArray.length;
if (len > 0)
{
rv = new NAME32[len];
for (int i = 0; i < len; i++)
{
rv[i] = new NAME32((String) inputArray[i]);
}
}
return rv;
}
/**
* Filters a list of strings from a string collection.
*
* @param inputSet
* Collection containing strings.
* @param pattern
* Matching pattern
* @return List of matching strings.
* @see WildcardMatch
*/
public LinkedList<String> matchStringsInSet(Collection<String> inputSet, String pattern)
{
LinkedList<String> rv = new LinkedList<String>();
Iterator<String> it = inputSet.iterator();
String element = "";
WildcardMatch wc = new WildcardMatch(pattern);
for (it = inputSet.iterator(); it.hasNext(); element = (String) it.next())
{
if (wc.matches(element))
{
rv.add(element);
}
}
return rv;
}
public static String[] matchStringsInArray(String[] inputArray, String pattern)
{
LinkedList<String> rv = new LinkedList<String>();
WildcardMatch wc = new WildcardMatch(pattern);
String[] outputArray;
for (int i=0; i<inputArray.length; i++)
{
if (wc.matches(inputArray[i]))
{
rv.add(inputArray[i]);
}
}
int siz = rv.size();
if (siz == 0) return null;
outputArray = new String[siz];
return (String[])rv.toArray(outputArray);
}
public static int[] getContiguousEndpoints(NAME32[] inputArray, String pattern)
{ /* called in the case of local multi-channel arrays */
if (inputArray == null || inputArray.length == 0) return null;
String[] strArray = new String[inputArray.length];
for (int i=0; i<inputArray.length; i++) strArray[i] = inputArray[i].name;
return getContiguousEndpoints(strArray, pattern);
}
public static int[] getContiguousEndpoints(String[] inputArray, String pattern)
{ /* called in the case of local multi-channel arrays */
int i, n;
int endpoints[] = new int[2];
boolean inSequence = false, finSequence = false;
if (pattern.compareTo("*") == 0)
{ /* channel array with '*' is continguous */
endpoints[0] = 0;
endpoints[1] = inputArray.length - 1;
return endpoints;
}
WildcardMatch wc = new WildcardMatch(pattern);
for (i=0,n=0; i<inputArray.length; i++)
{
if (!wc.matches(inputArray[i]))
{
if (inSequence) finSequence = true;
continue;
}
if (finSequence) return null; /* found another match -> not contiguous ! */
if (!inSequence) endpoints[0] = i;
inSequence = true;
n++;
}
endpoints[1] = endpoints[0] + n - 1;
return n > 0 ? endpoints : null;
}
}