From b166a015b61d1006aafbd33fea630d18b3ddfdf8 Mon Sep 17 00:00:00 2001 From: Bengt Martensson Date: Tue, 29 Oct 2024 16:39:16 +0100 Subject: [PATCH] New functions [Protocol,ParameterSpecs].addDefaulteds(map). --- .../java/org/harctoolbox/irp/ParameterSpecs.java | 11 +++++++++++ src/main/java/org/harctoolbox/irp/Protocol.java | 4 ++++ .../org/harctoolbox/irp/ParameterSpecsNGTest.java | 15 +++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/main/java/org/harctoolbox/irp/ParameterSpecs.java b/src/main/java/org/harctoolbox/irp/ParameterSpecs.java index 5b55e91c..536c8e5f 100644 --- a/src/main/java/org/harctoolbox/irp/ParameterSpecs.java +++ b/src/main/java/org/harctoolbox/irp/ParameterSpecs.java @@ -247,6 +247,17 @@ public void removeDefaulteds(Map namesMap) { } }); } + + public void addDefaulteds(Mapparams) { + NameEngine nameEngine = new NameEngine(params); + map.values().stream().filter(parameter -> (parameter.hasDefault() && !params.containsKey(parameter.getName()))).forEachOrdered(parameter -> { + try { + params.put(parameter.getName(), parameter.getDefault().toLong(nameEngine)); + } catch (NameUnassignedException ex) { + // Just ignore it + } + }); + } public void removeNotInParameterSpec(Map namesMap) { List names = new ArrayList<>(namesMap.keySet()); diff --git a/src/main/java/org/harctoolbox/irp/Protocol.java b/src/main/java/org/harctoolbox/irp/Protocol.java index caf3a486..7161c533 100644 --- a/src/main/java/org/harctoolbox/irp/Protocol.java +++ b/src/main/java/org/harctoolbox/irp/Protocol.java @@ -950,6 +950,10 @@ public long getParameterMin(String parameterName) { public void removeDefaulteds(Map params) { getParameterSpecs().removeDefaulteds(params); } + + public void addDefaulteds(Map params) { + getParameterSpecs().addDefaulteds(params); + } public boolean hasNonStandardParameters() { return getParameterSpecs().hasNonStandardParameters(); diff --git a/src/test/java/org/harctoolbox/irp/ParameterSpecsNGTest.java b/src/test/java/org/harctoolbox/irp/ParameterSpecsNGTest.java index 2d958c3d..aa52bf1d 100644 --- a/src/test/java/org/harctoolbox/irp/ParameterSpecsNGTest.java +++ b/src/test/java/org/harctoolbox/irp/ParameterSpecsNGTest.java @@ -110,4 +110,19 @@ public void testSort() { String[] result = sortedMap.keySet().toArray(new String[5]); assertEquals(result, expResult); } + + /** + * Test of addDefaulteds method, of class ParameterSpecs. + * @throws NameUnassignedException + */ + @Test + public void testAddDefaulteds() throws NameUnassignedException { + System.out.println("addDefaulteds"); + Map params = new HashMap<>(4); + params.put("D", 200L); + ParameterSpecs instance = nec1; + instance.addDefaulteds(params); + assertEquals(params.size(), 2); + assertEquals(params.get("S").longValue(), 55L); + } }