Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

persistence functions should return Optional instead of null #51

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.settings/
bin/

*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule;
package org.openhab.automation.jrule.exception;

/**
* The {@link JRuleExecutionException} wraps underlying openHAB exceptions
Expand All @@ -21,4 +21,8 @@ public class JRuleExecutionException extends Exception {
public JRuleExecutionException(String message) {
super(message);
}

public JRuleExecutionException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.exception;

/**
* The {@link JRuleItemNotFoundException} rethrows internal ItemNotFoundException's
*
* @author Robert Delbrück
*/
public class JRuleItemNotFoundException extends JRuleRuntimeException {
public JRuleItemNotFoundException(String message) {
super(message);
}

public JRuleItemNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.exception;

/**
* The {@link JRuleRuntimeException} is for throwing internal runtime exceptions
*
* @author Robert Delbrück
*/
public class JRuleRuntimeException extends RuntimeException {
public JRuleRuntimeException(String message) {
super(message);
}

public JRuleRuntimeException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.openhab.automation.jrule.internal.handler;

import org.openhab.automation.jrule.JRuleExecutionException;
import org.openhab.automation.jrule.exception.JRuleExecutionException;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationHelper;
import org.osgi.framework.BundleContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;

/**
Expand All @@ -27,7 +29,7 @@ protected JRuleCallItem(String itemName) {
super(itemName);
}

public static JRuleCallItem forName(String itemName) {
public static JRuleCallItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleCallItem.class);
}

Expand All @@ -44,7 +46,7 @@ public void postUpdate(String value) {
}

// Persistence methods
public String getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId);
public Optional<String> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.rules.value.JRuleColorValue;
import org.openhab.automation.jrule.rules.value.JRuleIncreaseDecreaseValue;
Expand All @@ -30,7 +32,7 @@ protected JRuleColorItem(String itemName) {
super(itemName);
}

public static JRuleColorItem forName(String itemName) {
public static JRuleColorItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleColorItem.class);
}

Expand Down Expand Up @@ -74,7 +76,7 @@ public void postUpdate(int value) {
JRuleEventHandler.get().postUpdate(itemName, new JRulePercentType(value));
}

public String getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId);
public Optional<String> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.rules.value.JRuleOpenClosedValue;
import org.openhab.automation.jrule.trigger.JRuleContactTrigger;
Expand All @@ -29,7 +31,7 @@ protected JRuleContactItem(String itemName) {
super(itemName);
}

public static JRuleContactItem forName(String itemName) {
public static JRuleContactItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleContactItem.class);
}

Expand All @@ -38,8 +40,8 @@ public JRuleOpenClosedValue getState() {
}

// Persistence method
public JRuleOpenClosedValue getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRuleOpenClosedValue.getValueFromString(
JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId));
public Optional<JRuleOpenClosedValue> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId)
.map(JRuleOpenClosedValue::getValueFromString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;

/**
Expand All @@ -28,7 +30,7 @@ protected JRuleDateTimeItem(String itemName) {
super(itemName);
}

public static JRuleDateTimeItem forName(String itemName) {
public static JRuleDateTimeItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleDateTimeItem.class);
}

Expand Down Expand Up @@ -56,12 +58,8 @@ public ZonedDateTime getZonedDateTimeState() {
return JRuleEventHandler.get().getStateFromItemAsZonedDateTime(itemName);
}

public Date getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
String state = JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId);
if (state != null) {
return new Date(ZonedDateTime.parse(state).toInstant().toEpochMilli());
} else {
return null;
}
public Optional<ZonedDateTime> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId)
.map(ZonedDateTime::parse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.rules.value.JRuleIncreaseDecreaseValue;
import org.openhab.automation.jrule.rules.value.JRuleOnOffValue;
Expand All @@ -30,7 +32,7 @@ protected JRuleDimmerItem(String itemName) {
super(itemName);
}

public static JRuleDimmerItem forName(String itemName) {
public static JRuleDimmerItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleDimmerItem.class);
}

Expand Down Expand Up @@ -58,12 +60,8 @@ public void postUpdate(int value) {
JRuleEventHandler.get().postUpdate(itemName, new JRulePercentType(value));
}

public Integer getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
String state = JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId);
if (state != null) {
return Integer.parseInt(state);
} else {
return null;
}
public Optional<Integer> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId)
.map(Integer::parseInt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Optional;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;

/**
Expand All @@ -27,7 +29,7 @@ protected JRuleGroupCallItem(String itemName) {
super(itemName);
}

public static JRuleGroupCallItem forName(String itemName) {
public static JRuleGroupCallItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleGroupCallItem.class);
}

Expand All @@ -36,7 +38,7 @@ public String getState() {
}

// Persistence method
public String getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId);
public Optional<String> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.Set;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.rules.value.JRuleColorValue;
import org.openhab.automation.jrule.rules.value.JRuleIncreaseDecreaseValue;
Expand All @@ -31,7 +33,7 @@ protected JRuleGroupColorItem(String itemName) {
super(itemName);
}

public static JRuleGroupColorItem forName(String itemName) {
public static JRuleGroupColorItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleGroupColorItem.class);
}

Expand Down Expand Up @@ -83,7 +85,7 @@ public void postUpdate(int value) {
}

// Persistence method
public String getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId);
public Optional<String> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.Set;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.rules.value.JRuleOpenClosedValue;

Expand All @@ -29,7 +31,7 @@ protected JRuleGroupContactItem(String itemName) {
super(itemName);
}

public static JRuleGroupContactItem forName(String itemName) {
public static JRuleGroupContactItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleGroupContactItem.class);
}

Expand All @@ -48,8 +50,8 @@ public void postUpdate(JRuleOpenClosedValue value) {
}

// Persistence method
public JRuleOpenClosedValue getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRuleEventHandler.get().getOpenClosedValue(
JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId));
public Optional<JRuleOpenClosedValue> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId)
.map(s -> JRuleEventHandler.get().getOpenClosedValue(s));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
package org.openhab.automation.jrule.items;

import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Optional;
import java.util.Set;

import org.openhab.automation.jrule.exception.JRuleItemNotFoundException;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;

/**
Expand All @@ -29,27 +30,27 @@ protected JRuleGroupDateTimeItem(String itemName) {
super(itemName);
}

public static JRuleGroupDateTimeItem forName(String itemName) {
public static JRuleGroupDateTimeItem forName(String itemName) throws JRuleItemNotFoundException {
return JRuleItemRegistry.get(itemName, JRuleGroupDateTimeItem.class);
}

public Date getState() {
return JRuleEventHandler.get().getStateFromItemAsDate(itemName);
public ZonedDateTime getState() {
return JRuleEventHandler.get().getStateFromItemAsZonedDateTime(itemName);
}

public void sendCommand(Date value) {
public void sendCommand(ZonedDateTime value) {
final Set<String> groupMemberNames = JRuleEventHandler.get().getGroupMemberNames(itemName);
groupMemberNames.forEach(m -> JRuleEventHandler.get().sendCommand(m, value));
}

public void postUpdate(Date value) {
public void postUpdate(ZonedDateTime value) {
final Set<String> groupMemberNames = JRuleEventHandler.get().getGroupMemberNames(itemName);
groupMemberNames.forEach(m -> JRuleEventHandler.get().postUpdate(m, value));
}

// Persistence method
public String getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
// TODO should return as Date
return JRulePersistenceExtentions.historicState(itemName, timestamp, persistenceServiceId);
public Optional<ZonedDateTime> getHistoricState(ZonedDateTime timestamp, String persistenceServiceId) {
return JRulePersistenceExtensions.historicState(itemName, timestamp, persistenceServiceId)
.map(ZonedDateTime::parse);
}
}
Loading