From 24208404a73c6768501ba3bf45302587a14978a3 Mon Sep 17 00:00:00 2001 From: Yordan Mihaylov Date: Tue, 3 May 2016 14:46:44 +0300 Subject: [PATCH] Right behavior of getByTags(Set tags) method #1413 Signed-off-by: Yordan Mihaylov --- .../core/internal/RuleEngineTest.java | 33 +++++++++++++++++++ .../automation/core/internal/RuleEngine.java | 11 +++---- .../internal/template/TemplateManager.java | 12 +++---- .../core/internal/type/ModuleTypeManager.java | 10 ++---- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java b/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java index daa5fbe4f2e..a1b17515efb 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java @@ -147,6 +147,39 @@ public void testRuleTags() { Assert.assertTrue("Missing tag in rule", rule2GetTags.contains("tag2")); } + /** + * test get rules by tags + */ + @Test + public void testGetRuleByTags() { + RuleEngine ruleEngine = createRuleEngine(); + + Rule rule1 = new Rule("rule1", null, null, null, null, null); + Set ruleTags = new LinkedHashSet(); + ruleTags.add("tag1"); + rule1.setTags(ruleTags); + ruleEngine.addRule(rule1, true); + + Rule rule2 = new Rule("rule2", null, null, null, null, null); + Set ruleTags2 = new LinkedHashSet(); + ruleTags2.add("tag1"); + ruleTags2.add("tag2"); + rule2.setTags(ruleTags2); + ruleEngine.addRule(rule2, true); + + Collection rules = ruleEngine.getRulesByTags(null); + Assert.assertNotNull("Cannot find rule by set of tags: ", rules); + Assert.assertEquals("Collection of rules doesn't contains both rules.", 2, rules.size()); + + rules = ruleEngine.getRulesByTags(ruleTags); + Assert.assertNotNull("Cannot find rule by set of tags: ", rules); + Assert.assertEquals("Collection of rules doesn't contains both rules.", 2, rules.size()); + + rules = ruleEngine.getRulesByTags(ruleTags2); + Assert.assertNotNull("Cannot find rule by set of tags: ", rules); + Assert.assertEquals("Collection of rules has to contain only rule2.", 1, rules.size()); + } + /** * test rule configurations with null */ diff --git a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java index 9ae05fe02e0..be3ad75db6d 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java @@ -782,13 +782,9 @@ public synchronized Collection getRulesByTags(Set tags) { RuntimeRule r = it.next(); if (tags != null) { Set rTags = r.getTags(); - if (tags != null) { - for (Iterator i = rTags.iterator(); i.hasNext();) { - String tag = i.next(); - if (tags.contains(tag)) { - result.add(r.getRuleCopy()); - break; - } + if (rTags != null) { + if (rTags.containsAll(tags)) { + result.add(r.getRuleCopy()); } } } else { @@ -796,6 +792,7 @@ public synchronized Collection getRulesByTags(Set tags) { } } return result; + } /** diff --git a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java index d5a15b4037e..e5ff64865c4 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java @@ -99,14 +99,10 @@ public Collection getByTags(Set tags, Locale loc for (Iterator it = templates.iterator(); it.hasNext();) { T t = it.next(); if (tags != null) { - Collection rTags = t.getTags(); - if (rTags != null) { - for (Iterator itt = rTags.iterator(); itt.hasNext();) { - String tag = itt.next(); - if (tags.contains(tag)) { - result.add(t); - break; - } + Collection tTags = t.getTags(); + if (tTags != null) { + if (tTags.containsAll(tags)) { + result.add(t); } } } else { diff --git a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java index 50774ed45ce..7d673991d65 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java @@ -116,13 +116,9 @@ public Collection getByTags(Set tags, Locale l for (Iterator it = moduleTypes.iterator(); it.hasNext();) { ModuleType mt = it.next(); if (tags != null) { - Collection rTags = mt.getTags(); - for (Iterator itt = rTags.iterator(); itt.hasNext();) { - String tag = itt.next(); - if (tags.contains(tag)) { - result.add((T) createCopy(mt)); - break; - } + Collection mtTags = mt.getTags(); + if (mtTags.containsAll(tags)) { + result.add((T) createCopy(mt)); } } else { result.add((T) createCopy(mt));