edu.mit.csail.sdg.alloy4
Class Util

java.lang.Object
  extended by edu.mit.csail.sdg.alloy4.Util

public final class Util
extends java.lang.Object

This provides useful static methods for I/O and XML operations.

Thread Safety: Safe.


Nested Class Summary
static class Util.BooleanPref
          This reads and writes boolean-valued Java persistent preferences.
static class Util.IntPref
          This reads and writes integer-valued Java persistent preferences.
static class Util.StringPref
          This reads and writes String-valued Java persistent preferences.
 
Field Summary
static java.util.Comparator<java.lang.String> slashComparator
          Sorts two strings for optimum module order; we guarantee slashComparator(a,b)==0 iff a.equals(b).
 
Method Summary
static
<T> ConstList<T>
append(java.util.List<T> list, T element)
          Copy the input list, append "element" to it, then return the result as an unmodifiable list.
static
<T> T[]
append(T[] list, T element)
          Copy the input array, append "element" to it, then return the result as a new array.
static
<T> ConstList<T>
asList(T... array)
          Returns an unmodifiable List with same elements as the array.
static
<K,V> java.util.LinkedHashMap<K,V>
asMap(K[] keys, V... values)
          Returns a newly created LinkedHashMap mapping each key to its corresponding value, in the given order.
static
<V> java.util.LinkedHashSet<V>
asSet(V... values)
          Returns a newly created LinkedHashSet containing the given elements in the given order.
static java.lang.String canon(java.lang.String filename)
          Returns the canonical absolute path for a file.
static boolean close(java.io.Closeable object)
          Attempt to close the file/stream/reader/writer and return true if and only if we successfully closed it.
static java.lang.String convertLineBreak(java.lang.String input)
          Converts Windows/Mac/Unix linebreaks into '\n', and replace non-tab non-linebreak control characters into space.
static void copy(boolean executable, boolean keepPath, java.lang.String destdir, java.lang.String... names)
          Copy the list of files from JAR into the destination directory, then set the correct permissions on them if possible.
static java.lang.String encode(java.lang.String str)
          Encode special characters of a String using XML/HTML encoding.
static void encodeXML(java.io.PrintWriter out, java.lang.String str)
          Write a String into a PrintWriter, and encode special characters using XML-specific encoding.
static void encodeXML(java.lang.StringBuilder out, java.lang.String str)
          Write a String into a StringBuilder, and encode special characters using XML-specific encoding.
static void encodeXMLs(java.io.PrintWriter out, java.lang.String... strs)
          Write a list of Strings into a PrintWriter, where strs[2n] are written as-is, and strs[2n+1] are XML-encoded.
static void encodeXMLs(java.lang.StringBuilder out, java.lang.String... strs)
          Write a list of Strings into a StringBuilder, where strs[2n] are written as-is, and strs[2n+1] are XML-encoded.
static
<E> java.lang.Iterable<E>
fastJoin(java.lang.Iterable<E> collection1, java.lang.Iterable<E> collection2)
          Return an iterable whose iterator is a read-only iterator that first iterate over Collection1, and then Collection2.
static java.lang.String getCurrentDirectory()
          Returns the current "default directory" which is used by the FileOpen and FileSave dialogs.
static int indexOf(java.lang.String big, java.lang.String small, int start, boolean forward, boolean caseSensitive)
          Finds the first occurrence of small within big.
static java.lang.String jarPrefix()
          This returns the constant prefix to denote whether Util.readAll() should read from a JAR or read from the file system.
static int max(int bitwidth)
          Returns the largest allowed integer, or -1 if no integers are allowed (bitwidth < 1).
static int min(int bitwidth)
          Returns the smallest allowed integer, or 0 if no integers are allowed (bitwidth < 1)
static boolean onMac()
          Returns true iff running on Mac OS X.
static boolean onWindows()
          Returns true iff running on Windows
static
<T> ConstList<T>
prepend(java.util.List<T> list, T element)
          Copy the input list, prepend "element" to it, then return the result as an unmodifiable list.
static java.lang.String readAll(java.lang.String filename)
          Read everything into a String; throws IOException if an error occurred.
static void setCurrentDirectory(java.io.File newDirectory)
          Modifies the current "default directory" which is used by the FileOpen and FileSave dialogs.
static void shift(java.io.RandomAccessFile file, long from, long to)
          Copy file.content[from...f.length-1] into file.content[to...], then truncate the file after that point.
