]> git.basschouten.com Git - openhab-addons.git/blob
36905aa0e26fb7868642353089f483f153bbea91
[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.neeo.internal.models;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19
20 /**
21  * The model representing Neeo Recipes (serialize/deserialize json use only).
22  *
23  * @author Tim Roberts - Initial contribution
24  */
25 @NonNullByDefault
26 public class NeeoRecipes {
27
28     /** The recipes. */
29     private NeeoRecipe[] recipes;
30
31     /**
32      * Creates the recipes from the given recipes
33      *
34      * @param recipes the recipes
35      */
36     NeeoRecipes(NeeoRecipe[] recipes) {
37         this.recipes = recipes;
38     }
39
40     /**
41      * Gets the recipes.
42      *
43      * @return the recipes
44      */
45     public NeeoRecipe[] getRecipes() {
46         return recipes;
47     }
48
49     /**
50      * Gets the recipe by key
51      *
52      * @param key the key
53      * @return the recipe or null if none found
54      */
55     @Nullable
56     public NeeoRecipe getRecipe(String key) {
57         if (key.isEmpty()) {
58             return null;
59         }
60
61         for (NeeoRecipe recipe : recipes) {
62             if (key.equalsIgnoreCase(recipe.getKey())) {
63                 return recipe;
64             }
65         }
66         return null;
67     }
68
69     /**
70      * Gets the recipe by a scenario key and recipe type
71      *
72      * @param key the key
73      * @param type the recipe type
74      * @return the recipe or null if none found
75      */
76     @Nullable
77     public NeeoRecipe getRecipeByScenarioKey(String key, String type) {
78         if (key.isEmpty()) {
79             return null;
80         }
81
82         for (NeeoRecipe recipe : recipes) {
83             if (key.equalsIgnoreCase(recipe.getScenarioKey()) && type.equalsIgnoreCase(recipe.getType())) {
84                 return recipe;
85             }
86         }
87         return null;
88     }
89
90     /**
91      * Gets the recipe by name
92      *
93      * @param name the recipe name
94      * @return the recipe or null if none found
95      */
96     @Nullable
97     public NeeoRecipe getRecipeByName(String name) {
98         if (name.isEmpty()) {
99             return null;
100         }
101
102         for (NeeoRecipe recipe : recipes) {
103             if (name.equalsIgnoreCase(recipe.getName())) {
104                 return recipe;
105             }
106         }
107         return null;
108     }
109
110     @Override
111     public String toString() {
112         return "NeeoRecipes [recipes=" + Arrays.toString(recipes) + "]";
113     }
114 }