]> git.basschouten.com Git - openhab-addons.git/blob
ec15a0f6743fffb70f723ebee38d152b8bca8d86
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.Queue;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.nanoleaf.internal.layout.Point2D;
24 import org.openhab.binding.nanoleaf.internal.layout.ShapeType;
25 import org.openhab.binding.nanoleaf.internal.model.PositionDatum;
26
27 /**
28  * Create the correct chape for a given shape type.
29  *
30  * @author Jørgen Austvik - Initial contribution
31  */
32 @NonNullByDefault
33 public class PanelFactory {
34
35     public static List<Panel> createPanels(List<PositionDatum> panels) {
36         List<Panel> result = new ArrayList<>(panels.size());
37         Deque<PositionDatum> panelStack = new ArrayDeque<>(panels);
38         while (!panelStack.isEmpty()) {
39             PositionDatum panel = panelStack.peek();
40             final ShapeType shapeType = ShapeType.valueOf(panel.getShapeType());
41             Panel shape = createPanel(shapeType, takeFirst(shapeType.getNumLightsPerShape(), panelStack));
42             result.add(shape);
43         }
44
45         return result;
46     }
47
48     /**
49      * Return the first n elements from the stack.
50      * 
51      * @param n The number of elements to return
52      * @param stack The stack top get elements from
53      * @return The first n elements of the stack.
54      */
55     private static <@NonNull T> List<@NonNull T> takeFirst(int n, Queue<T> queue) {
56         List<T> result = new ArrayList<>(n);
57         for (int i = 0; i < n; i++) {
58             var res = queue.poll();
59             if (res != null) {
60                 result.add(res);
61             }
62         }
63
64         return result;
65     }
66
67     private static Panel createPanel(ShapeType shapeType, List<PositionDatum> positionDatum) {
68         switch (shapeType.getDrawingAlgorithm()) {
69             case SQUARE:
70                 PositionDatum squareShape = positionDatum.get(0);
71                 Point2D pos1 = new Point2D(squareShape.getPosX(), squareShape.getPosY());
72                 return new Square(shapeType, squareShape.getPanelId(), pos1, squareShape.getOrientation());
73
74             case TRIANGLE:
75                 PositionDatum triangleShape = positionDatum.get(0);
76                 Point2D pos2 = new Point2D(triangleShape.getPosX(), triangleShape.getPosY());
77                 return new Triangle(shapeType, triangleShape.getPanelId(), pos2, triangleShape.getOrientation());
78
79             case HEXAGON:
80                 PositionDatum hexShape = positionDatum.get(0);
81                 Point2D pos3 = new Point2D(hexShape.getPosX(), hexShape.getPosY());
82                 return new Hexagon(shapeType, hexShape.getPanelId(), pos3, hexShape.getOrientation());
83
84             case CORNER:
85                 return new HexagonCorners(shapeType, positionDatum);
86
87             default:
88                 PositionDatum shape = positionDatum.get(0);
89                 Point2D pos4 = new Point2D(shape.getPosX(), shape.getPosY());
90                 return new Point(shapeType, shape.getPanelId(), pos4);
91         }
92     }
93 }