static int shiftmask(int bitwidth)
          Returns a mask of the form 000..0011..11 where the number of 1s is equal to the number of significant bits of the highest integer withing the given bitwidth
static java.lang.String tail(java.lang.String string)
          Returns the substring after the last "/"
static long writeAll(java.lang.String filename, java.lang.String content)
          Open then overwrite the file with the given content; throws Err if an error occurred.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

slashComparator

public static final java.util.Comparator<java.lang.String> slashComparator
Sorts two strings for optimum module order; we guarantee slashComparator(a,b)==0 iff a.equals(b).
(1) First of all, the builtin names "extend" and "in" are sorted ahead of other names
(2) Else, if one string starts with "this/", then it is considered smaller
(3) Else, if one string has fewer '/' than the other, then it is considered smaller.
(4) Else, we compare them lexically without case-sensitivity.
(5) Finally, we compare them lexically with case-sensitivity.

Method Detail

append

public static <T> ConstList<T> append(java.util.List<T> list,
                                      T element)
Copy the input list, append "element" to it, then return the result as an unmodifiable list.


append

public static <T> T[] append(T[] list,
                             T element)
Copy the input array, append "element" to it, then return the result as a new array.


prepend

public static <T> ConstList<T> prepend(java.util.List<T> list,
                                       T element)
Copy the input list, prepend "element" to it, then return the result as an unmodifiable list.


asList

public static <T> ConstList<T> asList(T... array)
Returns an unmodifiable List with same elements as the array.


asSet

public static <V> java.util.LinkedHashSet<V> asSet(V... values)
Returns a newly created LinkedHashSet containing the given elements in the given order.


asMap

public static <K,V> java.util.LinkedHashMap<K,V> asMap(K[] keys,
                                                       V... values)
Returns a newly created LinkedHashMap mapping each key to its corresponding value, in the given order.


fastJoin

public static <E> java.lang.Iterable<E> fastJoin(java.lang.Iterable<E> collection1,
                                                 java.lang.Iterable<E> collection2)
Return an iterable whose iterator is a read-only iterator that first iterate over Collection1, and then Collection2.


convertLineBreak

public static java.lang.String convertLineBreak(java.lang.String input)
Converts Windows/Mac/Unix linebreaks into '\n', and replace non-tab non-linebreak control characters into space.


close

public static boolean close(java.io.Closeable object)
Attempt to close the file/stream/reader/writer and return true if and only if we successfully closed it. (If object==null, we return true right away)


setCurrentDirectory

public static void setCurrentDirectory(java.io.File newDirectory)
Modifies the current "default directory" which is used by the FileOpen and FileSave dialogs.


getCurrentDirectory

public static java.lang.String getCurrentDirectory()
Returns the current "default directory" which is used by the FileOpen and FileSave dialogs.


jarPrefix

public static java.lang.String jarPrefix()
This returns the constant prefix to denote whether Util.readAll() should read from a JAR or read from the file system. (The reason we made this into a "method" rather than a constant String is that it is used by Util.canon() which is called by many static initializer blocks... so if we made this into a static field of Util, then it may not be initialized yet when we need it!)


readAll

public static java.lang.String readAll(java.lang.String filename)
                                throws java.io.FileNotFoundException,
                                       java.io.IOException
Read everything into a String; throws IOException if an error occurred. (If filename begins with Util.jarPrefix() then we read from the JAR instead)

Throws:
java.io.FileNotFoundException
java.io.IOException

writeAll

public static long writeAll(java.lang.String filename,
                            java.lang.String content)
                     throws Err
Open then overwrite the file with the given content; throws Err if an error occurred.

Throws:
Err

canon

public static final java.lang.String canon(java.lang.String filename)
Returns the canonical absolute path for a file. If an IO error occurred, or if the file doesn't exist yet, we will at least return a noncanonical but absolute path for it.

Note: if filename=="", we return "".


copy

public static void copy(boolean executable,
                        boolean keepPath,
                        java.lang.String destdir,
                        java.lang.String... names)
Copy the list of files from JAR into the destination directory, then set the correct permissions on them if possible.

Parameters:
executable - - if true, we will attempt to set the file's "executable" permission (failure to do this is ignored)
keepPath - - if true, the full path will be created for the destination file
destdir - - the destination directory
names - - the files to copy from the JAR

shift

public static void shift(java.io.RandomAccessFile file,
                         long from,
                         long to)
                  throws java.io.IOException
Copy file.content[from...f.length-1] into file.content[to...], then truncate the file after that point.

