Skip to content

Commit

Permalink
Rename to getLastStateChange, getLastStateUpdate, getLastState
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Nov 28, 2024
1 parent 178df65 commit 9beb230
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public abstract class GenericItem implements ActiveItem {
protected final String type;

protected State state = UnDefType.NULL;
protected @Nullable State previousState;
protected @Nullable State lastState;

protected @Nullable ZonedDateTime lastUpdate;
protected @Nullable ZonedDateTime lastChange;
protected @Nullable ZonedDateTime lastStateUpdate;
protected @Nullable ZonedDateTime lastStateChange;

protected @Nullable String label;

Expand Down Expand Up @@ -109,18 +109,18 @@ public State getState() {
}

@Override
public @Nullable State getPreviousState() {
return previousState;
public @Nullable State getLastState() {
return lastState;
}

@Override
public @Nullable ZonedDateTime getLastUpdate() {
return lastUpdate;
public @Nullable ZonedDateTime getLastStateUpdate() {
return lastStateUpdate;
}

@Override
public @Nullable ZonedDateTime getLastChange() {
return lastChange;
public @Nullable ZonedDateTime getLastStateChange() {
return lastStateChange;
}

@Override
Expand Down Expand Up @@ -243,15 +243,15 @@ protected final void applyState(State state) {
boolean stateChanged = !oldState.equals(state);
this.state = state;
if (stateChanged) {
previousState = oldState; // update before we notify listeners
lastState = oldState; // update before we notify listeners
}
notifyListeners(oldState, state);
sendStateUpdatedEvent(state);
if (stateChanged) {
sendStateChangedEvent(state, oldState);
lastChange = now; // update after we've notified listeners
lastStateChange = now; // update after we've notified listeners
}
lastUpdate = now;
lastStateUpdate = now;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ public interface Item extends Identifiable<String> {
* @return the previous state of the item, or null if the item has never been changed.
*/
@Nullable
State getPreviousState();
State getLastState();

/**
* Returns the time the item was last updated.
*
* @return the time the item was last updated, or null if the item has never been updated.
*/
@Nullable
ZonedDateTime getLastUpdate();
ZonedDateTime getLastStateUpdate();

/**
* Returns the time the item was last changed.
*
* @return the time the item was last changed, or null if the item has never been changed.
*/
@Nullable
ZonedDateTime getLastChange();
ZonedDateTime getLastStateChange();

/**
* returns the name of the item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,44 +138,44 @@ public void testGetStateAsWithNull() {
}

@Test
public void testGetLastUpdate() {
public void testGetLastStateUpdate() {
TestItem item = new TestItem("member1");
assertNull(item.getLastUpdate());
assertNull(item.getLastStateUpdate());
item.setState(PercentType.HUNDRED);
assertThat(item.getLastUpdate().toInstant().toEpochMilli() * 1.0,
assertThat(item.getLastStateUpdate().toInstant().toEpochMilli() * 1.0,
is(closeTo(ZonedDateTime.now().toInstant().toEpochMilli(), 5)));
}

@Test
public void testGetLastChange() throws InterruptedException {
public void testGetLastStateChange() throws InterruptedException {
TestItem item = new TestItem("member1");
assertNull(item.getLastChange());
assertNull(item.getLastStateChange());
item.setState(PercentType.HUNDRED);
ZonedDateTime initialChangeTime = ZonedDateTime.now();
assertThat(item.getLastChange().toInstant().toEpochMilli() * 1.0,
assertThat(item.getLastStateChange().toInstant().toEpochMilli() * 1.0,
is(closeTo(initialChangeTime.toInstant().toEpochMilli(), 5)));

Thread.sleep(50);
item.setState(PercentType.HUNDRED);
assertThat(item.getLastChange().toInstant().toEpochMilli() * 1.0,
assertThat(item.getLastStateChange().toInstant().toEpochMilli() * 1.0,
is(closeTo(initialChangeTime.toInstant().toEpochMilli(), 5)));

Thread.sleep(50);
ZonedDateTime secondChangeTime = ZonedDateTime.now();
item.setState(PercentType.ZERO);
assertThat(item.getLastChange().toInstant().toEpochMilli() * 1.0,
assertThat(item.getLastStateChange().toInstant().toEpochMilli() * 1.0,
is(closeTo(secondChangeTime.toInstant().toEpochMilli(), 5)));
}

@Test
public void testGetPreviousState() {
public void testGetLastState() {
TestItem item = new TestItem("member1");
assertEquals(UnDefType.NULL, item.getState());
assertNull(item.getPreviousState());
assertNull(item.getLastState());
item.setState(PercentType.HUNDRED);
assertEquals(UnDefType.NULL, item.getPreviousState());
assertEquals(UnDefType.NULL, item.getLastState());
item.setState(PercentType.ZERO);
assertEquals(PercentType.HUNDRED, item.getPreviousState());
assertEquals(PercentType.HUNDRED, item.getLastState());
}

@Test
Expand Down

0 comments on commit 9beb230

Please sign in to comment.