2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nanoleaf.internal.layout.shape;
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;
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;
29 * Create the correct chape for a given shape type.
31 * @author Jørgen Austvik - Initial contribution
34 public class PanelFactory {
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));
50 * Return the first n elements from the stack.
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.
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();
68 private static Panel createPanel(ShapeType shapeType, List<PositionDatum> positionDatum) {
69 switch (shapeType.getDrawingAlgorithm()) {
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());
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());
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());
86 return new SingleLine(shapeType, positionDatum);
89 return new HexagonCorners(shapeType, positionDatum);
92 PositionDatum noShape = positionDatum.get(0);
93 Point2D pos4 = new Point2D(noShape.getPosX(), noShape.getPosY());
94 return new NoDraw(shapeType, noShape.getPanelId(), pos4);
97 PositionDatum shape = positionDatum.get(0);
98 Point2D pos5 = new Point2D(shape.getPosX(), shape.getPosY());
99 return new Point(shapeType, shape.getPanelId(), pos5);