If (from > to), this means we simply delete the portion of the file beginning at "to" and up to but excluding "from".

If (from < to), this means we insert (to-from) number of ARBITRARY bytes into the "from" location and shift the original file content accordingly.

Note: after this operation, the file's current position will be moved to the start of the file.

Throws:
java.io.IOException - if (from < 0) || (to < 0) || (from >= file.length())

encodeXML

public static void encodeXML(java.io.PrintWriter out,
                             java.lang.String str)
Write a String into a PrintWriter, and encode special characters using XML-specific encoding.

In particular, it changes LESS THAN, GREATER THAN, AMPERSAND, SINGLE QUOTE, and DOUBLE QUOTE into "&lt;" "&gt;" "&amp;" "&apos;" and "&quot;" and turns any characters outside of the 32..126 range into the "&#xHHHH;" encoding (where HHHH is the 4 digit lowercase hexadecimal representation of the character value).

Parameters:
out - - the PrintWriter to write into
str - - the String to write out

encodeXML

public static void encodeXML(java.lang.StringBuilder out,
                             java.lang.String str)
Write a String into a StringBuilder, and encode special characters using XML-specific encoding.

In particular, it changes LESS THAN, GREATER THAN, AMPERSAND, SINGLE QUOTE, and DOUBLE QUOTE into "&lt;" "&gt;" "&amp;" "&apos;" and "&quot;" and turns any characters outside of the 32..126 range into the "&#xHHHH;" encoding (where HHHH is the 4 digit lowercase hexadecimal representation of the character value).

Parameters:
out - - the StringBuilder to write into
str - - the String to write out

encode

public static java.lang.String encode(java.lang.String str)
Encode special characters of a String using XML/HTML encoding.

In particular, it changes LESS THAN, GREATER THAN, AMPERSAND, SINGLE QUOTE, and DOUBLE QUOTE into "&lt;" "&gt;" "&amp;" "&apos;" and "&quot;" and turns any characters outside of the 32..126 range into the "&#xHHHH;" encoding (where HHHH is the 4 digit lowercase hexadecimal representation of the character value).


encodeXMLs

public static void encodeXMLs(java.io.PrintWriter out,
                              java.lang.String... strs)
Write a list of Strings into a PrintWriter, where strs[2n] are written as-is, and strs[2n+1] are XML-encoded.

For example, if you call encodeXML(out, A, B, C, D, E), it is equivalent to the following:
out.print(A);
encodeXML(out, B);
out.print(C);
encodeXML(out, D);
out.print(E);
In other words, it writes the even entries as-is, and writes the odd entries using XML encoding.

Parameters:
out - - the PrintWriter to write into
strs - - the list of Strings to write out

encodeXMLs

public static void encodeXMLs(java.lang.StringBuilder out,
                              java.lang.String... strs)
Write a list of Strings into a StringBuilder, where strs[2n] are written as-is, and strs[2n+1] are XML-encoded.

For example, if you call encodeXML(out, A, B, C, D, E), it is equivalent to the following:
out.append(A);
encodeXML(out, B);
out.append(C);
encodeXML(out, D);
out.append(E);
In other words, it writes the even entries as-is, and writes the odd entries using XML encoding.

Parameters:
out - - the StringBuilder to write into
strs - - the list of Strings to write out

indexOf

public static int indexOf(java.lang.String big,
                          java.lang.String small,
                          int start,
                          boolean forward,
                          boolean caseSensitive)
Finds the first occurrence of small within big.

Parameters:
big - - the String that we want to perform the search on
small - - the pattern we are looking forward
start - - the offset within "big" to start (for example: 0 means to start from the beginning of "big")
forward - - true if the search should go forward; false if it should go backwards
caseSensitive - - true if the search should be done in a case-sensitive manner
Returns:
0 or greater if found, -1 if not found (Note: if small=="", then we always return -1)

onWindows

public static boolean onWindows()
Returns true iff running on Windows


onMac

public static boolean onMac()
Returns true iff running on Mac OS X.


tail

public static java.lang.String tail(java.lang.String string)
Returns the substring after the last "/"


max

public static int max(int bitwidth)
Returns the largest allowed integer, or -1 if no integers are allowed (bitwidth < 1).


min

public static int min(int bitwidth)
Returns the smallest allowed integer, or 0 if no integers are allowed (bitwidth < 1)


shiftmask

public static int shiftmask(int bitwidth)
Returns a mask of the form 000..0011..11 where the number of 1s is equal to the number of significant bits of the highest integer withing the given bitwidth