📄 functions.java
字号:
if (buffer.charAt(i) == '\r' && i + 1 < j && buffer.charAt(i + 1) != '\n') { buffer.insert(i + 1, '\n'); ++i; ++j; } else if (buffer.charAt(i) == '\n') { buffer.insert(i, '\r'); ++i; ++j; } ++i; } return buffer.toString(); } public static void Ticker(String text, int begin, int xOffset, int yOffset, int maxWidth, Graphics g, int anchor) { int strlen = text.length(); char c; if (begin >= strlen) { begin = 0; } for (int i = 0;; i = (i + 1) % strlen) { c = text.charAt((begin + i) % strlen);//imagine the text as a circle, take a char at the position (begin+i) if (xOffset + g.getFont().charWidth(c) > maxWidth) //try if it fits in { break; } g.drawChar(c, xOffset, yOffset, anchor); xOffset += g.getFont().charWidth(c); } } /** * Gets substring of given string that has desired width on the screen. * @param string the string to be cuted. * @param begin index in the string where is the first character of returned * substring. * @param width desiredd width of returned substring. * @param g represents the screen. * @return the substring of given string with desired width. */ static public String cutString(String string, int begin, int width, Graphics g) { int strWidth = 0; int strLength = string.length(); char c; int i = begin; while (i < strLength) { c = string.charAt(i); strWidth += g.getFont().charWidth(c); if (strWidth <= width) { i++; } else { break; } } return string.substring(begin, i); } /** * Returns true if left object is less than right object. * @param left left object * @param right right object * SRT_HDR_TIME left and right objects are headers, compare their times * SRT_HDR_TO left and right objects are headers, compare their "to" fields * SRT_HDR_FROM left and right objects are headers, compare their from fields * SRT_HDR_SUBJECT left and right objects are headers, compare their subject fields * SRT_HDR_SIZE left and right objects are headers, compare their size fields * SRT_HDR_RECORD_ID left and right objects are headers, compare their record id fields * SRT_HDR_MSGID left and right objects are headers, compare their message id fields * SRT_CNT_NAME left and right objects are contacts in address book, compare their names. * @param mode describes the type of left and right object * * @return true if left object is less than right object. */ static private boolean less(Object left, Object right, byte mode) { String l, r; switch (mode) { case SRT_HDR_TIME: return ((MessageHeader) left).getTime() < ((MessageHeader) right).getTime(); case SRT_HDR_TO: l = Functions.emailOnly(((MessageHeader) left).getRecipients()).toLowerCase(); r = Functions.emailOnly(((MessageHeader) right).getRecipients()).toLowerCase(); l = l.substring(0, l.indexOf("@")); r = r.substring(0, r.indexOf("@")); if (l.charAt(0) == r.charAt(0)) { return l.compareTo(r) < 0; } else { return l.charAt(0) < r.charAt(0); } case SRT_HDR_FROM: l = Functions.emailOnly(((MessageHeader) left).getFrom()).toLowerCase(); r = Functions.emailOnly(((MessageHeader) right).getFrom()).toLowerCase(); l = l.substring(0, l.indexOf("@")); r = r.substring(0, r.indexOf("@")); if (l.charAt(0) == r.charAt(0)) { return l.compareTo(r) < 0; } else { return l.charAt(0) < r.charAt(0); } case SRT_HDR_SUBJECT: l = ((MessageHeader) left).getSubject().toLowerCase(); r = ((MessageHeader) right).getSubject().toLowerCase(); if (l.length() == 0 || r.length() == 0) { return l.length() < r.length(); } if (l.charAt(0) == r.charAt(0)) { return l.compareTo(r) < 0; } else { return l.charAt(0) < r.charAt(0); } case SRT_HDR_SIZE: return ((MessageHeader) left).getSize() < ((MessageHeader) right).getSize(); case SRT_HDR_RECORD_ID: return ((MessageHeader) left).getRecordID() < ((MessageHeader) right).getRecordID(); case SRT_HDR_MSGID: l = ((MessageHeader) left).getMessageID().toLowerCase(); r = ((MessageHeader) right).getMessageID().toLowerCase(); if (l.length() == 0 || r.length() == 0) { return l.length() < r.length(); } if (l.charAt(0) == r.charAt(0)) { return l.compareTo(r) < 0; } else { return l.charAt(0) < r.charAt(0); } case SRT_CNT_NAME: l = ((AddressBook.Contact) left).getName().toLowerCase(); r = ((AddressBook.Contact) right).getName().toLowerCase(); if (l.charAt(0) == r.charAt(0)) { return l.compareTo(r) < 0; } else { return l.charAt(0) < r.charAt(0); } } return false; } static private boolean order(boolean val, byte order) { return order == SRT_ORDER_INC ? val : !val; } // exchange storage[i] and storage[j] private static void exch(Vector storage, int i, int j) { Object swap = storage.elementAt(i); storage.setElementAt(storage.elementAt(j), i); storage.setElementAt(swap, j); } // shuffle the storage private static void shuffle(Vector vector) { int N = vector.size(), r; for (int i = 0; i < N; i++) { r = i + Math.abs(new Random().nextInt()) % (N - i); // between i and N-1 exch(vector, i, r); } } private static int partition(Vector storage, int left, int right, byte order, byte mode) { int i = left - 1; int j = right; Object pivot = storage.elementAt(right); while (true) { // find item on left to swap. don't go out-of-bounds while (order(less(storage.elementAt(++i), pivot, mode), order) && i < right) { ; } // find item on right to swap. while (order(less(pivot, storage.elementAt(--j), mode), order) && j > left) { ; } // check if pointers cross if (i >= j) { break; } exch(storage, i, j); // swap two elements into place } exch(storage, i, right); // swap with partition element return i; } /** * Sorts elements in vector between positions left and right. * * @param vector * @param left * @param right * @param order * @param mode */ static public void sort(Vector vector, int left, int right, byte order, byte mode) { if (right <= left) { return; } int i = partition(vector, left, right, order, mode); sort(vector, left, i - 1, order, mode); sort(vector, i + 1, right, order, mode); } /** * Sorts vector. * * @param vector * @param order * @param mode */ //This is sort function that sorts a vector by given order (Funtioncs.SRT_ORDER_INC, Funtioncs.SRT_ORDER_DEC) and sort mode Functions.SRT_HDR_TIME... //The code is modified version of Princeton University code at http://www.cs.princeton.edu/introcs/42sort/QuickSort.java.html static public void sort(Vector vector, byte order, byte mode) { shuffle(vector); sort(vector, 0, vector.size() - 1, order, mode); } /** * Opens given record store. * @param file the string that describes the record store file. * @param createIfNeccessary true if record store should be created if it * does not exists. yet. * @return opened recordstor. * @throws mujmail.MyException if the record store cannot be opened. */ static public RecordStore openRecordStore(String file, boolean createIfNeccessary) throws MyException { try { if (DEBUG) System.out.println("DEBUG Functions.openRecordStore(String, boolean) - file: " + file); return RecordStore.openRecordStore(file, createIfNeccessary); } catch (Exception ex) { ex.printStackTrace(); throw new MyException(MyException.DB_CANNOT_OPEN_DB); } } /** * Closes given record store. * @param recordStore the record store to be closed. */ static public void closeRecordStore(RecordStore recordStore) { try { if (recordStore != null) { recordStore.closeRecordStore(); } } catch (Exception ex) { ex.printStackTrace(); } } /** * Returns how many bytes are left in given database represented by given file * name. * @param file the file name of the database. * @return number of bytes that are left in database. */ public static int spaceLeft(String file) { RecordStore store = null; int size = -1; try { //must be create a new one to give the correct space left. store = RecordStore.openRecordStore(file, true); size = store.getSizeAvailable(); } catch (Exception ex) { } closeRecordStore(store); return size; } /** * Gets the size of given database - that means occupied space. * @return database size in bytes. */ public static int spaceOccupied(String file) { RecordStore store = null; int size = 0; try { store = RecordStore.openRecordStore(file, true); size = store.getSize(); } catch (Exception ex) { ex.printStackTrace(); } closeRecordStore(store); return size; } /** * Formats size to be Bytes if <= 1024 or KBytes to one decimal point * if >1024 * @param size size to format in Bytes * @return formatted size as String */ public static String formatNumberByteorKByte(long size) { if (size <= 1024) { return String.valueOf(size) + "B"; } else { // l is file size in kB long l = size / 1024; // / 1024 ~ >> 10 double d = l * 10; // round kB number to one decimal point d = (d - Math.floor(d) >= 0.5 ? Math.ceil(d) / 10: Math.floor(d) / 10); return String.valueOf(d) + "kB"; } } // === NEW SORTING CONCEPT === /** * Sorts elements in {@link Vector} using comparator passed as parameter. * * @param vector vector to be sorted * @param comparator used for elements comparison */ public static void sort(Vector vector, Comparator comparator) { if (DEBUG) { System.out.println("DEBUG Functions.sort(Vector, Comparator) - comparator: " + comparator.getClass().getName() ); } shuffle(vector); sort(vector, 0, vector.size() - 1, comparator); } /** * Sorts {@link Vector} part bounded with leftBound and rightBound parameters. * * @param vector vector to be sorted * @param leftBound left bound of the vector part in which elements have to be sorted * @param rightBound right bound of the vector part in which elements have to be sorted * @param comparator used for elements comparison */ static public void sort(Vector vector, int leftBound, int rightBound, Comparator comparator) { if (rightBound <= leftBound) { return; } int i = partition(vector, leftBound, rightBound, comparator); sort(vector, leftBound, i - 1, comparator); sort(vector, i + 1, rightBound, comparator); } /** * Classic part of the QuickSort algorithm. * Pivot is chosen and elements are split on elements lower than pivot * and elements greater than pivot. * * @param vector vector we are sorting * @param leftBound left bound defining vector part we are interested in actually * @param rightBound right bound defining vector part we are interested in actually * @param comparator used for comparing elements in vector * @return index of the pivot in vector (the partitions with lower elements * and partitions with greater elements have to be sorted). * @see Comparator */ private static int partition(Vector vector, int leftBound, int rightBound, Comparator comparator) { int i = leftBound - 1; int j = rightBound; Object pivot = vector.elementAt(rightBound); while (true) { // find item on left to swap. don't go out-of-bounds while ( comparator.compare( vector.elementAt(++i), pivot) < 0 && i < rightBound ) { ; } // find item on right to swap. while (comparator.compare( pivot, vector.elementAt(--j) ) < 0 && j > leftBound ) { ; } // check if pointers cross if (i >= j) { break; } exch(vector, i, j); // swap two elements into place } exch(vector, i, rightBound); // swap with partition element return i; } /** * Adds mails that are in given storage to given vector. * @param storage the storage with mails to be added. * @param v the vector to that add the mails from given storage. */ public static void addMailsInStorageToVector( final IStorage storage, final Vector v) { final Enumeration emails = storage.getEnumeration(); while ( emails.hasMoreElements() ) { v.addElement( emails.nextElement() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -