Menu

[r73]: / GraphScript / trunk / ls / util / Utils.java  Maximize  Restore  History

Download this file

121 lines (112 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package ls.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import ls.util.msg.ErrorHandler;
import ls.util.msg.MessageHandler;
import ls.util.msg.MessageManager;
/**
* A general utility class
*
* @author Lior Schejter
*/
public class Utils
{
/**
* A message manager instance to handle various message managers
*/
private static MessageManager messageManager = new MessageManager();
/**
* Add a message handler to the list of handlers used by this class's message manager
*
* @param handler The handler to add
* @see #messageManager
* @see MessageManager
* @see MessageHandler
*/
public static void addMessageHandler(MessageHandler handler)
{
if (handler == null) return;
messageManager.registerMessageHandler(handler);
}
/**
* Add an error handler to this class's message manager
* @param eh The error handler to add
* @see #messageManager
* @see MessageManager
* @see ErrorHandler
*/
public static void addErrorHandler(ErrorHandler eh)
{
if (eh == null) return;
messageManager.registerErrorHandler(eh);
}
/* (non-Javadoc)
* @see ls.util.msg.ErrorHandler#handleException(java.lang.Exception)
*/
public static void exn(Exception exn)
{
messageManager.exn(exn);
}
/**
* Output an error message.
* @param errMsg The error message to output.
*/
public static void err(String errMsg)
{
messageManager.err("Error: " + errMsg);
}
/* (non-Javadoc)
* @see ls.util.msg.MessageHandler#handleMsg(java.lang.String)
*/
public static void msg(String _msg)
{
messageManager.msg(_msg);
}
public static final String EMPTY_STRING = "";
/**
* Return a new collection, with all the elements of the given iterator
* @param iter
* @return The new collection containing all elements of the given iterator
*/
public static <E> Collection<E> collectionFromIterator(Iterator<E> iter)
{
Collection<E> ret = new ArrayList<E>();
if (iter != null)
{
for (;iter.hasNext();)
{
ret.add(iter.next());
}
}
return ret;
}
/**
* Add the elments provided by the given iterator to the given collection
* @param coll The target collection to add all the elements to
* @param iter The source iterator
*/
public static <E> void addToCollection(Collection<E> coll,Iterator<? extends E> iter)
{
if (coll == null || iter == null) return;
for (; iter.hasNext();)
coll.add(iter.next());
}
/**
* Retrieve the given file's file extension
* @param f The file to inspect
* @return The file's extension or NULL if <code>f == null</code>.
* @throws IOException In case the file cannot be inspected for some reason
* @see File
*/
public static String fileExtension(File f) throws IOException
{
if (f == null) return null;
String filename = f.getCanonicalPath();
int lastDotInd = filename.lastIndexOf('.');
if (lastDotInd < 0) return "";
return filename.substring(lastDotInd+1);
}
}
MongoDB Logo MongoDB