mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Add billing plans so different plans can be compared. Performance enhancements to charge calculations.
This commit is contained in:
@@ -955,4 +955,48 @@ public class CollectionUtils {
|
||||
return 0;
|
||||
return _arr.length;
|
||||
}
|
||||
|
||||
public static byte[] toByteArray(float[] _floats) {
|
||||
if ((_floats == null) || (_floats.length == 0))
|
||||
return null;
|
||||
ByteBuffer bb = ByteBuffer.allocate(_floats.length * 4);
|
||||
for (float f : _floats) {
|
||||
bb.putFloat(f);
|
||||
}
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
public static float[] toFloatArray(byte[] _bytes) {
|
||||
if ((_bytes == null) || (_bytes.length == 0))
|
||||
return null;
|
||||
int offset = 0;
|
||||
float[] floats = new float[_bytes.length/4];
|
||||
ByteBuffer bb = ByteBuffer.wrap(_bytes);
|
||||
while (bb.hasRemaining()) {
|
||||
floats[offset++] = bb.getFloat();
|
||||
}
|
||||
return floats;
|
||||
}
|
||||
|
||||
public static byte[] toByteArray(double[] _doubles) {
|
||||
if ((_doubles == null) || (_doubles.length == 0))
|
||||
return null;
|
||||
ByteBuffer bb = ByteBuffer.allocate(_doubles.length * 8);
|
||||
for (double d : _doubles) {
|
||||
bb.putDouble(d);
|
||||
}
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
public static double[] toDoubleArray(byte[] _bytes) {
|
||||
if ((_bytes == null) || (_bytes.length == 0))
|
||||
return null;
|
||||
int offset = 0;
|
||||
double[] doubles = new double[_bytes.length/8];
|
||||
ByteBuffer bb = ByteBuffer.wrap(_bytes);
|
||||
while (bb.hasRemaining()) {
|
||||
doubles[offset++] = bb.getDouble();
|
||||
}
|
||||
return doubles;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.lanternsoftware.util;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
@@ -224,6 +226,11 @@ public abstract class DateUtils {
|
||||
return String.format("%d years", iYears);
|
||||
}
|
||||
|
||||
public static int getDaysBetween(Date _start, Date _end, TimeZone _tz) {
|
||||
ZoneId tz = _tz.toZoneId();
|
||||
return (int)ChronoUnit.DAYS.between(_start.toInstant().atZone(tz).toLocalDate(), _end.toInstant().atZone(tz).toLocalDate());
|
||||
}
|
||||
|
||||
public static int getMonthsBetween(Date _dtStart, Date _dtEnd) {
|
||||
Calendar calStart = getGMTCalendar(_dtStart.getTime());
|
||||
Calendar calEnd = getGMTCalendar(_dtEnd.getTime());
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.lanternsoftware.util.mutable;
|
||||
|
||||
public class MutableDouble {
|
||||
private double value;
|
||||
|
||||
public MutableDouble() {
|
||||
}
|
||||
|
||||
public MutableDouble(double _value) {
|
||||
value = _value;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(double _value) {
|
||||
value = _value;
|
||||
}
|
||||
|
||||
public void add(double _value) {
|
||||
value += _value;
|
||||
}
|
||||
|
||||
public void subtract(double _value) {
|
||||
value -= _value;
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,14 @@ package com.lanternsoftware.util.servlet;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import freemarker.template.Configuration;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class FreemarkerServlet extends LanternServlet {
|
||||
@@ -48,6 +50,16 @@ public abstract class FreemarkerServlet extends LanternServlet {
|
||||
|
||||
protected static DaoEntity model(HttpServletRequest _req) {
|
||||
DaoEntity model = new DaoEntity("context", _req.getContextPath());
|
||||
String linkPrefix = "";
|
||||
String[] path = getPath(_req);
|
||||
if (path.length > 1) {
|
||||
StringBuilder prefix = new StringBuilder();
|
||||
for(int i=0; i<path.length-1; i++) {
|
||||
prefix.append("../");
|
||||
}
|
||||
linkPrefix = prefix.toString();
|
||||
}
|
||||
model.put("link_prefix", linkPrefix);
|
||||
model.put("css_version", "1.0.0");
|
||||
return model;
|
||||
}
|
||||
@@ -78,4 +90,46 @@ public abstract class FreemarkerServlet extends LanternServlet {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void ajaxRender(HttpServletResponse _rep, String _template, Map<String, Object> _templateModel) {
|
||||
ajaxRender(_rep, _template, _templateModel, null);
|
||||
}
|
||||
|
||||
protected void ajaxRender(HttpServletResponse _rep, String _templateName, Map<String, Object> _templateModel, Map<String, Object> _jsonRep) {
|
||||
ajaxHtml(_rep, FreemarkerUtil.render(getFreemarkerConfig(), _templateName, _templateModel), _jsonRep);
|
||||
|
||||
}
|
||||
|
||||
protected static void ajaxHtml(HttpServletResponse _rep, String _html) {
|
||||
ajaxHtml(_rep, _html, null);
|
||||
}
|
||||
|
||||
protected static void ajaxHtml(HttpServletResponse _rep, String _html, Map<String, Object> _model) {
|
||||
if (_model == null) {
|
||||
_model = new HashMap<>();
|
||||
}
|
||||
_model.put("html", _html);
|
||||
ajaxJson(_rep, _model);
|
||||
}
|
||||
|
||||
protected static void ajaxJson(HttpServletResponse _rep, Map<String, Object> _model) {
|
||||
DaoEntity json = new DaoEntity(_model);
|
||||
setResponseEntity(_rep, "application/json", DaoSerializer.toJson(json));
|
||||
}
|
||||
|
||||
protected void ajaxRedirect(HttpServletResponse _rep, String _url) {
|
||||
setResponseEntity(_rep, "application/json", DaoSerializer.toJson(new DaoEntity("redirect", _url)));
|
||||
}
|
||||
|
||||
protected void ajaxError(HttpServletResponse _rep, String _error) {
|
||||
ajaxError(_rep, _error, null);
|
||||
}
|
||||
|
||||
protected void ajaxError(HttpServletResponse _rep, String _error, DaoEntity _model) {
|
||||
if (_model == null) {
|
||||
_model = new DaoEntity();
|
||||
}
|
||||
_model.put("error", _error);
|
||||
setResponseEntity(_rep, "application/json", DaoSerializer.toJson(_model, false, false));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user