EDIT: Turns out this is a long-standing bug with Eclipse and enums / array initializers, which is really unfortunate.
In Eclipse, I'm finding that I don't have access to any kind of code completion inside of enums. Here's a WIP, of course, snippet of code that I'm working with.
enum PlayerState implements State<Player> {
NORMAL(){
#Override
public void enter(Player player){
}
#Override
public void update(Player player){
}
},
JUMP_START(){
Timer jumpTimer = new Timer();
public void enter(Player player){
jumpTimer.set(1);
}
public void update(Player player){
}
},
GLOBAL_STATE(){
#Override
public void update(Player player){
}
};
#Override
public void enter(Player player) {
}
#Override
public void exit(Player player) {
}
#Override
public boolean onMessage(Player player, Telegram telegram) {
return false;
}
}
So in this code, I have no completion with "player.", or with "jumpTimer.". To note, I'm working with the GDX-AI library to manage a state machine, and it advises putting the states into an enum, like I have above. Note that I was able to have completion in IntelliJ. I've tried enabling "Java Proposals" in the Preferences/Java/Editor/Content Assist/Advanced section, but that didn't work, and restarting the IDE didn't help, either.
Any ideas?
EDIT: To confirm, this is just a snippet, and the code as a whole does compile. There's nothing wrong with the way it works so far, as far as I can tell; Eclipse isn't throwing any errors, so it's fine. The only issue is code completion, at the moment.
Oh, also, the strange thing is that Eclipse can tell me what's wrong with code (i.e. doing "changeState()" on the state machine from the state through the Player class will have a syntax error because I'm not providing an argument). So, there's some level of code checking, but no code hinting? I dunno...
Related
Is there any way I can get a system process and listen to it so I can get what it's doing? An example of what Im looking for is this:
RunningProcess proc = new RunningProcess("notepad.exe");
ProcListener procListener = new ProcListener(proc);
Listener class example:
public class ProcListener implements ProcessListener {
public ProcListener(...) {
}
#Override
public void started() {
}
#Override
public void terminated() {
}
#Override
public void ioOperation(IOoperation iop){
}
}
Is there something like that? I haven't found yet.
If you just want to run your jar whenever another program is started or terminated, I recommend using Bill2's Process Manager. Although this is not a Java way to do, it can launch a program or batch file when some processes started/terminated, and you can execute the jar using batch file. After installing, you may want to change the language in Options first. The software is simple and quite easy to use.
Sorry if this was asked, but I keep wondering and weren't able to google up solution and not for luck of trying.
When implementing Command pattern inside one class, this one-method interface keeps popping up in all places.
public interface Command {
void execute();
}
Then it gets reused plenty of times like this:
public void doAction1()
{
perform(new Command () {
#Override
public void execute()
{
//do some crazy stuff
}
});
}
public void doAction2()
{
perform(new Command () {
#Override
public void execute()
{
//do some event crazier stuff
}
});
}
public void doAction3()
{
perform(new Command () {
#Override
public void execute()
{
//do a barrel roll
}
});
}
private void perform(Command command)
{
command.execute();
}
Different namings, different modules, different software even --- but this one gets reimplemented over and over, cluttering source and doing essentially nothing new.
Is there any generic, OOB one-method interface that's OK to use instead of creating my own every time I need lambda-like sentence? Is it OK to use Runnable in this way? Wouldn't it create confusion in the mind of some future source code-reader?
PS: Now, I know, that there's java 1.8 with lambdas and all, but at my work we're stuck with 1.6 for the moment (enterprise customers are so enterprise), so I'd appreciate some archeological help here.
Yes, Runnable or Callable (if you need to return a result) are perfectly fine to use.
I have code which I have been using for years and this morning I noticed property change isn't being called when the task is done. I've got the swing worker set up as an inner class and I put a break point on the String properyName = evt..... and it never hits the break point.
void loadData() {
work2 = new bkgdLoadData();
work2.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if( propertyName.equals("state")) {
SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
if( state == SwingWorker.StateValue.DONE) {
work2 = null;
}
}
}
});
work2.execute();
}
You can see that I set the object work2 to null when the task is finished and now it is no longer being set to null. In the class I added a done routine which it hits when the doinbackground is finished. What is puzzling me is why the property change listener isn't triggered. Something must have changed without my noticing.
protected class bkgdLoadData extends SwingWorker<Integer, Object> {
#Override
protected Integer doInBackground() {
switch(bkgdMode) {
case 0:
doRead();
break;
case 1:
doWrite();
break;
case 2:
runRobot();
break;
}
return 0;
}
#Override
protected void done() {
int i=0;
i++;
}
}
The breakpoint at done is hit but no property change notice is delivered. (I put the done routine for the sole purpose of verifying that the swing worker knows that it is done.)
I looked at the documentation and I don't see that I have to manually fire off some sort of property change, so I am really, really stuck and would appreciate another pair of eyes to tell me what stupid mistake I am mistaking.
Thanks,
Ilan
It turned out my Java was corrupted. Removing JDK 1.6 and reinstalling it from the repository wasn't good enough.
My links in Netbeans to 1.6 got damamged and I had to reinstall Netbeans as well (going over to 7.3.1 in the process). Netbeans would not recognize the repository JDK 1.6 as valid so I had to go to Oracle and get the original version. Netbeans recognized the original and the problem I reported above was no longer a problem.
I removed the void done() routine since it had no purpose other than a place to put a break point. The code as such is OK. Thanks for the help.
I just can't figure out how to remove listener when I have some event executed. I have some leaking issue using websockets, and this can probaly fix it.
final WebSocket w = asyncHttpClient.prepareGet(url)
.execute(new WebSocketUpgradeHandler.Builder().build())
.get();
w.addWebSocketListener(new WebSocketTextListener() {
public void onMessage(String message) {
listener.onMessage(responseMessage);
// Here is the place I want to do my w.removeWebSocketListener(l);
}
#Override
public void onFragment(String s, boolean b) {
}
public void onOpen(WebSocket websocket) {
}
#Override
public void onClose(WebSocket webSocket) {
}
#Override
public void onError(Throwable throwable) {
}
});
The problem is when I create WebSocketTextListener lis = new .... and passing in there is something like one object need other object and other object is dependent on this, and I'm still now allowed to do what I want.
Looks like it is something simple, but can't figure out.
Normally event listeners can be removed with a removeXXXListener method. But it requires that you provide the exact same event listener instance as parameter. You can store the event listener and later remove it using the same reference. But since you, in the onMessage message already are inside the scope of the event listener, using this should work.
Try something like
listener.onMessage(responseMessage);
// Here is the place I want to do my w.removeWebSocketListener(l);
w.removeWebSocketListener(this);
Using "this" in anonumous inner class is the way to solve problem. But, it is muck better to refactor code, to avoid using anonumous classes, for testability and better understanding.
Here is my code:
public void showScore(Player winner)
{
view.runOnUiThread(new Runnable()
{
public void run()
{
String scored = view.getString(R.string.scored_a_goal);
String score = view.getString(R.string.score);
Toast.makeText(view,winner.name+" "+view.getString(R.string.scored_a_goal),Toast.LENGTH_SHORT).show();
}
});
}
Now everything is fine except for I cannot access winner(of type Player) from inside the Runnable, what is the work around for this?
Just define winner as final Player winner and this code should work. Hope this helps.
What is your IDE you are using, most modern Java IDEs will help you with that, just make the winner final and everything will go fine.