susimail:

- Generics in Folder
- for each
- type arguments
This commit is contained in:
str4d
2013-11-22 11:18:41 +00:00
parent e853d9a40b
commit 8d826cee93
3 changed files with 42 additions and 54 deletions

View File

@@ -41,7 +41,7 @@ import java.util.Iterator;
* *
* @author susi * @author susi
*/ */
public class Folder { public class Folder<O extends Object> {
public static final String PAGESIZE = "pager.pagesize"; public static final String PAGESIZE = "pager.pagesize";
public static final int DEFAULT_PAGESIZE = 10; public static final int DEFAULT_PAGESIZE = 10;
@@ -50,10 +50,10 @@ public class Folder {
public static final boolean UP = true; public static final boolean UP = true;
private int pages, pageSize, currentPage; private int pages, pageSize, currentPage;
private Object[] unsortedElements, elements; private O[] unsortedElements, elements;
private Hashtable sorter; private Hashtable<String, Comparator<O>> sorter;
private boolean sortingDirection; private boolean sortingDirection;
Comparator currentSorter; Comparator<O> currentSorter;
public Folder() public Folder()
{ {
@@ -61,7 +61,7 @@ public class Folder {
pageSize = 0; pageSize = 0;
currentPage = 1; currentPage = 1;
unsortedElements = null; unsortedElements = null;
sorter = new Hashtable(); sorter = new Hashtable<String, Comparator<O>>();
sortingDirection = UP; sortingDirection = UP;
currentSorter = null; currentSorter = null;
} }
@@ -129,12 +129,13 @@ public class Folder {
* @param source Array to copy. * @param source Array to copy.
* @return Copy of source. * @return Copy of source.
*/ */
private Object[] copyArray( Object[] source ) @SuppressWarnings("unchecked")
private O[] copyArray( O[] source )
{ {
Object[] destination = new Object[source.length]; Object[] destination = new Object[source.length];
for( int i = 0; i < source.length; i++ ) for( int i = 0; i < source.length; i++ )
destination[i] = source[i]; destination[i] = source[i];
return destination; return (O[])destination;
} }
/** /**
* Recalculates variables. * Recalculates variables.
@@ -171,9 +172,9 @@ public class Folder {
/** /**
* Set the array of objects the folder should manage. * Set the array of objects the folder should manage.
* *
* @param elements Array of Objects. * @param elements Array of Os.
*/ */
public void setElements( Object[] elements ) public void setElements( O[] elements )
{ {
this.unsortedElements = elements; this.unsortedElements = elements;
if( currentSorter != null ) if( currentSorter != null )
@@ -187,9 +188,9 @@ public class Folder {
* Returns an iterator containing the elements on the current page. * Returns an iterator containing the elements on the current page.
* @return Iterator containing the elements on the current page. * @return Iterator containing the elements on the current page.
*/ */
public Iterator currentPageIterator() public Iterator<O> currentPageIterator()
{ {
ArrayList list = new ArrayList(); ArrayList<O> list = new ArrayList<O>();
if( elements != null ) { if( elements != null ) {
int pageSize = getPageSize(); int pageSize = getPageSize();
int offset = ( currentPage - 1 ) * pageSize; int offset = ( currentPage - 1 ) * pageSize;
@@ -249,7 +250,7 @@ public class Folder {
* @param id ID to identify the Comparator with @link sortBy() * @param id ID to identify the Comparator with @link sortBy()
* @param sorter a Comparator to sort the Array given by @link setElements() * @param sorter a Comparator to sort the Array given by @link setElements()
*/ */
public void addSorter( String id, Comparator sorter ) public void addSorter( String id, Comparator<O> sorter )
{ {
this.sorter.put( id, sorter ); this.sorter.put( id, sorter );
} }
@@ -263,7 +264,7 @@ public class Folder {
*/ */
public void sortBy( String id ) public void sortBy( String id )
{ {
currentSorter = (Comparator)sorter.get( id ); currentSorter = sorter.get( id );
sort(); sort();
} }
@@ -273,9 +274,9 @@ public class Folder {
* @param x Position of the element on the current page. * @param x Position of the element on the current page.
* @return Element on the current page on the given position. * @return Element on the current page on the given position.
*/ */
public Object getElementAtPosXonCurrentPage( int x ) public O getElementAtPosXonCurrentPage( int x )
{ {
Object result = null; O result = null;
if( elements != null ) { if( elements != null ) {
int pageSize = getPageSize(); int pageSize = getPageSize();
int offset = ( currentPage - 1 ) * pageSize; int offset = ( currentPage - 1 ) * pageSize;
@@ -306,7 +307,7 @@ public class Folder {
* *
* @return First element. * @return First element.
*/ */
public Object getFirstElement() public O getFirstElement()
{ {
/* /*
* sorting direction is taken into account from getElement * sorting direction is taken into account from getElement
@@ -319,7 +320,7 @@ public class Folder {
* *
* @return Last element. * @return Last element.
*/ */
public Object getLastElement() public O getLastElement()
{ {
/* /*
* sorting direction is taken into account from getElement * sorting direction is taken into account from getElement
@@ -333,7 +334,7 @@ public class Folder {
* @param element * @param element
* @return index * @return index
*/ */
private int getIndexOf( Object element ) private int getIndexOf( O element )
{ {
if( elements != null ) { if( elements != null ) {
for( int i = 0; i < elements.length; i++ ) for( int i = 0; i < elements.length; i++ )
@@ -350,9 +351,9 @@ public class Folder {
* @param element * @param element
* @return The next element * @return The next element
*/ */
public Object getNextElement( Object element ) public O getNextElement( O element )
{ {
Object result = null; O result = null;
int i = getIndexOf( element ); int i = getIndexOf( element );
@@ -371,9 +372,9 @@ public class Folder {
* @param element * @param element
* @return The previous element * @return The previous element
*/ */
public Object getPreviousElement( Object element ) public O getPreviousElement( O element )
{ {
Object result = null; O result = null;
int i = getIndexOf( element ); int i = getIndexOf( element );
@@ -390,9 +391,9 @@ public class Folder {
* @param i * @param i
* @return Element at index i * @return Element at index i
*/ */
private Object getElement( int i ) private O getElement( int i )
{ {
Object result = null; O result = null;
if( elements != null ) { if( elements != null ) {
if( sortingDirection == DOWN ) if( sortingDirection == DOWN )
@@ -424,7 +425,7 @@ public class Folder {
* *
* @param element * @param element
*/ */
public boolean isLastElement( Object element ) public boolean isLastElement( O element )
{ {
if( elements == null ) if( elements == null )
return false; return false;
@@ -437,7 +438,7 @@ public class Folder {
* *
* @param element * @param element
*/ */
public boolean isFirstElement( Object element ) public boolean isFirstElement( O element )
{ {
if( elements == null ) if( elements == null )
return false; return false;

View File

@@ -49,7 +49,6 @@ import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.ListIterator;
import java.util.Locale; import java.util.Locale;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@@ -316,7 +315,7 @@ public class WebMail extends HttpServlet
int state, smtpPort; int state, smtpPort;
POP3MailBox mailbox; POP3MailBox mailbox;
MailCache mailCache; MailCache mailCache;
Folder folder; Folder<String> folder;
String user, pass, host, error, info; String user, pass, host, error, info;
String replyTo, replyCC; String replyTo, replyCC;
String subject, body, showUIDL; String subject, body, showUIDL;
@@ -413,8 +412,7 @@ public class WebMail extends HttpServlet
if( mailPart.multipart ) { if( mailPart.multipart ) {
if( mailPart.type.compareTo( "multipart/alternative" ) == 0 ) { if( mailPart.type.compareTo( "multipart/alternative" ) == 0 ) {
MailPart chosen = null; MailPart chosen = null;
for( ListIterator li = mailPart.parts.listIterator(); li.hasNext(); ) { for( MailPart subPart : mailPart.parts ) {
MailPart subPart = (MailPart)li.next();
if( subPart.type != null && subPart.type.compareTo( "text/plain" ) == 0 ) if( subPart.type != null && subPart.type.compareTo( "text/plain" ) == 0 )
chosen = subPart; chosen = subPart;
} }
@@ -423,16 +421,12 @@ public class WebMail extends HttpServlet
return; return;
} }
} }
for( ListIterator li = mailPart.parts.listIterator(); li.hasNext(); ) { for( MailPart part : mailPart.parts )
MailPart part = (MailPart)li.next();
showPart( out, part, level + 1, html ); showPart( out, part, level + 1, html );
}
} }
else if( mailPart.message ) { else if( mailPart.message ) {
for( ListIterator li = mailPart.parts.listIterator(); li.hasNext(); ) { for( MailPart part : mailPart.parts )
MailPart part = (MailPart)li.next();
showPart( out, part, level + 1, html ); showPart( out, part, level + 1, html );
}
} }
else { else {
boolean showBody = false; boolean showBody = false;
@@ -627,7 +621,7 @@ public class WebMail extends HttpServlet
sessionObject.smtpPort = smtpPortNo; sessionObject.smtpPort = smtpPortNo;
sessionObject.state = STATE_LIST; sessionObject.state = STATE_LIST;
sessionObject.mailCache = new MailCache( sessionObject.mailbox ); sessionObject.mailCache = new MailCache( sessionObject.mailbox );
sessionObject.folder = new Folder(); sessionObject.folder = new Folder<String>();
sessionObject.folder.setElements( sessionObject.mailbox.getUIDLs() ); sessionObject.folder.setElements( sessionObject.mailbox.getUIDLs() );
sessionObject.folder.addSorter( SORT_ID, new IDSorter( sessionObject.mailCache ) ); sessionObject.folder.addSorter( SORT_ID, new IDSorter( sessionObject.mailCache ) );
sessionObject.folder.addSorter( SORT_SENDER, new SenderSorter( sessionObject.mailCache ) ); sessionObject.folder.addSorter( SORT_SENDER, new SenderSorter( sessionObject.mailCache ) );
@@ -1052,8 +1046,8 @@ public class WebMail extends HttpServlet
return part; return part;
if( part.multipart || part.message ) { if( part.multipart || part.message ) {
for( Iterator it = part.parts.iterator(); it.hasNext(); ) { for( MailPart p : part.parts ) {
MailPart subPart = getMailPartFromHashCode( (MailPart)it.next(), hashCode ); MailPart subPart = getMailPartFromHashCode( p, hashCode );
if( subPart != null ) if( subPart != null )
return subPart; return subPart;
} }
@@ -1275,7 +1269,7 @@ public class WebMail extends HttpServlet
if( sessionObject.state == STATE_LIST ) { if( sessionObject.state == STATE_LIST ) {
processFolderButtons( sessionObject, request ); processFolderButtons( sessionObject, request );
for( Iterator it = sessionObject.folder.currentPageIterator(); it != null && it.hasNext(); ) { for( Iterator<String> it = sessionObject.folder.currentPageIterator(); it != null && it.hasNext(); ) {
String uidl = (String)it.next(); String uidl = (String)it.next();
Mail mail = sessionObject.mailCache.getMail( uidl, MailCache.FETCH_HEADER ); Mail mail = sessionObject.mailCache.getMail( uidl, MailCache.FETCH_HEADER );
if( mail != null && mail.error.length() > 0 ) { if( mail != null && mail.error.length() > 0 ) {
@@ -1520,8 +1514,7 @@ public class WebMail extends HttpServlet
} }
if( multipart ) { if( multipart ) {
for( Iterator it = sessionObject.attachments.iterator(); it.hasNext(); ) { for( Attachment attachment : sessionObject.attachments ) {
Attachment attachment = (Attachment)it.next();
body.append( "\r\n--" + boundary + "\r\nContent-type: " + attachment.getContentType() + "\r\nContent-Disposition: attachment; filename=\"" + attachment.getFileName() + "\"\r\nContent-Transfer-Encoding: " + attachment.getTransferEncoding() + "\r\n\r\n" ); body.append( "\r\n--" + boundary + "\r\nContent-type: " + attachment.getContentType() + "\r\nContent-Disposition: attachment; filename=\"" + attachment.getFileName() + "\"\r\nContent-Transfer-Encoding: " + attachment.getTransferEncoding() + "\r\n\r\n" );
body.append( attachment.getData() ); body.append( attachment.getData() );
} }
@@ -1615,12 +1608,11 @@ public class WebMail extends HttpServlet
if( sessionObject.attachments != null && !sessionObject.attachments.isEmpty() ) { if( sessionObject.attachments != null && !sessionObject.attachments.isEmpty() ) {
boolean wroteHeader = false; boolean wroteHeader = false;
for( Iterator it = sessionObject.attachments.iterator(); it.hasNext(); ) { for( Attachment attachment : sessionObject.attachments ) {
if( !wroteHeader ) { if( !wroteHeader ) {
out.println( "<tr><td colspan=\"2\" align=\"center\">" + _("Attachments:") + "</td></tr>" ); out.println( "<tr><td colspan=\"2\" align=\"center\">" + _("Attachments:") + "</td></tr>" );
wroteHeader = true; wroteHeader = true;
} }
Attachment attachment = (Attachment)it.next();
out.println( "<tr><td colspan=\"2\" align=\"center\"><input type=\"checkbox\" class=\"optbox\" name=\"check" + attachment.hashCode() + "\" value=\"1\">&nbsp;" + attachment.getFileName() + "</td></tr>"); out.println( "<tr><td colspan=\"2\" align=\"center\"><input type=\"checkbox\" class=\"optbox\" name=\"check" + attachment.hashCode() + "\" value=\"1\">&nbsp;" + attachment.getFileName() + "</td></tr>");
} }
} }
@@ -1682,7 +1674,7 @@ public class WebMail extends HttpServlet
thSpacer + "<th>" + sortHeader( SORT_SIZE, _("Size"), sessionObject.imgPath ) + "</th></tr>" ); thSpacer + "<th>" + sortHeader( SORT_SIZE, _("Size"), sessionObject.imgPath ) + "</th></tr>" );
int bg = 0; int bg = 0;
int i = 0; int i = 0;
for( Iterator it = sessionObject.folder.currentPageIterator(); it != null && it.hasNext(); ) { for( Iterator<String> it = sessionObject.folder.currentPageIterator(); it != null && it.hasNext(); ) {
String uidl = (String)it.next(); String uidl = (String)it.next();
Mail mail = sessionObject.mailCache.getMail( uidl, MailCache.FETCH_HEADER ); Mail mail = sessionObject.mailCache.getMail( uidl, MailCache.FETCH_HEADER );
String link = "<a href=\"" + myself + "?" + SHOW + "=" + i + "\">"; String link = "<a href=\"" + myself + "?" + SHOW + "=" + i + "\">";

View File

@@ -59,8 +59,6 @@ public class POP3MailBox {
private Object synchronizer = null; private Object synchronizer = null;
private Object[] uidls = null;
/** /**
* @param host * @param host
* @param port * @param port
@@ -275,7 +273,6 @@ public class POP3MailBox {
uidlToID.clear(); uidlToID.clear();
uidlList.clear(); uidlList.clear();
uidls = null;
readBuffer = sendCmdNa( "UIDL", DEFAULT_BUFSIZE ); readBuffer = sendCmdNa( "UIDL", DEFAULT_BUFSIZE );
if( readBuffer != null ) { if( readBuffer != null ) {
@@ -295,7 +292,6 @@ public class POP3MailBox {
} }
} }
} }
uidls = uidlList.toArray();
} }
else { else {
System.err.println( "Error getting UIDL list from pop3 server."); System.err.println( "Error getting UIDL list from pop3 server.");
@@ -350,7 +346,6 @@ public class POP3MailBox {
uidlList.clear(); uidlList.clear();
uidlToID.clear(); uidlToID.clear();
sizes.clear(); sizes.clear();
uidls = null;
mails = 0; mails = 0;
} }
/** /**
@@ -649,7 +644,7 @@ public class POP3MailBox {
private int getIDfromUIDL( String uidl ) private int getIDfromUIDL( String uidl )
{ {
int result = -1; int result = -1;
Integer intObject = (Integer)uidlToID.get( uidl ); Integer intObject = uidlToID.get( uidl );
if( intObject != null ) { if( intObject != null ) {
result = intObject.intValue(); result = intObject.intValue();
} }
@@ -662,15 +657,15 @@ public class POP3MailBox {
*/ */
public String getUIDLfromID( int id ) public String getUIDLfromID( int id )
{ {
return (String)uidlList.get( id ); return uidlList.get( id );
} }
/** /**
* *
* @return A list of the available UIDLs. * @return A list of the available UIDLs.
*/ */
public Object[] getUIDLs() public String[] getUIDLs()
{ {
return uidls; return uidlList.toArray(new String[uidlList.size()]);
} }
/** /**
* *