Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JLimit.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.user.test.configurations.limits;
2 
3 import com.griddynamics.jagger.user.test.configurations.limits.auxiliary.LowErrThresh;
4 import com.griddynamics.jagger.user.test.configurations.limits.auxiliary.LowWarnThresh;
5 import com.griddynamics.jagger.user.test.configurations.limits.auxiliary.UpWarnThresh;
6 import com.griddynamics.jagger.user.test.configurations.limits.auxiliary.UpErrThresh;
7 
8 import java.util.Objects;
9 
16 public abstract class JLimit {
17 
18  private final String metricId;
19  private final Double lowWarnThresh;
20  private final Double upperWarningThreshold;
21  private final Double lowerErrorThreshold;
22  private final Double upperErrorThreshold;
23 
24  JLimit(Builder builder) {
25  this.metricId = builder.metricId;
26  this.lowWarnThresh = builder.lowWarnThresh.value();
27  this.upperWarningThreshold = builder.upWarnThresh.value();
28  this.lowerErrorThreshold = builder.lowErrThresh.value();
29  this.upperErrorThreshold = builder.upErrThresh.value();
30  }
31 
32 
33  public abstract static class Builder {
34  String metricId;
35  LowWarnThresh lowWarnThresh = LowWarnThresh.of(1.0);
36  UpWarnThresh upWarnThresh = UpWarnThresh.of(1.0);
37  LowErrThresh lowErrThresh = LowErrThresh.of(1.0);
38  UpErrThresh upErrThresh = UpErrThresh.of(1.0);
39 
40  // I'm really sorry for that, but I have no idea how to do it better.
41  private boolean initialized;
42 
43 
51  public Builder withOnlyWarnings(LowWarnThresh lowWarnThresh, UpWarnThresh upWarnThresh) {
52  if (initialized) {
53  throw new IllegalArgumentException("It is already initialized with values: " +
54  this.lowWarnThresh + ", " + this.lowErrThresh + ", " + this.upWarnThresh + ", " + this.upErrThresh);
55  }
56  Objects.requireNonNull(lowWarnThresh);
57  Objects.requireNonNull(upWarnThresh);
58 
59  this.lowErrThresh = LowErrThresh.of(Double.NEGATIVE_INFINITY);
60  this.upErrThresh = UpErrThresh.of(Double.POSITIVE_INFINITY);
61  this.lowWarnThresh = lowWarnThresh;
62  this.upWarnThresh = upWarnThresh;
63 
64  initialized = true;
65 
66  return this;
67  }
68 
76  public Builder withOnlyErrors(LowErrThresh lowErrThresh, UpErrThresh upErrThresh) {
77  if (initialized) {
78  throw new IllegalArgumentException("It is already initialized with values: " +
79  this.lowWarnThresh + ", " + this.lowErrThresh + ", " + this.upWarnThresh + ", " + this.upErrThresh);
80  }
81  Objects.requireNonNull(lowErrThresh);
82  Objects.requireNonNull(upErrThresh);
83 
84  this.lowErrThresh = lowErrThresh;
85  this.upErrThresh = upErrThresh;
86  this.lowWarnThresh = LowWarnThresh.of(this.lowErrThresh.value());
87  this.upWarnThresh = UpWarnThresh.of(this.upErrThresh.value());
88 
89  initialized = true;
90 
91  return this;
92  }
93 
101  public Builder withOnlyUpperThresholds(UpWarnThresh upWarnThresh, UpErrThresh upErrThresh) {
102  if (initialized) {
103  throw new IllegalArgumentException("It is already initialized with values: " +
104  this.lowWarnThresh + ", " + this.lowErrThresh + ", " + this.upWarnThresh + ", " + this.upErrThresh);
105  }
106  Objects.requireNonNull(upWarnThresh);
107  Objects.requireNonNull(upErrThresh);
108 
109  this.lowErrThresh = LowErrThresh.of(Double.NEGATIVE_INFINITY);
110  this.upErrThresh = upErrThresh;
111  this.lowWarnThresh = LowWarnThresh.of(Double.NEGATIVE_INFINITY);
112  this.upWarnThresh = upWarnThresh;
113 
114  initialized = true;
115 
116  return this;
117  }
118 
126  public Builder withOnlyLowerThresholds(LowErrThresh lowErrThresh, LowWarnThresh lowWarnThresh) {
127  if (initialized) {
128  throw new IllegalArgumentException("It is already initialized with values: " +
129  this.lowWarnThresh + ", " + this.lowErrThresh + ", " + this.upWarnThresh + ", " + this.upErrThresh);
130  }
131  Objects.requireNonNull(lowWarnThresh);
132  Objects.requireNonNull(lowErrThresh);
133 
134  this.lowErrThresh = lowErrThresh;
135  this.upErrThresh = UpErrThresh.of(Double.POSITIVE_INFINITY);
136  this.lowWarnThresh = lowWarnThresh;
137  this.upWarnThresh = UpWarnThresh.of(Double.POSITIVE_INFINITY);
138 
139  initialized = true;
140 
141  return this;
142  }
143 
153  public Builder withExactLimits(LowErrThresh lowErrThresh, LowWarnThresh lowWarnThresh, UpWarnThresh upWarnThresh, UpErrThresh upErrThresh) {
154  if (initialized) {
155  throw new IllegalArgumentException("It is already initialized with values: " +
156  this.lowErrThresh + ", " + this.lowWarnThresh + ", " + this.upWarnThresh + ", " + this.upErrThresh);
157  }
158  Objects.requireNonNull(lowWarnThresh);
159  Objects.requireNonNull(lowErrThresh);
160  Objects.requireNonNull(upWarnThresh);
161  Objects.requireNonNull(upErrThresh);
162 
163  this.lowErrThresh = lowErrThresh;
164  this.upErrThresh = upErrThresh;
165  this.lowWarnThresh = lowWarnThresh;
166  this.upWarnThresh = upWarnThresh;
167 
168  initialized = true;
169 
170  return this;
171  }
172 
179  public abstract JLimit build();
180 
181  }
182 
183  public String getMetricId() {
184  return metricId;
185  }
186 
187  public Double getLowWarnThresh() {
188  return lowWarnThresh;
189  }
190 
191  public Double getUpperWarningThreshold() {
192  return upperWarningThreshold;
193  }
194 
195  public Double getLowerErrorThreshold() {
196  return lowerErrorThreshold;
197  }
198 
199  public Double getUpperErrorThreshold() {
200  return upperErrorThreshold;
201  }
202 }