001 package hirondelle.web4j.model; 002 003 import java.io.IOException; 004 import java.io.ObjectInputStream; 005 import java.io.Serializable; 006 import java.util.ArrayList; 007 import java.util.Collections; 008 import java.util.List; 009 010 /** General implementation of {@link MessageList}. */ 011 public class MessageListImpl implements MessageList, Serializable { 012 013 /** Create an empty {@link MessageList}. */ 014 public MessageListImpl(){ 015 //empty 016 } 017 018 /** 019 Create a {@link MessageList} having one simple {@link AppResponseMessage}. 020 021 <P>The argument satisfies the same conditions as {@link AppResponseMessage#forSimple}. 022 */ 023 public MessageListImpl(String aMessage) { 024 add(aMessage); 025 } 026 027 /** 028 Create a {@link MessageList} having one compound {@link AppResponseMessage}. 029 030 <P>The arguments satisfy the same conditions as 031 {@link AppResponseMessage#forCompound}. 032 */ 033 public MessageListImpl(String aMessage, Object... aParams) { 034 add(aMessage, aParams); 035 } 036 037 public final void add(String aMessage){ 038 fAppResponseMessages.add(AppResponseMessage.forSimple(aMessage)); 039 } 040 041 public final void add(String aMessage, Object... aParams){ 042 fAppResponseMessages.add(AppResponseMessage.forCompound(aMessage, aParams)); 043 } 044 045 public final void add (AppException ex) { 046 fAppResponseMessages.addAll( ex.getMessages() ); 047 } 048 049 public final List<AppResponseMessage> getMessages () { 050 return Collections.unmodifiableList(fAppResponseMessages); 051 } 052 053 public final boolean isNotEmpty () { 054 return !isEmpty(); 055 } 056 057 public final boolean isEmpty () { 058 return fAppResponseMessages.isEmpty(); 059 } 060 061 /** Intended for debugging only. */ 062 @Override public String toString(){ 063 return 064 "Messages : + " + fAppResponseMessages.toString() + 065 " Has Been Displayed : " + fHasBeenDisplayed 066 ; 067 } 068 069 // PRIVATE 070 071 /** List of {@link AppResponseMessage}s attached to this exception. */ 072 private final List<AppResponseMessage> fAppResponseMessages = new ArrayList<AppResponseMessage>(); 073 074 /** Controls the display-once-only behavior needed for JSPs. */ 075 private boolean fHasBeenDisplayed = false; 076 077 private static final long serialVersionUID = 1000L; 078 079 /** Always treat de-serialization as a full-blown constructor, by validating the final state of the deserialized object. */ 080 private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException { 081 aInputStream.defaultReadObject(); 082 } 083 }