]> git.basschouten.com Git - openhab-addons.git/blob
009bd59ce730ddd7bdda0db4cce6b6fc6130a523
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nanoleaf.internal.layout.shape;
14
15 import java.util.ArrayDeque;
16 import java.util.ArrayList;
17 import java.util.Deque;
18 import java.util.List;
19 import java.util.Objects;
20 import java.util.Queue;
21
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.nanoleaf.internal.layout.Point2D;
25 import org.openhab.binding.nanoleaf.internal.layout.ShapeType;
26 import org.openhab.binding.nanoleaf.internal.model.PositionDatum;
27
28 /**
29  * Create the correct chape for a given shape type.
30  *
31  * @author Jørgen Austvik - Initial contribution
32  */
33 @NonNullByDefault
34 public class PanelFactory {
35
36     public static List<Panel> createPanels(List<PositionDatum> panels) {
37         List<Panel> result = new ArrayList<>(panels.size());
38         Deque<PositionDatum> panelStack = new ArrayDeque<>(panels);
39         while (!panelStack.isEmpty()) {
40             PositionDatum panel = Objects.requireNonNull(panelStack.peek());
41             final ShapeType shapeType = ShapeType.valueOf(panel.getShapeType());
42             Panel shape = createPanel(shapeType, takeFirst(shapeType.getNumLightsPerShape(), panelStack));
43             result.add(shape);
44         }
45
46         return result;
47     }
48
49     /**
50      * Return the first n elements from the stack.
51      * 
52      * @param n The number of elements to return
53      * @param stack The stack top get elements from
54      * @return The first n elements of the stack.
55      */
56     private static <@NonNull T> List<@NonNull T> takeFirst(int n, Queue<T> queue) {
57         List<T> result = new ArrayList<>(n);
58         for (int i = 0; i < n; i++) {
59             var res = queue.poll();
60             if (res != null) {
61                 result.add(res);
62             }
63         }
64
65         return result;
66     }
67
68     private static Panel createPanel(ShapeType shapeType, List<PositionDatum> positionDatum) {
69         switch (shapeType.getDrawingAlgorithm()) {
70             case SQUARE:
71                 PositionDatum squareShape = positionDatum.get(0);
72                 Point2D pos1 = new Point2D(squareShape.getPosX(), squareShape.getPosY());
73                 return new Square(shapeType, squareShape.getPanelId(), pos1, squareShape.getOrientation());
74
75             case TRIANGLE:
76                 PositionDatum triangleShape = positionDatum.get(0);
77                 Point2D pos2 = new Point2D(triangleShape.getPosX(), triangleShape.getPosY());
78                 return new Triangle(shapeType, triangleShape.getPanelId(), pos2, triangleShape.getOrientation());
79
80             case HEXAGON:
81                 PositionDatum hexShape = positionDatum.get(0);
82                 Point2D pos3 = new Point2D(hexShape.getPosX(), hexShape.getPosY());
83                 return new Hexagon(shapeType, hexShape.getPanelId(), pos3, hexShape.getOrientation());
84
85             case LINE:
86                 return new SingleLine(shapeType, positionDatum);
87
88             case CORNER:
89                 return new HexagonCorners(shapeType, positionDatum);
90
91             case NONE:
92                 PositionDatum noShape = positionDatum.get(0);
93                 Point2D pos4 = new Point2D(noShape.getPosX(), noShape.getPosY());
94                 return new NoDraw(shapeType, noShape.getPanelId(), pos4);
95
96             default:
97                 PositionDatum shape = positionDatum.get(0);
98                 Point2D pos5 = new Point2D(shape.getPosX(), shape.getPosY());
99                 return new Point(shapeType, shape.getPanelId(), pos5);
100         }
101     }
102 }