More utilities and alterations to the progress bar system.

This commit is contained in:
Olof Larsson 2015-01-09 15:46:23 +01:00
parent b857df8763
commit d65323efb1
2 changed files with 190 additions and 15 deletions

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -63,13 +65,13 @@ public class Progressbar
public Progressbar withQuota(double quota) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withWidth(int width) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withLeft(String left) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar solid(String solid) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar between(String between) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar empty(String empty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar right(String right) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar solidsPerEmpty(double solidsPerEmpty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar colorTag(String colorTag) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar roofToColor(Map<Double, String> roofToColor) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withSolid(String solid) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withBetween(String between) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withEmpty(String empty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withRight(String right) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withSolidsPerEmpty(double solidsPerEmpty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withColorTag(String colorTag) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
public Progressbar withRoofToColor(Map<Double, String> roofToColor) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); }
// -------------------------------------------- //
// PRIVATE CONSTRUCTOR
@ -106,13 +108,26 @@ public class Progressbar
{
return render(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor);
}
public List<String> renderList()
{
return renderList(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor);
}
// -------------------------------------------- //
// STATIC UTIL
// -------------------------------------------- //
public static String render(double quota, int width, String left, String solid, String between, String empty, String right, double solidsPerEmpty, String colorTag, Map<Double, String> roofToColor)
{
return Txt.implode(renderList(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor), "");
}
public static List<String> renderList(double quota, int width, String left, String solid, String between, String empty, String right, double solidsPerEmpty, String colorTag, Map<Double, String> roofToColor)
{
// Create Ret
List<String> ret = new ArrayList<String>();
// Ensure between 0 and 1;
quota = limit(quota);
@ -125,18 +140,56 @@ public class Progressbar
// The rest is empty
int emptyCount = (int) ((width - solidCount) / solidsPerEmpty);
// Create the non-parsed bar
String ret = left + Txt.repeat(solid, solidCount) + between + Txt.repeat(empty, emptyCount) + right;
// Color Parse Parts
left = colorParse(left, colorTag, color);
solid = colorParse(solid, colorTag, color);
between = colorParse(between, colorTag, color);
empty = colorParse(empty, colorTag, color);
right = colorParse(right, colorTag, color);
// Replace color tag
ret = ret.replace(colorTag, color);
// Parse amp color codes
ret = Txt.parse(ret);
// Combine Parts
if (left != null)
{
ret.add(left);
}
if (solid != null)
{
for (int i = 1; i <= solidCount; i++)
{
ret.add(solid);
}
}
if (between != null)
{
ret.add(between);
}
if (empty != null)
{
for (int i = 1; i <= emptyCount; i++)
{
ret.add(empty);
}
}
if (right != null)
{
ret.add(right);
}
return ret;
}
public static String colorParse(String string, String colorTag, String color)
{
if (string == null) return null;
string = string.replace(colorTag, color);
string = Txt.parse(string);
return string;
}
public static double limit(double quota)
{
if (quota > 1) return 1;

View File

@ -292,6 +292,128 @@ public class MUtil
return ret.toString();
}
// -------------------------------------------- //
// LIST OPERATIONS
// -------------------------------------------- //
public static <T> List<T> repeat(T object, int times)
{
List<T> ret = new ArrayList<T>(times);
for (int i = 1; i <= times; i++)
{
ret.add(object);
}
return ret;
}
public static void keepLeft(List<?> list, int maxlength)
{
if (list.size() <= maxlength) return;
list.subList(maxlength, list.size()).clear();
}
public static void keepRight(List<?> list, int maxlength)
{
if (list.size() <= maxlength) return;
list.subList(0, maxlength).clear();
}
public static <T> void padLeft(List<T> list, T object, int length)
{
if (list.size() >= length) return;
list.addAll(0, repeat(object, length - list.size()));
}
public static <T> void padRight(List<T> list, T object, int length)
{
if (list.size() >= length) return;
list.addAll(repeat(object, length - list.size()));
}
// -------------------------------------------- //
// ITERABLE MATH
// -------------------------------------------- //
public static <T extends Number> double getSum(Iterable<T> numbers)
{
if (numbers == null) throw new NullPointerException("numbers");
double sum = 0;
for (T number : numbers)
{
sum += number.doubleValue();
}
return sum;
}
public static <T extends Number> double getAverage(Iterable<T> numbers)
{
if (numbers == null) throw new NullPointerException("numbers");
double sum = 0;
int count = 0;
for (T number : numbers)
{
sum += number.doubleValue();
count++;
}
if (count == 0) throw new IllegalArgumentException("numbers empty");
return sum / count;
}
// -------------------------------------------- //
// TABLE OPERATIONS
// -------------------------------------------- //
public static <T> List<List<T>> rotateLeft(List<List<T>> rows)
{
List<List<T>> ret = transpose(rows);
flipVertically(ret);
return ret;
}
public static <T> List<List<T>> rotateRight(List<List<T>> rows)
{
List<List<T>> ret = transpose(rows);
flipHorizontally(ret);
return ret;
}
public static <T> List<List<T>> transpose(List<List<T>> rows)
{
List<List<T>> ret = new ArrayList<List<T>>();
final int n = rows.get(0).size();
for (int i = 0; i < n; i++)
{
List<T> col = new ArrayList<T>();
for (List<T> row : rows)
{
col.add(row.get(i));
}
ret.add(col);
}
return ret;
}
public static <T> void flipHorizontally(List<List<T>> rows)
{
for (List<T> row : rows)
{
Collections.reverse(row);
}
}
public static <T> void flipVertically(List<List<T>> rows)
{
Collections.reverse(rows);
}
// -------------------------------------------- //
// COLOR INT CODE
// -------------------------------------------- //