Skip to content

Commit 91cdfc8

Browse files
authored
Merge pull request #65 from Zavster/fix6
Minor enhancements to http client
2 parents 989e8e1 + a54f880 commit 91cdfc8

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/main/java/com/zavtech/morpheus/reference/XDataFrameSmooth.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ public DataFrame<R,C> sma(double windowSize) {
5252
@Override
5353
public DataFrame<R,C> ema(double halfLife) {
5454
try {
55-
if (!inPlace) {
55+
if (halfLife < 0) {
56+
throw new IllegalArgumentException("Half-life for smoothing must be >= 0, " + halfLife + " is illegal");
57+
} else if (halfLife == 0d) {
58+
return frame;
59+
} else if (!inPlace) {
5660
return frame.copy().smooth(true).ema(halfLife);
5761
} else {
5862
final int rowCount = frame.rows().count();
5963
if (rowCount > 0) {
6064
final int colCount = frame.cols().count();
61-
for (int colIndex=0; colIndex<colCount; ++colIndex) {
65+
for (int colIndex = 0; colIndex < colCount; ++colIndex) {
6266
final double value = frame.data().getDouble(0, colIndex);
6367
frame.data().setDouble(0, colIndex, value);
6468
}
@@ -68,7 +72,7 @@ public DataFrame<R,C> ema(double halfLife) {
6872
if (rowOrdinal > 0) {
6973
row.forEachValue(v -> {
7074
final double rawValue = v.getDouble();
71-
final double emaPrior = v.col().getDouble(rowOrdinal-1);
75+
final double emaPrior = v.col().getDouble(rowOrdinal - 1);
7276
final double emaValue = rawValue * alpha + (1d - alpha) * emaPrior;
7377
v.setDouble(emaValue);
7478
});
@@ -77,7 +81,7 @@ public DataFrame<R,C> ema(double halfLife) {
7781
}
7882
return frame;
7983
}
80-
} catch (DataFrameException ex) {
84+
} catch (IllegalArgumentException ex) {
8185
throw ex;
8286
} catch (Exception ex) {
8387
throw new DataFrameException("Failed to apply EWMA smoothing to DataFrame", ex);

src/main/java/com/zavtech/morpheus/util/http/HttpClient.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Optional;
2727
import java.util.function.Consumer;
28+
import java.util.zip.GZIPInputStream;
2829

2930
import com.zavtech.morpheus.util.IO;
3031
import com.zavtech.morpheus.util.Initialiser;
@@ -217,14 +218,36 @@ private static class DefaultResponse implements HttpResponse {
217218
});
218219
}
219220

221+
/**
222+
* Returns the content encoding for response
223+
* @return the content encoding
224+
*/
225+
private Optional<String> getContentEncoding() {
226+
for (HttpHeader header : headers) {
227+
if (header.getKey().equals("Content-Encoding")) {
228+
return Optional.ofNullable(header.getValue());
229+
}
230+
}
231+
return Optional.empty();
232+
}
233+
220234
@Override
221235
public HttpStatus getStatus() {
222236
return status;
223237
}
224238

225239
@Override
226240
public InputStream getStream() {
227-
return stream;
241+
try {
242+
final String encoding = getContentEncoding().orElse("default").toLowerCase();
243+
if (encoding.equalsIgnoreCase("gzip")) {
244+
return new GZIPInputStream(stream);
245+
} else {
246+
return stream;
247+
}
248+
} catch (Exception ex) {
249+
throw new RuntimeException("Failed to read from input stream", ex);
250+
}
228251
}
229252

230253
@Override

src/main/java/com/zavtech/morpheus/util/text/SmartFormat.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ public class SmartFormat extends java.text.Format {
6161
private Matcher timeMatcher1 = Pattern.compile(TIME_REGEX_1).matcher("");
6262
private Matcher timeMatcher2 = Pattern.compile(TIME_REGEX_2).matcher("");
6363

64-
private Format decimalFormat1 = new DecimalFormat("0.0000####;-0.0000####");
65-
private Format decimalFormat2 = new DecimalFormat("0.0000####;-0.0000####");
66-
private Format decimalFormat6 = new DecimalFormat("0.0000####;-0.0000####");
64+
private Format billionsFormat = new DecimalFormat("0.0000'B';-0.0000'B'");
65+
private Format millionsFormat = new DecimalFormat("0.0000'M';-0.0000'M'");
66+
private Format thousandFormat = new DecimalFormat("0.0000'K';-0.0000'K'");
67+
private Format decimalFormat = new DecimalFormat("###,###,##0.0000####;-###,###,##0.0000####");
68+
6769
private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
6870
private DateFormat dateFormat2 = new SimpleDateFormat("dd-MM-yyyy");
6971
private DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy");
@@ -105,11 +107,17 @@ public StringBuffer format(Object value, StringBuffer buffer, FieldPosition posi
105107
return buffer;
106108
} else if (value instanceof Double) {
107109
final Double doubleValue = (Double)value;
108-
if (Double.isNaN(doubleValue)) buffer.append("NaN");
109-
else if (doubleValue < 0.001d) buffer.append(decimalFormat1.format(value));
110-
else if (doubleValue < 0d) buffer.append(decimalFormat6.format(value));
111-
else if (doubleValue > 1000000d) buffer.append(decimalFormat1.format(value));
112-
else buffer.append(decimalFormat2.format(value));
110+
if (Double.isNaN(doubleValue)) {
111+
buffer.append("NaN");
112+
} else if (Math.abs(doubleValue) > 1000000000d) {
113+
buffer.append(billionsFormat.format(doubleValue / 1000000000d));
114+
} else if (Math.abs(doubleValue) > 1000000d) {
115+
buffer.append(millionsFormat.format(doubleValue / 1000000d));
116+
} else if (Math.abs(doubleValue) > 1000d) {
117+
buffer.append(thousandFormat.format(doubleValue / 1000d));
118+
} else {
119+
buffer.append(decimalFormat.format(doubleValue));
120+
}
113121
return buffer;
114122
} else if (value instanceof Boolean) {
115123
buffer.append(value.toString());

0 commit comments

Comments
 (0)