Jump to content

Trying to understand this Sleep class


Epos OSBot

Recommended Posts

public class Sleep extends ConditionalSleep {
	
	private BooleanSupplier condition;
	
	public Sleep(BooleanSupplier condition, int timeout) {
		super(timeout);
		this.condition = condition;
	}

	@Override
	public boolean condition() throws InterruptedException {
		return condition.getAsBoolean();
	}
	
	public static boolean sleepUntil(BooleanSupplier condition, int timeout) {
		return new Sleep(condition, timeout).sleep();
	}
}

 

I got this class from a tutorial, but I'm trying to understand it and have a few questions:

 

What does super(timeout) do? I know super calls the parent class but what exactly does it do in this context?

 

What does the condition() method do in this class? It's an abstract method so it has to be implemented, but I don't see it being used for anything in this class, so why is return condition.getAsBoolean() necessary?

Link to comment
Share on other sites

 

23 minutes ago, Epos OSBot said:

What does super(timeout) do? I know super calls the parent class but what exactly does it do in this context?

As you correctly say, the `super` keyword is a means to access the parent class. Roughly speaking, to construct an instance of a child object (e.g. Sleep in this case), an instance of the parent class also must be constructed (ConditionalSleep).  If it did not do this, then how would you access the `sleep` method of ConditionalSleep? The `super` keyword when used in the constructor enables this: in this particular case, it will create an instance of the parent class with the `ConditionalSleep(int timeout)` constructor. It is clear that this is necessary as the ConditionalSleep class needs to know the duration of the sleep.

34 minutes ago, Epos OSBot said:

What does the condition() method do in this class? It's an abstract method so it has to be implemented, but I don't see it being used for anything in this class, so why is return condition.getAsBoolean() necessary?

The `condition` method is actually used by the ConditionalSleep class. As you again rightly say, it is abstract, so though ConditionalSleep refers to it, it does not provide a concrete implementation. It is for this reason that ConditionalSleeps are commonly defined anonymously, e.g.:

new ConditionalSleep(3000) {
  @Override
    public boolean condition() {
    return getCombat().isSpecialActivated();
  }
}.sleep();

This class which you show simply provides a concrete implementation for the condition method, where a BooleanSupplier (supplied during construction) provides the return value.

 

It looks like this class is essentially pointless, other than perhaps to enable succinct code by working with BooleanSuppliers rather than anonymous instantiations. If it's easier to understand for you, i'd stick with anonymous instances, or you could create your own concrete sleep classes, e.g.:

public class RunSleep extends ConditionalSleep {
  private final MethodProvider mp;

  public RunSleep(MethodProvider mp, int timeout) {
    super(timeout);
    this.mp = mp;
  }

  @Override
  public boolean condition() throws InterruptedException {
    return mp.getSettings().isRunning();
  }
}

Hopefully this is helpful, let me know if you're still stick or have any further questions, i'm always happy to help.

Good luck!

-Apa

  • Like 1
Link to comment
Share on other sites

@Apaec 

Would it be possible to simply define and assign a variable for timeout the same way it's done for condition? So just define it and assign it in the constructor. You're able to use sleep() because the class extends ConditionalSleep, right? I'm still not quite sure what super(timeout) is for. Why not this.timeout = timeout; as done with condition?

Link to comment
Share on other sites

1 hour ago, Epos OSBot said:

@Apaec 

Would it be possible to simply define and assign a variable for timeout the same way it's done for condition? So just define it and assign it in the constructor. You're able to use sleep() because the class extends ConditionalSleep, right? I'm still not quite sure what super(timeout) is for. Why not this.timeout = timeout; as done with condition?

As you say, you're able to use the `sleep` method as the class extends ConditionalSleep, but you still need to construct an instance of the ConditionalSleep parent class for this to happen. In the same way, ConditionalSleep (which extends `Object`) has to construct an instance of Object.

Rather than trying to explain the details of this myself, I think it would be better to refer you to the Java documentation about inheritance, as this should answer your question while giving you wider understanding: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Apa

Edit: I'd recommend going through the whole of the tutorial I link, not just the page at that URL. It explains the concepts very nicely!

Edited by Apaec
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...