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