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