Permalink
Browse files

Code cleanup

  • Loading branch information...
mcicolella committed May 17, 2018
1 parent 7522617 commit 57a365cfb5ba413616984698c7049f9601f2692f
@@ -38,9 +38,9 @@
private static final long serialVersionUID = -5474545571527398625L;
private Trigger trigger = new Trigger();
//list of optional conditions
private List<Condition> conditions = new ArrayList<Condition>();
private List<Condition> conditions = new ArrayList<>();
private String uuid;
private List<Command> commands = new ArrayList<Command>();
private List<Command> commands = new ArrayList<>();
private String description;
private String shortDescription;
@@ -111,7 +111,7 @@ public Reaction(Trigger trigger) {
* @param command the command performed when the reaction is triggered
*/
public Reaction(Trigger trigger, Command command) {
ArrayList<Command> tmp = new ArrayList<Command>();
List<Command> tmp = new ArrayList<>();
tmp.add(command);
create(trigger, tmp);
}
@@ -159,7 +159,7 @@ public Trigger getTrigger() {
*/
public List<Command> getCommands() {
if (commands == null) {
setCommands(new ArrayList<Command>());
setCommands(new ArrayList<>());
}
return commands;
}
@@ -185,14 +185,14 @@ private String buildShortDescription() {
b.append("] ");
if ((conditions != null) && (!conditions.isEmpty())) {
for (Condition c : conditions) {
conditions.forEach((c) -> {
b.append(c.getStatement().getLogical())
.append(" [")
.append(c.getTarget()).append(" ")
.append(c.getStatement().getAttribute()).append(" ")
.append(c.getStatement().getOperand()).append(" ")
.append(c.getStatement().getValue()).append("] ");
}
});
}
b.append(" THEN ");
@@ -347,7 +347,8 @@ public String getUuid() {
}
/**
* Sets the reaction uuid. * @param uuid the uuid to set
* Sets the reaction uuid.
* @param uuid the uuid to set
*/
public void setUuid(String uuid) {
this.uuid = uuid;
@@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2009-2016 Freedomotic team http://freedomotic.com
* Copyright (c) 2009-2018 Freedomotic team http://freedomotic.com
*
* This file is part of Freedomotic
*
@@ -233,6 +233,7 @@ public void setPayload(Payload payload) {
//can be moved to a stategy pattern
/**
* Checks whether the trigger can fire.
*
* @return true if trigger can fire.
*/
public boolean canFire() {
@@ -251,7 +252,7 @@ public boolean canFire() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(wakeup);
if (LOG.isDebugEnabled()) {
LOG.debug("Trigger " + getName() + " is suspended until "
LOG.debug("Trigger \"" + getName() + "\" is suspended until "
+ formatter.format(calendar.getTime()));
}
//it is currently suspended
@@ -264,7 +265,8 @@ public boolean canFire() {
}
/**
* Increments the number of trigger executions and updates the suspensionStart.
* Increments the number of trigger executions and updates the
* suspensionStart.
*/
public synchronized void setExecuted() {
suspensionStart = System.currentTimeMillis();
@@ -273,6 +275,7 @@ public synchronized void setExecuted() {
/**
* Sets delay of the trigger.
*
* @param delay delay to set.
*/
public void setDelay(int delay) {
@@ -374,7 +377,7 @@ public int hashCode() {
}
/*
* Performs the Trigger check comparing the received event with itself (this
* Performs the trigger check comparing the received event with itself (this
* trigger)
*/
@Override
@@ -389,15 +392,15 @@ public void onMessage(ObjectMessage message) {
if (msgPayload instanceof EventTemplate) {
EventTemplate event = (EventTemplate) msgPayload;
LOG.debug("Trigger '" + this.getName() + "' filters event '" + event.getEventName()
+ "' on channel " + this.getChannel());
LOG.debug("Trigger \"" + this.getName() + "\" filters event \"" + event.getEventName()
+ "\" on channel \"" + this.getChannel() + "\"");
checker.check(event, this);
}
}
/**
* Clones the trigger.
*
* @return the cloned trigger.
*/
@Override
@@ -469,9 +472,9 @@ public void setPersistence(boolean persist) {
}
/**
* Gets whether the trigger should be persisted.
* Gets if the trigger should be persisted.
*
* @return true if trigger should be persisted
* @return true if trigger should be persisted, false otherwise
*/
public boolean isToPersist() {
return persistence;
@@ -51,7 +51,7 @@
class TriggerRepositoryImpl implements TriggerRepository {
private static final Logger LOG = LoggerFactory.getLogger(TriggerRepositoryImpl.class.getName());
private static ArrayList<Trigger> list = new ArrayList<>();
private static List<Trigger> list = new ArrayList<>();
private final DataUpgradeService dataUpgradeService;
private static final String TRIGGER_FILE_EXTENSION = ".xtrg";
@@ -82,7 +82,7 @@ public void saveTriggers(File folder) {
try {
LOG.info("Saving triggers to file into \"{}\"", folder.getAbsolutePath());
StringBuilder summaryContent = new StringBuilder();
for (Trigger trigger : list) {
list.stream().map((trigger) -> {
if (trigger.isToPersist()) {
String uuid = trigger.getUUID();
@@ -94,10 +94,11 @@ public void saveTriggers(File folder) {
File file = new File(folder + "/" + fileName);
FreedomXStream.toXML(trigger, file);
}
return trigger;
}).forEachOrdered((trigger) -> {
summaryContent.append(trigger.getUUID()).append("\t\t").append(trigger.getName()).append("\t\t\t")
.append(trigger.getChannel()).append("\n");
}
});
writeSummaryFile(new File(folder, "index.txt"), "#Filename \t\t #TriggerName \t\t\t #ListenedChannel\n", summaryContent.toString());
@@ -116,14 +117,14 @@ private static void deleteTriggerFiles(File folder) {
// this filter only returns triggers files
FileFilter objectFileFilter
= new FileFilter() {
public boolean accept(File file) {
if (file.isFile() && file.getName().endsWith(TRIGGER_FILE_EXTENSION)) {
return true;
} else {
return false;
}
}
};
public boolean accept(File file) {
if (file.isFile() && file.getName().endsWith(TRIGGER_FILE_EXTENSION)) {
return true;
} else {
return false;
}
}
};
files = folder.listFiles(objectFileFilter);
for (File file : files) {
file.delete();
@@ -149,15 +150,15 @@ public void loadTriggers(File folder) {
XStream xstream = FreedomXStream.getXstream();
// this filter only returns triggers files
FileFilter objectFileFileter
FileFilter objectFileFilter
= new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile() && file.getName().endsWith(TRIGGER_FILE_EXTENSION);
}
};
@Override
public boolean accept(File file) {
return file.isFile() && file.getName().endsWith(TRIGGER_FILE_EXTENSION);
}
};
File[] files = folder.listFiles(objectFileFileter);
File[] files = folder.listFiles(objectFileFilter);
try {
@@ -179,7 +180,7 @@ public boolean accept(File file) {
dataProperties.load(new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties")));
fromVersion = dataProperties.getProperty("data.version");
} catch (IOException iOException) {
LOG.error(Freedomotic.getStackTraceInfo(iOException));
LOG.error(Freedomotic.getStackTraceInfo(iOException));
// fallback to a default version for older version without that properties file
fromVersion = "5.5.0";
}
@@ -295,7 +296,6 @@ public static synchronized void remove(Trigger t) {
* @param name
* @return a trigger with the name (ignore-case) as the String in input
*/
@Deprecated
public static Trigger getTrigger(String name) {
if ((name == null) || (name.trim().isEmpty())) {
return null;
@@ -387,11 +387,9 @@ public static int size() {
@Override
public List<Trigger> findByName(String name) {
List<Trigger> tl = new ArrayList<>();
for (Trigger t : findAll()) {
if (t.getName().equalsIgnoreCase(name)) {
tl.add(t);
}
}
findAll().stream().filter((t) -> (t.getName().equalsIgnoreCase(name))).forEachOrdered((t) -> {
tl.add(t);
});
return tl;
}
@@ -479,9 +477,9 @@ public Trigger copy(Trigger trg) {
@Override
public void deleteAll() {
try {
for (Trigger t : findAll()) {
findAll().forEach((t) -> {
delete(t);
}
});
} catch (Exception e) {
} finally {
list.clear();
@@ -6,6 +6,6 @@
<property name="startup-time" value="on load"/>
<property name="automatic-reply-to-commands" value="false"/>
<property name="enable-i18n" value="true"/>
<property name="enable-sensors-widget" value="true"/>
<property name="enable-sensors-widget" value="false"/>
</properties>
</config>

0 comments on commit 57a365c

Please sign in to comment.