BrightSide Workbench Full Report + Source Code
Indicators.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.indicator;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.concurrent.locks.Lock;
24 import java.util.concurrent.locks.ReentrantLock;
25 import org.turro.string.Strings;
26 import org.turro.annotation.ElephantIndicator;
27 import org.turro.reflection.Instances;
28 import org.turro.string.Words;
29 
34 public class Indicators {
35 
36  /* Elephant Indicator */
37 
38  public static boolean hasDirectProcess(List<IndicatorVariable> variables) {
39  return variables.stream().anyMatch(v -> !v.isPreprocess());
40  }
41 
42  public static List<IndicatorVariable> getRankingVariables(String root) {
44  return ei != null ? ei.getAllRankingVariables(root): Collections.EMPTY_LIST;
45  }
46 
47  public static List<IndicatorVariable> getMatchingVariables(String root) {
49  return ei != null ? ei.getAllMatchingVariables(root): Collections.EMPTY_LIST;
50  }
51 
52  public static List<IndicatorVariable> getRankingVariablesFinal(String root) {
53  List<IndicatorVariable> variables = getRankingVariables(root);
54  variables.add(0, IndicatorVariable.result(root, VariableType.RANKING_VARIABLE));
55  return variables;
56  }
57 
58  public static List<IndicatorVariable> getMatchingVariablesFinal(String root) {
59  List<IndicatorVariable> variables = getMatchingVariables(root);
60  variables.add(0, IndicatorVariable.result(root, VariableType.MATCHING_VARIABLE));
61  return variables;
62  }
63 
64  public static IndicatorVariable getVariable(String root, String name) {
66  return ei != null ? ei.getVariable(name) : null;
67  }
68 
69  public static IElephantIndicator getIndicator(String root) {
70  for(IElephantIndicator iIndicator : getIndicators()) {
71  if(iIndicator.itsMine(root)) {
72  return iIndicator;
73  }
74  }
75  return null;
76  }
77 
79  for(IElephantIndicator iIndicator : getIndicators()) {
80  if(iIndicator.itsMine(variable)) {
81  return iIndicator;
82  }
83  }
84  return null;
85  }
86 
87  public static double getValue(Object entity, IndicatorVariable variable) {
88  IElephantIndicator ei = getIndicator(variable);
89  return ei != null ? ei.getValue(variable, entity) : 0.0;
90  }
91 
92  public static double getValue(Object entity, String relatedPath, IndicatorVariable variable) {
93  IElephantIndicator ei = getIndicator(variable);
94  return ei != null ? ei.getValue(variable, entity, relatedPath) : 0.0;
95  }
96 
97  /* Reading indicators */
98 
99  public static IndicatorResult readIndicator(String indicator) {
100  if(!Strings.isBlank(indicator) && indicator.contains(":")) {
101  String v[] = indicator.split("\\s*:\\s*");
102  if(!Strings.isBlank(v[0]) && !Strings.isBlank(v[1]) && !Strings.isBlank(v[2])) {
103  for(IElephantIndicator iIndicator : getIndicators()) {
104  if(iIndicator.getClass().getSimpleName().equals(v[1])) {
105  return iIndicator.readIndicator(v[0], v[2]);
106  }
107  }
108  }
109  }
110  return new IndicatorResult(0, 0);
111  }
112 
113  public static IndicatorResult readIndicator(String jpaClass, String indicatorClass,
114  String entityPath, String relatedPath, String indicator) {
115  for(IElephantIndicator iIndicator : getIndicators()) {
116  if(iIndicator.getClass().getSimpleName().equals(indicatorClass)) {
117  return iIndicator.readIndicator(jpaClass,
118  Words.instance(".", entityPath, relatedPath, indicator).toString());
119  }
120  }
121  return new IndicatorResult(0, 0);
122  }
123 
124  public static List<IndicatorVariable> getVariables(String indicatorClass) {
125  for(IElephantIndicator iIndicator : getIndicators()) {
126  if(iIndicator.getClass().getSimpleName().equals(indicatorClass)) {
127  return iIndicator.getAllVariables();
128  }
129  }
130  return Collections.EMPTY_LIST;
131  }
132 
133  /* Initiated indicators */
134 
135  private static List<IElephantIndicator> _indicators;
136  private static final Lock indicatorLock = new ReentrantLock();
137 
138  public static List<IElephantIndicator> getIndicators() {
139  if(_indicators == null) {
140  indicatorLock.lock();
141  try {
142  if(_indicators == null) {
143  _indicators = Instances.cached().byAnnotation(ElephantIndicator.class, IElephantIndicator.class);
144  _indicators.stream().forEach(i -> i.initIndicator());
145  _indicators.stream().forEach(i -> i.prepareIndicator());
146  }
147  } finally {
148  indicatorLock.unlock();
149  }
150  }
151  return _indicators;
152  }
153 
154  private Indicators() {
155  }
156 
157 }
static IndicatorVariable result(String root, VariableType type)
static List< IndicatorVariable > getVariables(String indicatorClass)
static boolean hasDirectProcess(List< IndicatorVariable > variables)
Definition: Indicators.java:38
static double getValue(Object entity, IndicatorVariable variable)
Definition: Indicators.java:87
static double getValue(Object entity, String relatedPath, IndicatorVariable variable)
Definition: Indicators.java:92
static IndicatorResult readIndicator(String jpaClass, String indicatorClass, String entityPath, String relatedPath, String indicator)
static List< IndicatorVariable > getRankingVariables(String root)
Definition: Indicators.java:42
static List< IElephantIndicator > getIndicators()
static IndicatorResult readIndicator(String indicator)
Definition: Indicators.java:99
static IElephantIndicator getIndicator(IndicatorVariable variable)
Definition: Indicators.java:78
static List< IndicatorVariable > getMatchingVariables(String root)
Definition: Indicators.java:47
static IElephantIndicator getIndicator(String root)
Definition: Indicators.java:69
static List< IndicatorVariable > getMatchingVariablesFinal(String root)
Definition: Indicators.java:58
static List< IndicatorVariable > getRankingVariablesFinal(String root)
Definition: Indicators.java:52
static IndicatorVariable getVariable(String root, String name)
Definition: Indicators.java:64
IndicatorVariable getVariable(String name)
List< IndicatorVariable > getAllRankingVariables(String root)
List< IndicatorVariable > getAllMatchingVariables(String root)
double getValue(IndicatorVariable variable, Object entity)