View Javadoc

1   package paolomind.multitalk.netmessage.impl;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import paolomind.multitalk.netmessage.IMessageFactory;
8   import paolomind.multitalk.netmessage.IMessage;
9   import paolomind.multitalk.netmessage.MessageException;
10  
11  /**
12   * Classe che costruisce messaggi per contenere altri messaggi.
13   * @author paolo
14   *
15   */
16  public class MultiMessageFactoryImpl implements IMessageFactory {
17  
18    /**
19     * separatore dei messaggi.
20     */
21    public static final String SEPARATOR;
22  
23    /** */
24    private IMessageFactory factory;
25  
26    static {
27      String s = System.getProperty("mexseparator");
28      if (s == null) {
29        s = "\n-\n";
30      }
31      SEPARATOR = s;
32    }
33  
34    /**
35     * crea un mesaggio vuoto.
36     * @return un Message
37     * @see paolomind.multitalk.netmessage.IMessageFactory#createMessage()
38     */
39    public final IMessage createMessage() {
40      return new MultiMessageImpl();
41    }
42  
43    /**
44     * crea un messeggio da un stringha che rappresenta il messaggio originale.
45     * @param s il testo del messaggio
46     * @return un Message
47     * @throws MessageException se il testo non è valido
48     * @see paolomind.multitalk.netmessage.IMessageFactory#createMessage(java.lang.String)
49     */
50    public final IMessage createMessage(final String s) throws MessageException {
51      return new MultiMessageImpl(s);
52    }
53  
54    /**
55     * implementazione di IMessage.
56     * @author paolo
57     *
58     */
59    private class MultiMessageImpl implements paolomind.multitalk.netmessage.IMessage {
60  
61      /** */
62      private static final long serialVersionUID = -3548197602594421396L;
63      /** */
64      private List infos;
65  
66      /** */
67      public MultiMessageImpl() {
68        infos = new ArrayList();
69      }
70  
71      /**
72       * @param mex string
73       * @throws MessageException eccezzione
74       */
75      public MultiMessageImpl(final String mex) throws MessageException {
76        this();
77        int s = 0, e;
78        int n = mex.length();
79        do {
80          e = mex.indexOf(SEPARATOR, s);
81          if (e < 0) {
82            e = n;
83          }
84          infos.add(factory.createMessage(mex.substring(s, e)));
85          s = e + SEPARATOR.length();
86        } while (s < n);
87      }
88  
89      /**
90       * @return object
91       * @see paolomind.multitalk.netmessage.IMessage#getInfo()
92       */
93      public final Object getInfo() {
94        if (infos.isEmpty()) {
95          return null;
96        }
97        IMessage s = (IMessage) infos.remove(0);
98        return s;
99      }
100 
101     /**
102      * @param info object
103      * @see paolomind.multitalk.netmessage.IMessage#addInfo(java.lang.Object)
104      */
105     public final void addInfo(final Object info) {
106       try {
107         this.addInfo((IMessage) info);
108       } catch (ClassCastException e) {
109         throw new ClassCastException("l'argomento deve essere un tipo message");
110       }
111     }
112 
113     /**
114      * @param info message
115      */
116     public final void addInfo(final IMessage info) {
117       this.infos.add(info);
118     }
119 
120     /**
121      * @return object
122      * @see paolomind.multitalk.netmessage.IMessage#viewInfo()
123      */
124     public final Object viewInfo() {
125       return this.infos.get(0);
126     }
127 
128     /**
129      * @return string
130      * @see java.lang.Object#toString()
131      */
132     public final String toString() {
133       StringBuffer m = new StringBuffer();
134       final int n = this.infos.size();
135       Iterator it = this.infos.iterator();
136       for (int i = 0; i < n - 1; i++) {
137         m.append((it.next().toString()) + SEPARATOR);
138       }
139       if (it.hasNext()) {
140         m.append(it.next().toString());
141       }
142       return m.toString();
143     }
144 
145     /**
146      * @return int
147      * @see java.lang.Object#hashCode()
148      */
149     public final int hashCode() {
150       return toString().hashCode();
151     }
152 
153     /**
154      * @param o object
155      * @return boolean
156      * @see java.lang.Object#equals(java.lang.Object)
157      */
158     public final boolean equals(final Object o) {
159       return (o != null && this.hashCode() == o.hashCode());
160     }
161 
162     /**
163      * @param varname string
164      * @param value object
165      * @see paolomind.multitalk.netmessage.IMessage#addInfo(java.lang.String, java.lang.Object)
166      */
167     public void addInfo(final String varname, final Object value) {
168       throw new UnsupportedOperationException("operazione non consentita");
169     }
170 
171     /**
172      * @return sring array
173      * @throws MessageException eccezzione
174      * @see paolomind.multitalk.netmessage.IMessage#getNameValue()
175      */
176     public String[] getNameValue() throws MessageException {
177       throw new UnsupportedOperationException("operazione non consentita");
178     }
179   }
180 
181   /**
182    * setta la factory per il messaggio da contenere.
183    * @param pfactory factory del messaggi
184    */
185   public final void setFactory(final IMessageFactory pfactory) {
186     this.factory = pfactory;
187   }
188 }