Tweaks and documentation for ModeParser.

fix-message-formatting
Danny van Heumen 12 years ago
parent 69a76652af
commit 5851e1b719

@ -7,30 +7,56 @@
public class ModeParser public class ModeParser
{ {
/**
* List of parsed, processed modes.
*/
private final List<ModeEntry> modes = new ArrayList<ModeEntry>(); private final List<ModeEntry> modes = new ArrayList<ModeEntry>();
/**
* Index of current parameter.
*/
private int index = 0; private int index = 0;
/**
* Additional parameters
*/
private String[] params; private String[] params;
/**
* Constructor for parsing based on an irc-api ChannelModeMessage.
*
* @param message ChannelModeMessage describing mode changes
*/
public ModeParser(ChannelModeMessage message) public ModeParser(ChannelModeMessage message)
{ {
this(message.getModeStr()); this(message.getModeStr());
} }
/**
* Constructor for initiating mode parser and parsing mode string.
*
* @param modestring mode string that should be parsed
*/
protected ModeParser(String modestring) protected ModeParser(String modestring)
{ {
String[] parts = modestring.split(" "); String[] parts = modestring.split(" ");
String mode = parts[0]; String mode = parts[0];
this.params = new String[parts.length-1]; this.params = new String[parts.length - 1];
System.arraycopy(parts, 1, this.params, 0, parts.length-1); System.arraycopy(parts, 1, this.params, 0, parts.length - 1);
parse(mode); parse(mode);
} }
/**
* Parse a complete mode string and extract individual mode entries.
*
* @param modestring full mode string
*/
private void parse(String modestring) private void parse(String modestring)
{ {
boolean adding = true; boolean adding = true;
for(char c : modestring.toCharArray()) for (char c : modestring.toCharArray())
{ {
switch(c) switch (c)
{ {
case '+': case '+':
adding = true; adding = true;
@ -44,61 +70,115 @@ private void parse(String modestring)
ModeEntry entry = process(adding, c); ModeEntry entry = process(adding, c);
modes.add(entry); modes.add(entry);
} }
catch(IllegalArgumentException e) catch (IllegalArgumentException e)
{ {
System.out.println("Unknown mode encountered: '"+c+"' (mode string '"+modestring+"')"); System.out.println("Unknown mode encountered: '" + c
+ "' (mode string '" + modestring + "')");
} }
break; break;
} }
} }
} }
/**
* Process mode character given state of addition/removal.
*
* @param add indicates whether mode change is addition or removal
* @param mode mode character
* @return returns an entry that contains all parts of an individual mode
* change
*/
private ModeEntry process(boolean add, char mode) private ModeEntry process(boolean add, char mode)
{ {
switch(mode) switch (mode)
{ {
case 'O': case 'O':
return new ModeEntry(add, Mode.bySymbol(mode), this.params[this.index++]); return new ModeEntry(add, Mode.bySymbol(mode),
this.params[this.index++]);
case 'o': case 'o':
return new ModeEntry(add, Mode.bySymbol(mode), this.params[this.index++]); return new ModeEntry(add, Mode.bySymbol(mode),
this.params[this.index++]);
case 'v': case 'v':
return new ModeEntry(add, Mode.bySymbol(mode), this.params[this.index++]); return new ModeEntry(add, Mode.bySymbol(mode),
this.params[this.index++]);
case 'l': case 'l':
String[] params = (add ? new String[] { this.params[this.index++] } : new String[]{}); String[] params = (add ? new String[]
{ this.params[this.index++] } : new String[] {});
return new ModeEntry(add, Mode.bySymbol(mode), params); return new ModeEntry(add, Mode.bySymbol(mode), params);
default: default:
throw new IllegalArgumentException(""+mode); throw new IllegalArgumentException("" + mode);
} }
} }
/**
* Get list of modes.
*
* @return returns list of parsed modes
*/
public List<ModeEntry> getModes() public List<ModeEntry> getModes()
{ {
return modes; return modes;
} }
/**
* Class for single mode entry, optionally with corresponding parameter(s).
*/
public static class ModeEntry public static class ModeEntry
{ {
/**
* Flag to indicate addition or removal of mode.
*/
private final boolean added; private final boolean added;
/**
* Type of mode.
*/
private final Mode mode; private final Mode mode;
/**
* Optional additional parameter(s).
*/
private final String[] params; private final String[] params;
protected ModeEntry(boolean add, Mode mode, String... params) /**
* Constructor.
*
* @param add true if mode is added, false if it is removed
* @param mode type of mode
* @param params optional, additional parameters
*/
private ModeEntry(boolean add, Mode mode, String... params)
{ {
this.added = add; this.added = add;
this.mode = mode; this.mode = mode;
this.params = params; this.params = params;
} }
/**
* Added or removed.
*
* @return returns true if added, removed otherwise
*/
public boolean isAdded() public boolean isAdded()
{ {
return this.added; return this.added;
} }
/**
* Get type of mode
*
* @return returns enum instance of mode
*/
public Mode getMode() public Mode getMode()
{ {
return this.mode; return this.mode;
} }
/**
* Get additional parameters
*
* @return returns array of additional parameters if any
*/
public String[] getParams() public String[] getParams()
{ {
return this.params; return this.params;

Loading…
Cancel
Save