summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/GenericCommandAutoService.java
blob: b9c1d5ede300c13bc8905b6fd23df81d99fb643e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package f00f.net.irc.martyr;

import java.util.Observable;
import java.util.Observer;

/**
 * Provides a framework for an auto service that operates with
 * InCommands.  Does enable by default.  Splits the 'update' method
 * into two, 'updateState' and 'updateCommand'.  Also provides thread
 * safety on all methods.
 */
public abstract class GenericCommandAutoService implements Observer
{

protected boolean enabled = false;
protected IRCConnection connection;

protected GenericCommandAutoService( IRCConnection connection )
{
	this.connection = connection;

	enable();
}

public synchronized void enable()
{
	if( enabled )
		return;
	
	connection.addCommandObserver( this );
	enabled = true;
}

public synchronized void disable()
{
	if( !enabled )
		return;
		
	connection.removeCommandObserver( this );
	enabled = false;
}

public synchronized void update( Observable observer, Object updated )
{
	if( !enabled )
		throw new IllegalStateException("This observer is not enabled." );
	if( updated instanceof State )
	{
		throw new IllegalArgumentException("This is not a state observer." );
	}
	else if( updated instanceof InCommand )
	{
		updateCommand( (InCommand)updated );
	}
	else
	{
		throw new IllegalArgumentException("Unknown object given to update.");
	}
}

protected IRCConnection getConnection()
{
	return connection;
}

protected synchronized boolean isEnabled()
{
	return enabled;
}

protected abstract void updateCommand( InCommand command );


// END AutoRegister
}