mirror of
https://github.com/SonarSource/sonarqube.git
synced 2024-09-13 15:45:52 +02:00
Fix Quality flaws about complexity
This commit is contained in:
parent
129d630f52
commit
15e9f443ee
@ -28,6 +28,8 @@ import java.util.Map;
|
||||
import javax.annotation.CheckForNull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static com.google.common.base.Strings.isNullOrEmpty;
|
||||
|
||||
/**
|
||||
* PLUGINS MUST NOT USE THIS CLASS, EXCEPT FOR UNIT TESTING.
|
||||
*
|
||||
@ -110,24 +112,25 @@ public class FieldDiffs implements Serializable {
|
||||
|
||||
public static FieldDiffs parse(@Nullable String s) {
|
||||
FieldDiffs diffs = new FieldDiffs();
|
||||
if (!Strings.isNullOrEmpty(s)) {
|
||||
Iterable<String> fields = FIELDS_SPLITTER.split(s);
|
||||
for (String field : fields) {
|
||||
String[] keyValues = field.split("=");
|
||||
if (keyValues.length == 2) {
|
||||
String[] values = keyValues[1].split("\\|");
|
||||
String oldValue = "";
|
||||
String newValue = "";
|
||||
if (values.length == 1) {
|
||||
newValue = Strings.nullToEmpty(values[0]);
|
||||
} else if (values.length == 2) {
|
||||
oldValue = Strings.nullToEmpty(values[0]);
|
||||
newValue = Strings.nullToEmpty(values[1]);
|
||||
}
|
||||
diffs.setDiff(keyValues[0], oldValue, newValue);
|
||||
} else {
|
||||
diffs.setDiff(keyValues[0], "", "");
|
||||
if (isNullOrEmpty(s)) {
|
||||
return diffs;
|
||||
}
|
||||
Iterable<String> fields = FIELDS_SPLITTER.split(s);
|
||||
for (String field : fields) {
|
||||
String[] keyValues = field.split("=");
|
||||
if (keyValues.length == 2) {
|
||||
String[] values = keyValues[1].split("\\|");
|
||||
String oldValue = "";
|
||||
String newValue = "";
|
||||
if (values.length == 1) {
|
||||
newValue = Strings.nullToEmpty(values[0]);
|
||||
} else if (values.length == 2) {
|
||||
oldValue = Strings.nullToEmpty(values[0]);
|
||||
newValue = Strings.nullToEmpty(values[1]);
|
||||
}
|
||||
diffs.setDiff(keyValues[0], oldValue, newValue);
|
||||
} else {
|
||||
diffs.setDiff(keyValues[0], "", "");
|
||||
}
|
||||
}
|
||||
return diffs;
|
||||
|
@ -246,31 +246,30 @@ public class Measure<G extends Serializable> implements Serializable {
|
||||
* For internal use.
|
||||
*/
|
||||
public G value() {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
switch (getMetric().getType()) {
|
||||
case BOOL:
|
||||
return value == null ? null : (G) Boolean.valueOf(Double.doubleToRawLongBits(value) != 0L);
|
||||
return (G) Boolean.valueOf(Double.doubleToRawLongBits(value) != 0L);
|
||||
case INT:
|
||||
case MILLISEC:
|
||||
case RATING:
|
||||
return value == null ? null : (G) Integer.valueOf(value.intValue());
|
||||
return (G) Integer.valueOf(value.intValue());
|
||||
case FLOAT:
|
||||
case PERCENT:
|
||||
return value == null ? null : (G) value;
|
||||
case STRING:
|
||||
case LEVEL:
|
||||
case DATA:
|
||||
case DISTRIB:
|
||||
return data == null ? null : (G) data;
|
||||
return (G) data;
|
||||
case WORK_DUR:
|
||||
return value == null ? null : (G) Long.valueOf(value.longValue());
|
||||
return (G) Long.valueOf(value.longValue());
|
||||
default:
|
||||
if (getMetric().isNumericType()) {
|
||||
return value == null ? null : (G) value;
|
||||
} else if (getMetric().isDataType()) {
|
||||
return data == null ? null : (G) data;
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported type :" + getMetric().getType());
|
||||
if (getMetric().isNumericType() || getMetric().isDataType()) {
|
||||
return (G) value;
|
||||
}
|
||||
throw new UnsupportedOperationException("Unsupported type :" + getMetric().getType());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
*/
|
||||
package org.sonar.api.utils.text;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
@ -214,37 +215,13 @@ public class JsonWriter {
|
||||
* @throws org.sonar.api.utils.text.WriterException on any failure
|
||||
*/
|
||||
public JsonWriter valueObject(@Nullable Object value) {
|
||||
|
||||
try {
|
||||
if (value == null) {
|
||||
stream.nullValue();
|
||||
} else {
|
||||
if (value instanceof String) {
|
||||
stream.value(serializeEmptyStrings ? (String) value : emptyToNull((String) value));
|
||||
} else if (value instanceof Number) {
|
||||
stream.value((Number) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
stream.value((Boolean) value);
|
||||
} else if (value instanceof Date) {
|
||||
valueDateTime((Date) value);
|
||||
} else if (value instanceof Enum) {
|
||||
stream.value(((Enum) value).name());
|
||||
} else if (value instanceof Map) {
|
||||
stream.beginObject();
|
||||
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
|
||||
stream.name(entry.getKey().toString());
|
||||
valueObject(entry.getValue());
|
||||
}
|
||||
stream.endObject();
|
||||
} else if (value instanceof Iterable) {
|
||||
stream.beginArray();
|
||||
for (Object o : (Iterable<Object>) value) {
|
||||
valueObject(o);
|
||||
}
|
||||
stream.endArray();
|
||||
} else {
|
||||
throw new IllegalArgumentException(getClass() + " does not support encoding of type: " + value.getClass());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
valueNonNullObject(value);
|
||||
return this;
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw e;
|
||||
@ -253,6 +230,35 @@ public class JsonWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private void valueNonNullObject(Object value) throws IOException {
|
||||
if (value instanceof String) {
|
||||
stream.value(serializeEmptyStrings ? (String) value : emptyToNull((String) value));
|
||||
} else if (value instanceof Number) {
|
||||
stream.value((Number) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
stream.value((Boolean) value);
|
||||
} else if (value instanceof Date) {
|
||||
valueDateTime((Date) value);
|
||||
} else if (value instanceof Enum) {
|
||||
stream.value(((Enum) value).name());
|
||||
} else if (value instanceof Map) {
|
||||
stream.beginObject();
|
||||
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
|
||||
stream.name(entry.getKey().toString());
|
||||
valueObject(entry.getValue());
|
||||
}
|
||||
stream.endObject();
|
||||
} else if (value instanceof Iterable) {
|
||||
stream.beginArray();
|
||||
for (Object o : (Iterable<Object>) value) {
|
||||
valueObject(o);
|
||||
}
|
||||
stream.endArray();
|
||||
} else {
|
||||
throw new IllegalArgumentException(getClass() + " does not support encoding of type: " + value.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a list of values in an array, for example:
|
||||
* <pre>
|
||||
@ -382,7 +388,7 @@ public class JsonWriter {
|
||||
}
|
||||
|
||||
private static IllegalStateException rethrow(Exception e) {
|
||||
throw new WriterException("Fail to write JSON", e);
|
||||
throw new WriterException("Fail to write JSON", e);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@ -198,36 +198,37 @@ public class DefaultIndex {
|
||||
|
||||
public Measure addMeasure(Resource resource, Measure measure) {
|
||||
Bucket bucket = getBucket(resource);
|
||||
if (bucket != null) {
|
||||
if (sensorStorage.isDeprecatedMetric(measure.getMetricKey())) {
|
||||
// Ignore deprecated metrics
|
||||
return measure;
|
||||
}
|
||||
org.sonar.api.batch.measure.Metric<?> metric = metricFinder.findByKey(measure.getMetricKey());
|
||||
if (metric == null) {
|
||||
throw new UnsupportedOperationException("Unknown metric: " + measure.getMetricKey());
|
||||
}
|
||||
DefaultMeasure<?> newMeasure;
|
||||
if (Boolean.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Boolean>().forMetric((Metric<Boolean>) metric)
|
||||
.withValue(measure.getValue() != 0.0);
|
||||
} else if (Integer.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Integer>().forMetric((Metric<Integer>) metric)
|
||||
.withValue(measure.getValue().intValue());
|
||||
} else if (Double.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Double>().forMetric((Metric<Double>) metric)
|
||||
.withValue(measure.getValue());
|
||||
} else if (String.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<String>().forMetric((Metric<String>) metric)
|
||||
.withValue(measure.getData());
|
||||
} else if (Long.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Long>().forMetric((Metric<Long>) metric)
|
||||
.withValue(measure.getValue().longValue());
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
|
||||
}
|
||||
sensorStorage.saveMeasure(componentCache.get(resource).inputComponent(), newMeasure);
|
||||
if (bucket == null) {
|
||||
return measure;
|
||||
}
|
||||
if (sensorStorage.isDeprecatedMetric(measure.getMetricKey())) {
|
||||
// Ignore deprecated metrics
|
||||
return measure;
|
||||
}
|
||||
org.sonar.api.batch.measure.Metric<?> metric = metricFinder.findByKey(measure.getMetricKey());
|
||||
if (metric == null) {
|
||||
throw new UnsupportedOperationException("Unknown metric: " + measure.getMetricKey());
|
||||
}
|
||||
DefaultMeasure<?> newMeasure;
|
||||
if (Boolean.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Boolean>().forMetric((Metric<Boolean>) metric)
|
||||
.withValue(measure.getValue() != 0.0);
|
||||
} else if (Integer.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Integer>().forMetric((Metric<Integer>) metric)
|
||||
.withValue(measure.getValue().intValue());
|
||||
} else if (Double.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Double>().forMetric((Metric<Double>) metric)
|
||||
.withValue(measure.getValue());
|
||||
} else if (String.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<String>().forMetric((Metric<String>) metric)
|
||||
.withValue(measure.getData());
|
||||
} else if (Long.class.equals(metric.valueType())) {
|
||||
newMeasure = new DefaultMeasure<Long>().forMetric((Metric<Long>) metric)
|
||||
.withValue(measure.getValue().longValue());
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
|
||||
}
|
||||
sensorStorage.saveMeasure(componentCache.get(resource).inputComponent(), newMeasure);
|
||||
return measure;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user