Package: UpdateBlockTaskAggregator

UpdateBlockTaskAggregator

nameinstructionbranchcomplexitylinemethod
UpdateBlockTaskAggregator(List, BlockFactory, BlockFactory)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addBlock(Block)
M: 17 C: 16
48%
M: 8 C: 2
20%
M: 5 C: 1
17%
M: 1 C: 6
86%
M: 0 C: 1
100%
createReadTaskAggregator(List, BlockFactory)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$0(Block)
M: 7 C: 18
72%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 4
67%
M: 0 C: 1
100%
setMinimumGapSize(int)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
stream()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017 Eurotech and/or its affiliates and others
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: * Contributors:
10: * Eurotech
11: *******************************************************************************/
12:
13: package org.eclipse.kura.driver.block.task;
14:
15: import static java.util.Objects.requireNonNull;
16:
17: import java.util.ArrayList;
18: import java.util.List;
19: import java.util.stream.Collectors;
20: import java.util.stream.Stream;
21:
22: import org.eclipse.kura.driver.block.Block;
23: import org.eclipse.kura.driver.block.BlockFactory;
24: import org.eclipse.kura.driver.block.ProhibitedBlock;
25:
26: /**
27: * <p>
28: * Represents a specialized version of {@link BlockTaskAggregator} that supports the aggregation of tasks whose
29: * {@link Mode} is {@link Mode#UPDATE}. This class only supports the aggregation of tasks that are either in
30: * {@link Mode#WRITE} or {@link Mode#UPDATE} mode.
31: * </p>
32: * <p>
33: * <p>
34: * If a task whose {@link Mode} is {@link Mode#UPDATE} is found in the input block list, it will be assigned to two
35: * {@link ToplevelBlockTask} instances, both of them will contain the interval specified by the update task.
36: * The first one will operate in {@link Mode#READ}, the second one in {@link Mode#WRITE}.
37: * </p>
38: * <p>
39: * If the {@link ToplevelBlockTask} instances described above are executed in sequence, the {@link BlockTask#run()}
40: * method of the update task will be called twice, the first time the parent of the update task will be assigned to the
41: * {@link ToplevelBlockTask} in {@link Mode#READ} mode, the second time it will be assigned to the
42: * {@link ToplevelBlockTask} in {@link Mode#WRITE} mode.
43: * </p>
44: * <p>
45: * If a task that operates in {@link Mode#UPDATE} needs to be defined, the
46: * {@link UpdateBlockTask} class should be extended.
47: * </p>
48: * <p>
49: * If some tasks in {@link Mode#UPDATE} are found in the input block list, this class will perform two
50: * separate aggregation processes: one for the {@link ToplevelBlockTask} instances required to fetch the data needed for
51: * the read part of the read-update-write operations, and the other for the {@link ToplevelBlockTask} instances required
52: * for the write part.
53: * </p>
54: *
55: * @see ToplevelBlockTask
56: * @see BlockTask
57: */
58: public class UpdateBlockTaskAggregator extends BlockTaskAggregator {
59:
60: private final BlockTaskAggregator readTaskAggregator;
61:
62: /**
63: * Creates a new {@link UpdateBlockTaskAggregator} instance
64: *
65: * @param tasks
66: * the list of input tasks
67: * @param readTaskFactory
68: * a {@link BlockFactory} that will be used for creating the {@link ToplevelBlockTask} instances in
69: * {@link Mode#READ} mode
70: * @param writeTaskFactory
71: * a {@link BlockFactory} that will be used for creating the {@link ToplevelBlockTask} instances in
72: * {@link Mode#WRITE} mode
73: * @throws IllegalArgumentException
74: * if any task in {@link Mode#READ} is found in the input task list
75: */
76: public UpdateBlockTaskAggregator(List<Block> tasks, BlockFactory<ToplevelBlockTask> readTaskFactory,
77: BlockFactory<ToplevelBlockTask> writeTaskFactory) {
78: super(tasks, writeTaskFactory);
79: requireNonNull(readTaskFactory, "Read tasks factory cannot be null");
80: this.readTaskAggregator = createReadTaskAggregator(tasks, readTaskFactory);
81: }
82:
83: private static BlockTaskAggregator createReadTaskAggregator(List<Block> tasks,
84: BlockFactory<ToplevelBlockTask> readTaskFactory) {
85: ArrayList<Block> updateTasks = tasks.stream().filter(block -> {
86:• if (!(block instanceof BlockTask)) {
87: return false;
88: }
89: BlockTask task = (BlockTask) block;
90:• if (task.getMode() == Mode.READ) {
91: throw new IllegalArgumentException("Read task are not supported by UpdateBlockTaskAggregator");
92: }
93:• return task.getMode() == Mode.UPDATE;
94: }).collect(Collectors.toCollection(ArrayList::new));
95: return new BlockTaskAggregator(updateTasks, readTaskFactory);
96: }
97:
98: /**
99: * Sets the {@code minimumGapSize} that will be used for aggregating the {@link ToplevelBlockTask} tasks
100: * in {@link Mode#READ} mode, the {@link ToplevelBlockTask} instances in {@link Mode#WRITE} will always be
101: * aggregated with {@code minimumGapSize = 0}.
102: */
103: @Override
104: public void setMinimumGapSize(int minimumGapSize) {
105: this.readTaskAggregator.setMinimumGapSize(minimumGapSize);
106: }
107:
108: /**
109: * {@inheritDoc}
110: *
111: * @throws IllegalArgumentException
112: * if the provided task is in {@link Mode#READ} mode.
113: */
114: @Override
115: public void addBlock(Block block) {
116: boolean isBlockTask = block instanceof BlockTask;
117:• if (isBlockTask && ((BlockTask) block).getMode() == Mode.READ) {
118: throw new IllegalArgumentException("Read task are not supported by UpdateBlockTaskAggregator");
119: }
120: super.addBlock(block);
121:• if (block instanceof ProhibitedBlock || isBlockTask && ((BlockTask) block).getMode() == Mode.UPDATE) {
122: this.readTaskAggregator.addBlock(block);
123: }
124: }
125:
126: /**
127: * Returns a {@link Stream} yielding the result of the aggregation. The returned {@link Stream} will produce the
128: * {@link ToplevelBlockTask} instances in {@link Mode#READ} first and then the {@link ToplevelBlockTask} instances
129: * in {@link Mode#WRITE} mode.
130: */
131: @Override
132: public Stream<ToplevelBlockTask> stream() {
133: return Stream.concat(this.readTaskAggregator.stream(), super.stream());
134: }
135: }