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 <[email protected]>
  • Loading branch information
jimtng committed Nov 28, 2024
1 parent 178df65 commit cccb8d0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/
public class EnrichedGroupItemDTO extends EnrichedItemDTO {

public EnrichedGroupItemDTO(ItemDTO itemDTO, EnrichedItemDTO[] members, String link, String state,
String previousState, Long lastUpdate, Long lastChange, String transformedState,
StateDescription stateDescription, String unitSymbol) {
super(itemDTO, link, state, previousState, lastUpdate, lastChange, transformedState, stateDescription, null,
unitSymbol);
public EnrichedGroupItemDTO(ItemDTO itemDTO, EnrichedItemDTO[] members, String link, String state, String lastState,
Long lastStateUpdate, Long lastStateChange, String transformedState, StateDescription stateDescription,
String unitSymbol) {
super(itemDTO, link, state, lastState, lastStateUpdate, lastStateChange, transformedState, stateDescription,
null, unitSymbol);
this.members = members;
this.groupType = ((GroupItemDTO) itemDTO).groupType;
this.function = ((GroupItemDTO) itemDTO).function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public class EnrichedItemDTO extends ItemDTO {
public String transformedState;
public StateDescription stateDescription;
public CommandDescription commandDescription;
public String previousState;
public Long lastUpdate;
public Long lastChange;
public String lastState;
public Long lastStateUpdate;
public Long lastStateChange;
public String unitSymbol;
public Map<String, Object> metadata;
public Boolean editable;

public EnrichedItemDTO(ItemDTO itemDTO, String link, String state, String previousState, Long lastUpdate,
Long lastChange, String transformedState, StateDescription stateDescription,
public EnrichedItemDTO(ItemDTO itemDTO, String link, String state, String lastState, Long lastStateUpdate,
Long lastStateChange, String transformedState, StateDescription stateDescription,
CommandDescription commandDescription, String unitSymbol) {
this.type = itemDTO.type;
this.name = itemDTO.name;
Expand All @@ -54,9 +54,9 @@ public EnrichedItemDTO(ItemDTO itemDTO, String link, String state, String previo
this.transformedState = transformedState;
this.stateDescription = stateDescription;
this.commandDescription = commandDescription;
this.previousState = previousState;
this.lastUpdate = lastUpdate;
this.lastChange = lastChange;
this.lastState = lastState;
this.lastStateUpdate = lastStateUpdate;
this.lastStateChange = lastStateChange;
this.unitSymbol = unitSymbol;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown
}
StateDescription stateDescription = considerTransformation(item.getStateDescription(locale));

String previousState = Optional.ofNullable(item.getPreviousState()).map(State::toFullString).orElse(null);
Long lastUpdate = Optional.ofNullable(item.getLastUpdate()).map(zdt -> zdt.toInstant().toEpochMilli())
String lastState = Optional.ofNullable(item.getLastState()).map(State::toFullString).orElse(null);
Long lastStateUpdate = Optional.ofNullable(item.getLastStateUpdate()).map(zdt -> zdt.toInstant().toEpochMilli())
.orElse(null);
Long lastChange = Optional.ofNullable(item.getLastChange()).map(zdt -> zdt.toInstant().toEpochMilli())
Long lastStateChange = Optional.ofNullable(item.getLastStateChange()).map(zdt -> zdt.toInstant().toEpochMilli())
.orElse(null);

final String link;
Expand Down Expand Up @@ -131,10 +131,10 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown
} else {
memberDTOs = new EnrichedItemDTO[0];
}
enrichedItemDTO = new EnrichedGroupItemDTO(itemDTO, memberDTOs, link, state, previousState, lastUpdate,
lastChange, transformedState, stateDescription, unitSymbol);
enrichedItemDTO = new EnrichedGroupItemDTO(itemDTO, memberDTOs, link, state, lastState, lastStateUpdate,
lastStateChange, transformedState, stateDescription, unitSymbol);
} else {
enrichedItemDTO = new EnrichedItemDTO(itemDTO, link, state, previousState, lastUpdate, lastChange,
enrichedItemDTO = new EnrichedItemDTO(itemDTO, link, state, lastState, lastStateUpdate, lastStateChange,
transformedState, stateDescription, item.getCommandDescription(locale), unitSymbol);
}

Expand Down
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 cccb8d0

Please sign in to comment.