package netpaint.messaging; import java.util.StringTokenizer; /** * Class AdministrativeMessage: This class extends Client Message and * is the base for all administrative network messages that will be * sent by the client. All of the more specific client message classes * will be derived from it and will add more and more spcific * functionality onto the basics provided in this class. * *
* Revision History: * v1.0 (Jan. 29, 2004) - Created the AdministrativeMessage class ** * @author Daniyar Zhanbekov * @version Version 1.0, Jan. 29, 2004 */ public abstract class AdministrativeMessage extends ClientMessage { /** * Creates a new
AdministrativeMessage instance.
*
* @param type a String value
* @param author a String value
*/
public AdministrativeMessage(final String type, final String author) {
super(type, author);
}
/**
* Returns a string representation of the message fit for plain-text
* transmission over the network.
*
* @return a String value
*/
public final String tokenize() {
return getMessageType() + ":" + getAuthor();
}
/**
* Parses the string into the specific message.
*
* @param message a String value
* @return an AbstractMessage value
*/
protected static AbstractMessage parse(final String message) {
String type = getMessageType(message);
StringTokenizer st = null;
if (type == null) {
return null;
}
if (!(type.equals(MessageSettings.JOIN_TYPE)
|| type.equals(MessageSettings.QUIT_TYPE))) {
return null;
}
st = new StringTokenizer(message, ":");
//Message type:
if (!st.hasMoreTokens()) { return null; }
type = st.nextToken();
//Username:
if (!st.hasMoreTokens()) { return null; }
if (type.equals(MessageSettings.JOIN_TYPE)) {
return new JoinMessage(st.nextToken());
} else if (type.equals(MessageSettings.QUIT_TYPE)) {
return new QuitMessage(st.nextToken());
}
return null;
}
}