Menu

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

Download this file

147 lines (136 with data), 4.0 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package ls.util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.ImageIcon;
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);
}
/**
* Create and return an icon based on the given class's resource path and the
* path given
*
* @param classObj The class object used to located the resource
* @param path The path to the file
* @return The new {@link ImageIcon ImageIcon} instance or null if no resource is found
* @see Class#getResource(String)
*/
public static ImageIcon getIcon(Class<?> classObj,String path)
{
URL imgURL = classObj.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL);
}
else
{
Utils.err("Couldn't find file: " + path);
return null;
}
}
}
MongoDB Logo MongoDB