Epos OSBot Posted June 9, 2020 Share Posted June 9, 2020 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? Quote Link to comment Share on other sites More sharing options...
Apaec Posted June 9, 2020 Share Posted June 9, 2020 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 1 Quote Link to comment Share on other sites More sharing options...
Epos OSBot Posted June 9, 2020 Author Share Posted June 9, 2020 @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? Quote Link to comment Share on other sites More sharing options...
Apaec Posted June 9, 2020 Share Posted June 9, 2020 (edited) 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 June 9, 2020 by Apaec 1 Quote Link to comment Share on other sites More sharing options...
ExtraBotz Posted June 9, 2020 Share Posted June 9, 2020 I made a video talking about OSBot sleeps and I cover this class. I start talking about it at 12:11 1 Quote Link to comment Share on other sites More sharing options...
dreameo Posted June 9, 2020 Share Posted June 9, 2020 tldr: This is all a conditional sleep does: while(timeRunningFor < timeout && !myBooleanCondition){ Thread.sleep(some_small_value_in_ms); } 1 Quote Link to comment Share on other sites More sharing options...
Epos OSBot Posted June 9, 2020 Author Share Posted June 9, 2020 @ExtraBotz This was especially useful. Definitely cleared up some confusion. Quote Link to comment Share on other sites More sharing options...