2 * Copyright (c) 2010-2022 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.Queue;
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;
28 * Create the correct chape for a given shape type.
30 * @author Jørgen Austvik - Initial contribution
33 public class PanelFactory {
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));
49 * Return the first n elements from the stack.
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.
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();
67 private static Panel createPanel(ShapeType shapeType, List<PositionDatum> positionDatum) {
68 switch (shapeType.getDrawingAlgorithm()) {
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());
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());
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());
85 return new SingleLine(shapeType, positionDatum);
88 return new HexagonCorners(shapeType, positionDatum);
91 PositionDatum noShape = positionDatum.get(0);
92 Point2D pos4 = new Point2D(noShape.getPosX(), noShape.getPosY());
93 return new NoDraw(shapeType, noShape.getPanelId(), pos4);
96 PositionDatum shape = positionDatum.get(0);
97 Point2D pos5 = new Point2D(shape.getPosX(), shape.getPosY());
98 return new Point(shapeType, shape.getPanelId(), pos5);