public code v1
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?flintstones.helper.data.xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>flintstones.group</groupId>
|
||||
<artifactId>flintstones.bundles</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>flintstones.helper.data</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>eclipse-plugin</packaging>
|
||||
<name>[bundle] Data</name>
|
||||
</project>
|
||||
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>flintstones.helper.data</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.ManifestBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.SchemaBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.pde.PluginNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1779484362605</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
@@ -0,0 +1,7 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
@@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
@@ -0,0 +1,18 @@
|
||||
Manifest-Version: 1.0
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Name: Data
|
||||
Bundle-SymbolicName: flintstones.helper.data
|
||||
Bundle-Version: 1.0.0.qualifier
|
||||
Automatic-Module-Name: flintstones.helper.data
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
|
||||
Export-Package: flintstones.helper,
|
||||
flintstones.helper.data,
|
||||
flintstones.helper.data.adapter,
|
||||
flintstones.helper.data.text
|
||||
Require-Bundle: flintstones.helper.extensionpoint,
|
||||
flintstones.helper.debug,
|
||||
org.eclipse.osgi,
|
||||
org.eclipse.equinox.registry,
|
||||
org.eclipse.swt,
|
||||
org.apache.commons.math3,
|
||||
org.apache.commons.lang
|
||||
@@ -0,0 +1,4 @@
|
||||
source.. = src/
|
||||
output.. = bin/
|
||||
bin.includes = META-INF/,\
|
||||
.
|
||||
@@ -0,0 +1,332 @@
|
||||
package flintstones.helper;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
|
||||
/**
|
||||
* The Class DoubleHelper.
|
||||
*/
|
||||
public class DoubleHelper {
|
||||
|
||||
/** The Constant NUMBER_OF_DECIMALS. */
|
||||
private final static int NUMBER_OF_DECIMALS = 3;
|
||||
|
||||
public static String Draw(double val, int decimalPlaces, boolean zeroesAfterComma) {
|
||||
|
||||
String pattern = "0.";
|
||||
String placeholder = zeroesAfterComma ? "0" : "#";
|
||||
for (int i = 0; i < decimalPlaces; i++)
|
||||
pattern += placeholder;
|
||||
|
||||
DecimalFormatSymbols decimalSymbol = new DecimalFormatSymbols();
|
||||
decimalSymbol.setDecimalSeparator(',');
|
||||
|
||||
DecimalFormat df = new DecimalFormat(pattern, decimalSymbol);
|
||||
String formatted = df.format(val);
|
||||
return formatted;
|
||||
|
||||
}
|
||||
|
||||
public static String Draw(Double val, int decimalPlaces) {
|
||||
return Draw(val, decimalPlaces, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw.
|
||||
*
|
||||
* @param val the value
|
||||
* @return the string ready to be drawn in the UI.
|
||||
*/
|
||||
public static String Draw(Double val) {
|
||||
return Draw(val, NUMBER_OF_DECIMALS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string fraction into a double.
|
||||
*
|
||||
* @param fraction the fraction
|
||||
* @return the double
|
||||
*/
|
||||
public static Double ParseFraction(String fraction) {
|
||||
if (fraction.contains("/")) {
|
||||
String[] rat = fraction.split("/");
|
||||
return Double.parseDouble(rat[0]) / Double.parseDouble(rat[1]);
|
||||
}
|
||||
return Double.parseDouble(fraction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the double that uses a comma.
|
||||
*
|
||||
* @param numberString the number string
|
||||
* @return the double
|
||||
*/
|
||||
public static Double ParseDouble(String numberString) {
|
||||
|
||||
if (numberString.equals("") || numberString.equals("-"))
|
||||
return 0.0;
|
||||
|
||||
String number = numberString.replace(",", ".");
|
||||
return Double.parseDouble(number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize.
|
||||
*
|
||||
* @param arr the arr
|
||||
* @return the array normalized as double[]
|
||||
*/
|
||||
public static double[] Normalize(double[] arr) {
|
||||
double sum = 0.0;
|
||||
double result[] = new double[arr.length];
|
||||
|
||||
for (double x : arr)
|
||||
sum += x;
|
||||
|
||||
for (int i = 0; i < arr.length; i++)
|
||||
result[i] = arr[i] / sum;
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Round the value.
|
||||
*
|
||||
* @param value the value
|
||||
* @param places the places
|
||||
* @return the double
|
||||
*/
|
||||
public static double Round(double value, int places) {
|
||||
double scale = Math.pow(10, places);
|
||||
return Math.round(value * scale) / scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the max item and return it index.
|
||||
*
|
||||
* @param arr the arr
|
||||
* @return the int
|
||||
*/
|
||||
public static int FindMaxIndex(double[] arr) {
|
||||
int maxIndex = -1;
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
|
||||
double val = arr[i];
|
||||
if (val > max) {
|
||||
max = val;
|
||||
maxIndex = i;
|
||||
|
||||
}
|
||||
}
|
||||
return maxIndex;
|
||||
|
||||
}
|
||||
|
||||
public static int[] FindMaxIndexes(double[] arr) {
|
||||
|
||||
ArrayList<Integer> maxIndexes = new ArrayList<>();
|
||||
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
|
||||
double val = arr[i];
|
||||
if (val > max) {
|
||||
|
||||
max = val;
|
||||
|
||||
maxIndexes.clear();
|
||||
maxIndexes.add(i);
|
||||
|
||||
} else if( val == max ) {
|
||||
maxIndexes.add(i);
|
||||
}
|
||||
}
|
||||
return ArrayUtils.toPrimitive(maxIndexes.toArray(new Integer[0]));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the max item and return it index.
|
||||
*
|
||||
* @param arr the arr
|
||||
* @return the int
|
||||
*/
|
||||
public static int FindMinIndex(double[] arr) {
|
||||
int minIndex = -1;
|
||||
double min = Double.MAX_VALUE;
|
||||
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
|
||||
double val = arr[i];
|
||||
if (val < min) {
|
||||
min = val;
|
||||
minIndex = i;
|
||||
|
||||
}
|
||||
}
|
||||
return minIndex;
|
||||
|
||||
}
|
||||
|
||||
public static int[] FindMinIndexes(double[] arr) {
|
||||
|
||||
ArrayList<Integer> minIndexes = new ArrayList<>();
|
||||
|
||||
double min = Double.MAX_VALUE;
|
||||
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
|
||||
double val = arr[i];
|
||||
if (val < min) {
|
||||
min = val;
|
||||
|
||||
minIndexes.clear();
|
||||
minIndexes.add(i);
|
||||
|
||||
} else if( val == min) {
|
||||
minIndexes.add(i);
|
||||
}
|
||||
}
|
||||
return ArrayUtils.toPrimitive(minIndexes.toArray(new Integer[0]));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a value can be double.
|
||||
*
|
||||
* @param str the str
|
||||
* @return true, if successful
|
||||
*/
|
||||
public static boolean IsDouble(String str) {
|
||||
try {
|
||||
Double.valueOf(str);
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static double NormalizeZeroToOne(String singleValue) {
|
||||
double value = DoubleHelper.ParseDouble(singleValue);
|
||||
if (value > 1.0) {
|
||||
int length = singleValue.length();
|
||||
double div = Math.pow(10, length);
|
||||
value = (value / div);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static double[] ToRange(double min, double max, int parts, boolean addLast) {
|
||||
|
||||
double total = max - min;
|
||||
|
||||
double partSize = total / parts;
|
||||
int partNumber = addLast ? parts + 1 : parts;
|
||||
double[] values = new double[partNumber];
|
||||
|
||||
double sum = partSize;
|
||||
double start = min;
|
||||
|
||||
for (int i = 0; i < partNumber; i++)
|
||||
values[i] = Math.round((start + (sum * i)) * 100d) / 100d;
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
public static Double[] GetCentralPoints(Double[] data) {
|
||||
|
||||
Double centrals[] = new Double[data.length-1];
|
||||
for(int i = 0; i < data.length-1; i++) {
|
||||
double low = data[i];
|
||||
double up = data[i+1];
|
||||
|
||||
double cen = (up+low)/2;
|
||||
centrals[i] = cen;
|
||||
}
|
||||
|
||||
return centrals;
|
||||
}
|
||||
|
||||
|
||||
public static boolean IsAscending(double[] data) {
|
||||
int len = data.length;
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
if (data[i] > data[i + 1])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean IsDescending(double[] data) {
|
||||
int len = data.length;
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
if (data[i] < data[i + 1])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean IsSorted(double[] data) {
|
||||
return IsAscending(data) || IsDescending(data);
|
||||
}
|
||||
|
||||
public static double[] Convert(String[] data) {
|
||||
double[] arr = new double[data.length];
|
||||
for(int i = 0; i < data.length; i++) {
|
||||
arr[i] = ParseDouble( data[i] );
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static double[] Add(double[] arr, double element) {
|
||||
arr = Arrays.copyOf(arr, arr.length+1);
|
||||
arr[arr.length-1] = element;
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static Double[] Add(Double[] arr, double element) {
|
||||
arr = Arrays.copyOf(arr, arr.length+1);
|
||||
arr[arr.length-1] = element;
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static int FindInAscendingRange(double[] arr, double val) {
|
||||
for(int i = 0; i< arr.length;i++) {
|
||||
double current = arr[i];
|
||||
if(val < current)
|
||||
return i-1;
|
||||
if(val == current)
|
||||
return i;
|
||||
}
|
||||
return arr.length-1;
|
||||
}
|
||||
|
||||
public static int FindInDescendingRange(double[] arr, double val) {
|
||||
for(int i = 0; i< arr.length;i++) {
|
||||
double current = arr[i];
|
||||
if(val >= current)
|
||||
return i;
|
||||
}
|
||||
return arr.length-1;
|
||||
}
|
||||
|
||||
public static boolean Equals(double first, double second, int decPrecision) {
|
||||
|
||||
double pAdd = 1.0/Math.pow(10, decPrecision); // 0.1, 0.01, 0.001...
|
||||
double max = first + pAdd;
|
||||
double min = first - pAdd;
|
||||
|
||||
if(max > second && min < second)
|
||||
return true;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package flintstones.helper;
|
||||
|
||||
import flintstones.helper.debug.DH;
|
||||
import org.apache.commons.math3.linear.EigenDecomposition;
|
||||
import org.apache.commons.math3.linear.MatrixUtils;
|
||||
import org.apache.commons.math3.linear.RealMatrix;
|
||||
import org.apache.commons.math3.linear.RealVector;
|
||||
|
||||
/**
|
||||
* The Class EigenHelper can be used to call the EigenDecomposition and Normalization.
|
||||
*/
|
||||
public class EigenHelper {
|
||||
|
||||
/**
|
||||
* Instantiates a new eigen helper.
|
||||
*/
|
||||
public EigenHelper() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the eigen vector.
|
||||
*
|
||||
* @param values the values
|
||||
* @return the double[]
|
||||
*/
|
||||
private static double[] GetEigenVector(double[][] values) {
|
||||
|
||||
// Matrix to library
|
||||
RealMatrix realMatrix = MatrixUtils.createRealMatrix(values);
|
||||
|
||||
// Get Max eigenvalue
|
||||
EigenDecomposition descomposition = new EigenDecomposition(realMatrix);
|
||||
int pos = DoubleHelper.FindMaxIndex(descomposition.getRealEigenvalues());
|
||||
|
||||
// Get its vector
|
||||
RealVector eigenVector = descomposition.getEigenvector(pos);
|
||||
|
||||
return eigenVector.toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the normalized eigen vector.
|
||||
*
|
||||
* @param values the values
|
||||
* @return the double[]
|
||||
*/
|
||||
public static double[] GetNormalizedEigenVector(double[][] values) {
|
||||
|
||||
double[] result = GetEigenVector(values);
|
||||
|
||||
// Normalize
|
||||
return DoubleHelper.Normalize(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show 2 x 2 matrix into console.
|
||||
*/
|
||||
public static void Show2x2Matrix() {
|
||||
double[][] values = new double[2][2];
|
||||
values[0][0] = 1;
|
||||
values[0][1] = 1;
|
||||
values[1][0] = 1;
|
||||
values[1][1] = 1;
|
||||
|
||||
for(int i = 1; i < 10; i++) {
|
||||
|
||||
values[0][1] = i;
|
||||
values[1][0] = (double)1/i;
|
||||
|
||||
|
||||
double[] result = EigenHelper.GetNormalizedEigenVector(values);
|
||||
DH.out("(" + i + ", " + "1/" + i + ") " + " ==> " + DoubleHelper.Draw(result[0]) + ", " + DoubleHelper.Draw(result[1]));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void Show2x2MatrixNotNormalize() {
|
||||
double[][] values = new double[2][2];
|
||||
values[0][0] = 1;
|
||||
values[0][1] = 1;
|
||||
values[1][0] = 1;
|
||||
values[1][1] = 1;
|
||||
|
||||
for(int i = 1; i < 10; i++) {
|
||||
|
||||
values[0][1] = i;
|
||||
values[1][0] = (double)1/i;
|
||||
|
||||
|
||||
double[] result = EigenHelper.GetEigenVector(values);
|
||||
DH.out("(" + i + ", " + "1/" + i + ") " + " ==> " + DoubleHelper.Draw(result[0]) + ", " + DoubleHelper.Draw(result[1]));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package flintstones.helper;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.FrameworkUtil;
|
||||
|
||||
/**
|
||||
* The Class FileHelper solves common problem on file loading into a helper.
|
||||
*/
|
||||
public class FileHelper {
|
||||
|
||||
/**
|
||||
* Read file.
|
||||
*
|
||||
* @param path the path
|
||||
* @param encoding the encoding
|
||||
* @return the string
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
static String readFile(String path, Charset encoding) throws IOException {
|
||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded, encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file.
|
||||
*
|
||||
* @param path the path
|
||||
* @return the string
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static String readFile(String path) throws IOException {
|
||||
return readFile(path, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file.
|
||||
*
|
||||
* @param fileURL the file URL
|
||||
* @return the string
|
||||
*/
|
||||
public static String readFile(URL fileURL) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
try {
|
||||
String inputLine;
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(fileURL.openStream(), "UTF-8"));
|
||||
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
sb.append(inputLine);
|
||||
sb.append('\n');
|
||||
}
|
||||
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file.
|
||||
*
|
||||
* @param fileName the file name
|
||||
* @param bundle the bundle
|
||||
* @return the string
|
||||
*/
|
||||
public static String readFile(String fileName, Bundle bundle) {
|
||||
URL fileURL = bundle.getEntry(fileName);
|
||||
return readFile(fileURL);
|
||||
}
|
||||
|
||||
public static URL getFileURL(Class<?> classFromBundle, String path) {
|
||||
Bundle b = FrameworkUtil.getBundle(classFromBundle);
|
||||
|
||||
return b.getEntry(path);
|
||||
}
|
||||
|
||||
public static String encode(String uri) {
|
||||
try {
|
||||
return URLEncoder.encode(uri,"UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package flintstones.helper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import flintstones.helper.data.HashMatrix;
|
||||
|
||||
/**
|
||||
* The Class MapHelper.
|
||||
*/
|
||||
public class MapHelper {
|
||||
|
||||
/**
|
||||
* Sort by value a map.
|
||||
*
|
||||
* @param <K> the key type
|
||||
* @param <V> the value type
|
||||
* @param map the map
|
||||
* @return the map
|
||||
*/
|
||||
// https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values
|
||||
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean descending) {
|
||||
List<Entry<K, V>> list = new ArrayList<>(map.entrySet());
|
||||
list.sort(Entry.comparingByValue());
|
||||
if(descending)
|
||||
Collections.reverse(list);
|
||||
|
||||
Map<K, V> result = new LinkedHashMap<>();
|
||||
for (Entry<K, V> entry : list) {
|
||||
result.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// Return a map, ordered in the order of the given array
|
||||
public static <K, V> LinkedHashMap<K, V> sortByExample(Map<K, V> map, K[] examples) {
|
||||
|
||||
LinkedHashMap<K, V> linked = new LinkedHashMap<>();
|
||||
for (K item : examples)
|
||||
linked.put(item, map.get(item));
|
||||
|
||||
return linked;
|
||||
}
|
||||
|
||||
// Not working?
|
||||
public static <K1, K2, V> LinkedHashMap<K1, LinkedHashMap<K2, V>> sortByExample(HashMatrix<K1, K2, V> map,
|
||||
K1[] examples1, K2[] examples2) {
|
||||
|
||||
LinkedHashMap<K1, LinkedHashMap<K2, V>> outer = new LinkedHashMap<>();
|
||||
for (K1 item1 : examples1) {
|
||||
LinkedHashMap<K2, V> inner = new LinkedHashMap<>();
|
||||
for (K2 item2 : examples2)
|
||||
inner.put(item2, map.get(item1, item2));
|
||||
outer.put(item1, inner);
|
||||
}
|
||||
return outer;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package flintstones.helper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import flintstones.helper.data.HashMatrix;
|
||||
|
||||
/**
|
||||
* The Class MatrixHelper.
|
||||
*/
|
||||
public class MatrixHelper {
|
||||
|
||||
/**
|
||||
* Transpose a matrix.
|
||||
*
|
||||
* @param <T> the generic type
|
||||
* @param arrayList the array list
|
||||
* @return the list
|
||||
*/
|
||||
public static <T> List<List<T>> transpose(ArrayList<ArrayList<T>> arrayList) {
|
||||
List<List<T>> ret = new ArrayList<List<T>>();
|
||||
final int N = arrayList.get(0).size();
|
||||
for (int i = 0; i < N; i++) {
|
||||
List<T> col = new ArrayList<T>();
|
||||
for (List<T> row : arrayList) {
|
||||
col.add(row.get(i));
|
||||
}
|
||||
ret.add(col);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transpose a matrix.
|
||||
*
|
||||
* @param m the m
|
||||
* @return the double[][]
|
||||
*/
|
||||
public static double[][] transpose(double[][] m) {
|
||||
double[][] temp = new double[m[0].length][m.length];
|
||||
for (int i = 0; i < m.length; i++)
|
||||
for (int j = 0; j < m[0].length; j++)
|
||||
temp[j][i] = m[i][j];
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transpose a matrix.
|
||||
*
|
||||
* @param m the m
|
||||
* @return the string[][]
|
||||
*/
|
||||
public static String[][] transpose(String[][] m) {
|
||||
String[][] temp = new String[m[0].length][m.length];
|
||||
for (int i = 0; i < m.length; i++)
|
||||
for (int j = 0; j < m[0].length; j++)
|
||||
temp[j][i] = m[i][j];
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a matrix as a formatted String
|
||||
*
|
||||
* @param m the m
|
||||
* @return the string[][]
|
||||
*/
|
||||
public static String[][] asString(double[][] m) {
|
||||
String[][] temp = new String[m.length][m[0].length];
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[0].length; j++) {
|
||||
temp[i][j] = DoubleHelper.Draw(m[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
public static String[][] asString(int[][] m) {
|
||||
String[][] temp = new String[m.length][m[0].length];
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[0].length; j++) {
|
||||
temp[i][j] = m[i][j]+"";
|
||||
}
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
public static HashMatrix<String, String, String> toHashMatrix(String textMatrix, boolean hasHeaders) {
|
||||
|
||||
String[] rows = textMatrix.split("\r\n");
|
||||
|
||||
// H Header
|
||||
String[] hHeader = new String[0];
|
||||
if (hasHeaders) {
|
||||
hHeader = rows[0].split("\t");
|
||||
rows = Arrays.copyOfRange(rows, 1, rows.length);
|
||||
}
|
||||
|
||||
// Calculate sizes
|
||||
int nCols = rows[0].split("\t").length;
|
||||
if (hasHeaders)
|
||||
nCols--;
|
||||
|
||||
// Fix horizontal header removing first element if it is not empty
|
||||
if(nCols < hHeader.length)
|
||||
hHeader = Arrays.copyOfRange(hHeader, 1, hHeader.length);
|
||||
|
||||
|
||||
String[][] matrix = new String[rows.length][nCols];
|
||||
String[] vHeader = new String[rows.length];
|
||||
|
||||
for (int i = 0; i < rows.length; i++) {
|
||||
String row = rows[i];
|
||||
String[] column = row.split("\t");
|
||||
int colLength = hasHeaders ? column.length -1 : column.length;
|
||||
|
||||
for (int j = 0; j < colLength; j++) {
|
||||
int jj = hasHeaders ? j+1 : j;
|
||||
matrix[i][j] = column[jj];
|
||||
}
|
||||
|
||||
if (hasHeaders)
|
||||
vHeader[i] = column[0];
|
||||
|
||||
}
|
||||
|
||||
vHeader = Arrays.stream(vHeader).map(k -> StringHelper.Draw(k)).toArray(String[]::new);
|
||||
hHeader = Arrays.stream(hHeader).map(k -> StringHelper.Draw(k)).toArray(String[]::new);
|
||||
|
||||
return new HashMatrix<>(matrix, vHeader, hHeader);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String[][] addHeaders(String[][] matrix, String[] hHeaders, String[] vHeaders){
|
||||
|
||||
int rowC = matrix.length;
|
||||
int colC = matrix[0].length;
|
||||
|
||||
if(rowC != vHeaders.length)
|
||||
throw new RuntimeException("l1 != hHeaders");
|
||||
|
||||
if(colC != hHeaders.length)
|
||||
throw new RuntimeException("l2 != vHeaders");
|
||||
|
||||
|
||||
String m[][] = new String[rowC+1][colC+1];
|
||||
|
||||
// add hHeader
|
||||
m[0][colC] = " ";
|
||||
for(int i = 0; i < colC; i++) {
|
||||
m[0][i] = hHeaders[i];
|
||||
}
|
||||
|
||||
for(int i = 0; i < rowC; i++) {
|
||||
int row = i+1;
|
||||
for(int j = 0; j < colC; j++) {
|
||||
m[row][j] = matrix[i][j];
|
||||
}
|
||||
m[row][colC] = vHeaders[i]; // Add last item to the row
|
||||
}
|
||||
|
||||
return m;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package flintstones.helper;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang.RandomStringUtils;
|
||||
|
||||
public class StringHelper {
|
||||
|
||||
public static String PrintTable(String[][] table) {
|
||||
|
||||
// Find out what the maximum number of columns is in any row
|
||||
int maxColumns = 0;
|
||||
for (int i = 0; i < table.length; i++) {
|
||||
maxColumns = Math.max(table[i].length, maxColumns);
|
||||
}
|
||||
|
||||
// Find the maximum length of a string in each column
|
||||
int[] lengths = new int[maxColumns];
|
||||
for (int i = 0; i < table.length; i++) {
|
||||
for (int j = 0; j < table[i].length; j++) {
|
||||
lengths[j] = Math.max(table[i][j].length(), lengths[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a format string for each column
|
||||
String[] formats = new String[lengths.length];
|
||||
for (int i = 0; i < lengths.length; i++) {
|
||||
formats[i] = "%1$" + lengths[i] + "s"
|
||||
+ (i + 1 == lengths.length ? "\n" : " ");
|
||||
}
|
||||
|
||||
StringBuilder mod = new StringBuilder();
|
||||
// Print 'em out
|
||||
for (int i = 0; i < table.length; i++) {
|
||||
for (int j = 0; j < table[i].length; j++) {
|
||||
mod.append(String.format(formats[j], table[i][j]));
|
||||
}
|
||||
}
|
||||
return mod.toString();
|
||||
}
|
||||
|
||||
public static String Draw(String text) {
|
||||
|
||||
// Remove text between parentheses
|
||||
text = text.replaceAll("\\(.*\\)", "");
|
||||
|
||||
// Trip
|
||||
text = text.trim();
|
||||
|
||||
// Replace _ and - with space " "
|
||||
text = text.replaceAll("_", " ");
|
||||
text = text.replaceAll("-", " ");
|
||||
text = text.replaceAll("’", " ");
|
||||
text = text.replaceAll("'", " ");
|
||||
text = text.replaceAll("\"", " ");
|
||||
|
||||
// Remove double..+ spaces
|
||||
text = text.replaceAll(" +", " ");
|
||||
|
||||
return text;
|
||||
|
||||
}
|
||||
|
||||
public static int IndexOf(String[] arr, String text) {
|
||||
for(int i = 0; i < arr.length; i++)
|
||||
if(arr[i].equals(text))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static String Random(int lmax, int lmin) {
|
||||
int result = (int) (Math.random() * (lmax - lmin)) + lmin;
|
||||
return RandomStringUtils.randomAlphanumeric(result);
|
||||
}
|
||||
|
||||
public static String[] SplitSplit(String plainText) {
|
||||
String[] elements = plainText.split("\r\n");
|
||||
if(elements.length == 1)
|
||||
elements = plainText.split("\t");
|
||||
|
||||
for(int i = 0; i < elements.length; i++)
|
||||
elements[i] = elements[i].replaceAll("\n","").replaceAll("\r","").replaceAll("\t","");
|
||||
|
||||
return elements;
|
||||
|
||||
}
|
||||
|
||||
public static String GetLastAfterSplit(String text, String separator) {
|
||||
|
||||
String[] arr = text.split(Pattern.quote(separator));
|
||||
return arr[arr.length-1];
|
||||
|
||||
}
|
||||
|
||||
public static String GetButLastAfterSplit(String text, String separator) {
|
||||
|
||||
String[] arr = text.split(Pattern.quote(separator));
|
||||
String[] narr = new String[arr.length-1];
|
||||
for(int i = 0; i < arr.length-1; i++) {
|
||||
narr[i] = arr[i];
|
||||
}
|
||||
|
||||
return String.join(separator, narr);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package flintstones.helper.data;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Config {
|
||||
|
||||
private HashMap<String,Object> config = new HashMap<>();
|
||||
|
||||
public void set(String key, String val) {
|
||||
config.put(key, val);
|
||||
}
|
||||
|
||||
public void set(String key, Boolean val) {
|
||||
config.put(key, val);
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
return (String) config.get(key);
|
||||
}
|
||||
|
||||
public Boolean is(String key) {
|
||||
return (Boolean) config.get(key);
|
||||
}
|
||||
|
||||
public Config defaultEnabled(String... ars) {
|
||||
for(String key : ars)
|
||||
set(key, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Config defaultDisabled(String... ars) {
|
||||
for(String key : ars)
|
||||
set(key, false);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package flintstones.helper.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import flintstones.helper.StringHelper;
|
||||
|
||||
public class HashMapList<K, V> implements Iterable<Entry<K, ArrayList<V>>> {
|
||||
|
||||
private HashMap<K, ArrayList<V>> content = new HashMap<K, ArrayList<V>>();
|
||||
|
||||
public HashMapList() {
|
||||
}
|
||||
|
||||
public void add(K key, V value) {
|
||||
ArrayList<V> arr = content.get(key);
|
||||
if (arr == null)
|
||||
arr = new ArrayList<>();
|
||||
|
||||
arr.add(value);
|
||||
content.put(key, arr);
|
||||
}
|
||||
|
||||
public void put(K key, ArrayList<V> values) {
|
||||
content.put(key, values);
|
||||
}
|
||||
|
||||
public void put(K key, V[] values) {
|
||||
ArrayList<V> valuesL = new ArrayList<V>(Arrays.asList(values));
|
||||
put(key, valuesL);
|
||||
}
|
||||
|
||||
public ArrayList<V> get(K key) {
|
||||
return content.get(key);
|
||||
}
|
||||
|
||||
public ArrayList<V> getSafe(K key) {
|
||||
ArrayList<V> arr = content.get(key);
|
||||
return arr == null ? new ArrayList<V>() : arr;
|
||||
}
|
||||
|
||||
public HashMap<K, ArrayList<V>> getMap() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public boolean contains(K key) {
|
||||
return content.get(key) != null;
|
||||
}
|
||||
|
||||
public boolean contains(K key, V value) {
|
||||
ArrayList<V> arr = content.get(key);
|
||||
if (arr == null)
|
||||
return false;
|
||||
|
||||
return arr.contains(value);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
content.clear();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return content.size();
|
||||
}
|
||||
|
||||
public int size(K key) {
|
||||
ArrayList<V> arr = content.get(key);
|
||||
if (arr == null)
|
||||
return 0;
|
||||
return arr.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<K, ArrayList<V>>> iterator() {
|
||||
return content.entrySet().iterator();
|
||||
}
|
||||
|
||||
public Collection<ArrayList<V>> values() {
|
||||
return content.values();
|
||||
}
|
||||
|
||||
public Set<Map.Entry<K, ArrayList<V>>> entrySet() {
|
||||
return content.entrySet();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
return content.keySet();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String[] hHeaders = content.keySet().stream().map(k -> k.toString()).toArray(String[]::new);
|
||||
int size = content.values().iterator().next().size();
|
||||
String[] vHeaders = IntStream.rangeClosed(1, size).mapToObj(k -> "_" + k + "_").toArray(String[]::new);
|
||||
|
||||
String[][] matrix = new String[hHeaders.length+1][vHeaders.length+1];
|
||||
for(int i = 0; i < vHeaders.length; i++) {
|
||||
matrix[0][i] = vHeaders[i];
|
||||
}
|
||||
matrix[0][vHeaders.length] = "";
|
||||
|
||||
int i = 0;
|
||||
for(Entry<K,ArrayList<V>> entry : content.entrySet()) {
|
||||
ArrayList<V> innerList = entry.getValue();
|
||||
int j = 0;
|
||||
for(V innerEntry : innerList) {
|
||||
matrix[i+1][j] = innerEntry.toString();
|
||||
j++;
|
||||
}
|
||||
matrix[i+1][j] = hHeaders[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
return StringHelper.PrintTable(matrix);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package flintstones.helper.data;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import flintstones.helper.StringHelper;
|
||||
|
||||
public class HashMatrix<K1, K2, V> implements Iterable<Entry<K1, HashMap<K2, V>>> {
|
||||
|
||||
private HashMap<K1, HashMap<K2, V>> content = new HashMap<>();
|
||||
|
||||
public HashMatrix() {
|
||||
};
|
||||
|
||||
public HashMatrix(V[][] matrix, K1[] verticalHeader, K2[] horizontalHeader) {
|
||||
|
||||
for(int i = 0; i < verticalHeader.length; i++) {
|
||||
K1 item1 = verticalHeader[i];
|
||||
for(int j = 0; j < horizontalHeader.length; j++) {
|
||||
K2 item2 = horizontalHeader[j];
|
||||
V value = matrix[i][j];
|
||||
put(item1, item2, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void put(K1 key1, K2 key2, V value) {
|
||||
|
||||
HashMap<K2, V> innerMap = content.get(key1);
|
||||
if (innerMap == null)
|
||||
innerMap = new HashMap<>();
|
||||
innerMap.put(key2, value);
|
||||
content.put(key1, innerMap);
|
||||
|
||||
}
|
||||
|
||||
public V get(K1 key1, K2 key2) {
|
||||
HashMap<K2, V> innerMap = content.get(key1);
|
||||
if (innerMap == null)
|
||||
return null;
|
||||
return innerMap.get(key2);
|
||||
}
|
||||
|
||||
public HashMap<K2, V> get(K1 key1) {
|
||||
HashMap<K2, V> innerMap = content.get(key1);
|
||||
return innerMap;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return content.size();
|
||||
}
|
||||
|
||||
public int innerSize() {
|
||||
return content.values().iterator().next().size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
content.clear();
|
||||
}
|
||||
|
||||
public HashMap<K1, HashMap<K2, V>> getMap() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<K1, HashMap<K2, V>>> iterator() {
|
||||
return content.entrySet().iterator();
|
||||
}
|
||||
|
||||
public Collection<HashMap<K2, V>> values() {
|
||||
return content.values();
|
||||
}
|
||||
|
||||
public Set<Map.Entry<K1, HashMap<K2, V>>> entrySet() {
|
||||
return content.entrySet();
|
||||
}
|
||||
|
||||
public Set<K1> keySet() {
|
||||
return content.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String[] hHeaders = content.keySet().stream().map(k -> k.toString()).toArray(String[]::new);
|
||||
String[] vHeaders = content.values().iterator().next().keySet().stream().map(k -> k.toString())
|
||||
.toArray(String[]::new);
|
||||
|
||||
String[][] matrix = new String[hHeaders.length + 1][vHeaders.length + 1];
|
||||
for (int i = 0; i < vHeaders.length; i++) {
|
||||
matrix[0][i] = vHeaders[i];
|
||||
}
|
||||
matrix[0][vHeaders.length] = "";
|
||||
|
||||
int i = 0;
|
||||
for (Entry<K1, HashMap<K2, V>> entry : content.entrySet()) {
|
||||
HashMap<K2, V> innerMap = entry.getValue();
|
||||
int j = 0;
|
||||
for (Entry<K2, V> innerEntry : innerMap.entrySet()) {
|
||||
matrix[i + 1][j] = innerEntry.getValue().toString();
|
||||
j++;
|
||||
}
|
||||
matrix[i + 1][j] = hHeaders[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
return StringHelper.PrintTable(matrix);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package flintstones.helper.data;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import flintstones.helper.StringHelper;
|
||||
|
||||
public class LinkedHashMatrix<K1, K2, V> implements Iterable<Entry<K1, LinkedHashMap<K2, V>>> {
|
||||
|
||||
private LinkedHashMap<K1, LinkedHashMap<K2, V>> content = new LinkedHashMap<>();
|
||||
|
||||
public LinkedHashMatrix() {
|
||||
};
|
||||
|
||||
public LinkedHashMatrix(V[][] matrix, K1[] verticalHeader, K2[] horizontalHeader) {
|
||||
|
||||
for(int i = 0; i < verticalHeader.length; i++) {
|
||||
K1 item1 = verticalHeader[i];
|
||||
for(int j = 0; j < horizontalHeader.length; j++) {
|
||||
K2 item2 = horizontalHeader[j];
|
||||
V value = matrix[i][j];
|
||||
put(item1, item2, value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void put(K1 key1, K2 key2, V value) {
|
||||
|
||||
LinkedHashMap<K2, V> innerMap = content.get(key1);
|
||||
if (innerMap == null)
|
||||
innerMap = new LinkedHashMap<>();
|
||||
innerMap.put(key2, value);
|
||||
content.put(key1, innerMap);
|
||||
|
||||
}
|
||||
|
||||
public V get(K1 key1, K2 key2) {
|
||||
LinkedHashMap<K2, V> innerMap = content.get(key1);
|
||||
if (innerMap == null)
|
||||
return null;
|
||||
return innerMap.get(key2);
|
||||
}
|
||||
|
||||
public LinkedHashMap<K2, V> get(K1 key1) {
|
||||
LinkedHashMap<K2, V> innerMap = content.get(key1);
|
||||
return innerMap;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return content.size();
|
||||
}
|
||||
|
||||
public int innerSize() {
|
||||
return content.values().iterator().next().size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
content.clear();
|
||||
}
|
||||
|
||||
public LinkedHashMap<K1, LinkedHashMap<K2, V>> getMap() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<K1, LinkedHashMap<K2, V>>> iterator() {
|
||||
return content.entrySet().iterator();
|
||||
}
|
||||
|
||||
public Collection<LinkedHashMap<K2, V>> values() {
|
||||
return content.values();
|
||||
}
|
||||
|
||||
public Set<Map.Entry<K1, LinkedHashMap<K2, V>>> entrySet() {
|
||||
return content.entrySet();
|
||||
}
|
||||
|
||||
public Set<K1> keySet() {
|
||||
return content.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String[] hHeaders = content.keySet().stream().map(k -> k.toString()).toArray(String[]::new);
|
||||
String[] vHeaders = content.values().iterator().next().keySet().stream().map(k -> k.toString())
|
||||
.toArray(String[]::new);
|
||||
|
||||
String[][] matrix = new String[hHeaders.length + 1][vHeaders.length + 1];
|
||||
for (int i = 0; i < vHeaders.length; i++) {
|
||||
matrix[0][i] = vHeaders[i];
|
||||
}
|
||||
matrix[0][vHeaders.length] = "";
|
||||
|
||||
int i = 0;
|
||||
for (Entry<K1, LinkedHashMap<K2, V>> entry : content.entrySet()) {
|
||||
HashMap<K2, V> innerMap = entry.getValue();
|
||||
int j = 0;
|
||||
for (Entry<K2, V> innerEntry : innerMap.entrySet()) {
|
||||
matrix[i + 1][j] = innerEntry.getValue().toString();
|
||||
j++;
|
||||
}
|
||||
matrix[i + 1][j] = hHeaders[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
return StringHelper.PrintTable(matrix);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package flintstones.helper.data;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import flintstones.helper.StringHelper;
|
||||
|
||||
public class LinkedMapList<K, V> implements Iterable<Entry<K, LinkedList<V>>> {
|
||||
|
||||
private LinkedHashMap<K, LinkedList<V>> content = new LinkedHashMap<K, LinkedList<V>>();
|
||||
|
||||
public LinkedMapList() {
|
||||
}
|
||||
|
||||
public void add(K key, V value) {
|
||||
LinkedList<V> arr = content.get(key);
|
||||
if (arr == null)
|
||||
arr = new LinkedList<>();
|
||||
|
||||
arr.add(value);
|
||||
content.put(key, arr);
|
||||
}
|
||||
|
||||
public void put(K key, LinkedList<V> values) {
|
||||
content.put(key, values);
|
||||
}
|
||||
|
||||
public void put(K key, V[] values) {
|
||||
LinkedList<V> valuesL = new LinkedList<V>(Arrays.asList(values));
|
||||
put(key, valuesL);
|
||||
}
|
||||
|
||||
public LinkedList<V> get(K key) {
|
||||
return content.get(key);
|
||||
}
|
||||
|
||||
public LinkedList<V> getSafe(K key) {
|
||||
LinkedList<V> arr = content.get(key);
|
||||
return arr == null ? new LinkedList<V>() : arr;
|
||||
}
|
||||
|
||||
public LinkedHashMap<K, LinkedList<V>> getMap() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public boolean contains(K key) {
|
||||
return content.get(key) != null;
|
||||
}
|
||||
|
||||
public boolean contains(K key, V value) {
|
||||
LinkedList<V> arr = content.get(key);
|
||||
if (arr == null)
|
||||
return false;
|
||||
|
||||
return arr.contains(value);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
content.clear();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return content.size();
|
||||
}
|
||||
|
||||
public int size(K key) {
|
||||
LinkedList<V> arr = content.get(key);
|
||||
if (arr == null)
|
||||
return 0;
|
||||
return arr.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<K, LinkedList<V>>> iterator() {
|
||||
return content.entrySet().iterator();
|
||||
}
|
||||
|
||||
public Collection<LinkedList<V>> values() {
|
||||
return content.values();
|
||||
}
|
||||
|
||||
public Set<Map.Entry<K, LinkedList<V>>> entrySet() {
|
||||
return content.entrySet();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
return content.keySet();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String[] hHeaders = content.keySet().stream().map(k -> k.toString()).toArray(String[]::new);
|
||||
int size = content.values().iterator().next().size();
|
||||
String[] vHeaders = IntStream.rangeClosed(1, size).mapToObj(k -> "_" + k + "_").toArray(String[]::new);
|
||||
|
||||
String[][] matrix = new String[hHeaders.length+1][vHeaders.length+1];
|
||||
for(int i = 0; i < vHeaders.length; i++) {
|
||||
matrix[0][i] = vHeaders[i];
|
||||
}
|
||||
matrix[0][vHeaders.length] = "";
|
||||
|
||||
int i = 0;
|
||||
for(Entry<K,LinkedList<V>> entry : content.entrySet()) {
|
||||
LinkedList<V> innerList = entry.getValue();
|
||||
int j = 0;
|
||||
for(V innerEntry : innerList) {
|
||||
matrix[i+1][j] = innerEntry.toString();
|
||||
j++;
|
||||
}
|
||||
matrix[i+1][j] = hHeaders[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
return StringHelper.PrintTable(matrix);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package flintstones.helper.data;
|
||||
|
||||
/**
|
||||
* The Class Pair.
|
||||
*
|
||||
* @param <L> the generic type
|
||||
* @param <R> the generic type
|
||||
*/
|
||||
public class Pair<L, R> {
|
||||
|
||||
/** The left. */
|
||||
private L _left;
|
||||
|
||||
/** The right. */
|
||||
private R _right;
|
||||
|
||||
/**
|
||||
* Instantiates a new pair.
|
||||
*/
|
||||
public Pair() {
|
||||
this._left = null;
|
||||
this._right = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new pair.
|
||||
*
|
||||
* @param left the left
|
||||
* @param right the right
|
||||
*/
|
||||
public Pair(L left, R right) {
|
||||
this._left = left;
|
||||
this._right = right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the left value.
|
||||
*
|
||||
* @return the left
|
||||
*/
|
||||
public L getLeft() {
|
||||
return this._left;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the key.
|
||||
*
|
||||
* @return the key
|
||||
*/
|
||||
public L getKey() {
|
||||
return getLeft();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the right value.
|
||||
*
|
||||
* @return the right
|
||||
*/
|
||||
public R getRight() {
|
||||
return this._right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value.
|
||||
*
|
||||
* @return the value
|
||||
*/
|
||||
public R getValue() {
|
||||
return getRight();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the.
|
||||
*
|
||||
* @param left the left
|
||||
* @param right the right
|
||||
*/
|
||||
public void add(L left, R right) {
|
||||
this._left = left;
|
||||
this._right = right;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode() */
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this._left.hashCode() ^ this._right.hashCode();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object) */
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
if (!(o instanceof Pair))
|
||||
return false;
|
||||
Pair<?, ?> pairo = (Pair<?, ?>) o;
|
||||
return this._left.equals(pairo.getLeft()) && this._right.equals(pairo.getRight());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is empty.
|
||||
*
|
||||
* @return true, if is empty
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return (this._left == null) && (this._right == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear.
|
||||
*/
|
||||
public void clear() {
|
||||
this._left = null;
|
||||
this._right = null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + _left.toString() + ", " + _right.toString() + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package flintstones.helper.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* The Class PairList is a ArrayList<Pair<>>. It allows to replace and convert Maps<> to PairList<>.
|
||||
*
|
||||
* @param <L> the generic type
|
||||
* @param <R> the generic type
|
||||
*/
|
||||
public class PairList<L, R> implements Iterable< Pair<L,R> > {
|
||||
|
||||
private /** The data. */
|
||||
ArrayList< Pair<L,R> > data = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new pair list.
|
||||
*/
|
||||
public PairList() {}
|
||||
|
||||
/**
|
||||
* Instantiates a new pair list.
|
||||
*
|
||||
* @param map the map
|
||||
*/
|
||||
public PairList(HashMap<L,R> map) {
|
||||
for(Entry<L,R> entry : map.entrySet())
|
||||
put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Put.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
public void put(L key, R value) {
|
||||
data.add(new Pair<L,R>(key,value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the.
|
||||
*
|
||||
* @param index the index
|
||||
* @return the pair
|
||||
*/
|
||||
public Pair<L,R> get(int index){
|
||||
return data.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the key.
|
||||
*
|
||||
* @param index the index
|
||||
* @return the key
|
||||
*/
|
||||
public L getKey(int index) {
|
||||
return get(index).getLeft();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value.
|
||||
*
|
||||
* @param index the index
|
||||
* @return the value
|
||||
*/
|
||||
public R getValue(int index) {
|
||||
return get(index).getRight();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for( Pair<L,R> pair : data ) {
|
||||
sb.append(pair.toString() + "\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Iterable#iterator()
|
||||
*/
|
||||
@Override
|
||||
public Iterator<Pair<L, R>> iterator() {
|
||||
return data.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Size.
|
||||
*
|
||||
* @return the int
|
||||
*/
|
||||
public int size() {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
public ArrayList<Pair<L,R>> getAll(){
|
||||
return data;
|
||||
}
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
package flintstones.helper.data.adapter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import flintstones.helper.DoubleHelper;
|
||||
import flintstones.helper.StringHelper;
|
||||
import flintstones.helper.data.HashMatrix;
|
||||
import flintstones.helper.data.LinkedHashMatrix;
|
||||
|
||||
public class TableAdapter {
|
||||
|
||||
private String[] vHeader;
|
||||
private String[] hHeader;
|
||||
|
||||
private String[][] data;
|
||||
|
||||
public TableAdapter() {
|
||||
check();
|
||||
}
|
||||
|
||||
public TableAdapter(HashMap<?, ?> items) {
|
||||
|
||||
check();
|
||||
|
||||
int cols = items.size();
|
||||
hHeader = new String[cols];
|
||||
vHeader = new String[0];
|
||||
data = new String[1][cols];
|
||||
|
||||
Set<?> itemList = items.keySet();
|
||||
|
||||
int i = 0;
|
||||
for(Object item : itemList) {
|
||||
hHeader[i] = asString(item);
|
||||
data[0][i] = asString(items.get(item));
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public TableAdapter(LinkedHashMatrix<?, ?, ?> items) {
|
||||
|
||||
check();
|
||||
|
||||
int cols = items.innerSize();
|
||||
int rows = items.size();
|
||||
|
||||
hHeader = new String[cols];
|
||||
vHeader = new String[rows];
|
||||
data = new String[rows][cols];
|
||||
|
||||
int i = 0;
|
||||
for(Entry<?, ?> entry1 : items.entrySet()) {
|
||||
Object k1 = entry1.getKey();
|
||||
Object v1 = entry1.getValue();
|
||||
vHeader[i] = asString(k1);
|
||||
|
||||
int j = 0;
|
||||
LinkedHashMap<?,?> innerMap = (LinkedHashMap<?, ?>) v1;
|
||||
for(Entry<?,?> entry2 : innerMap.entrySet()) {
|
||||
Object k2 = entry2.getKey();
|
||||
Object v2 = entry2.getValue();
|
||||
|
||||
hHeader[j] = asString(k2); // Se repite en todas las iteraciones, no deberia
|
||||
data[i][j] = asString(v2);
|
||||
j++;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public TableAdapter(HashMatrix<?, ?, ?> items) {
|
||||
|
||||
check();
|
||||
|
||||
int cols = items.innerSize();
|
||||
int rows = items.size();
|
||||
|
||||
hHeader = new String[cols];
|
||||
vHeader = new String[rows];
|
||||
data = new String[rows][cols];
|
||||
|
||||
int i = 0;
|
||||
for(Entry<?, ?> entry1 : items.entrySet()) {
|
||||
Object k1 = entry1.getKey();
|
||||
Object v1 = entry1.getValue();
|
||||
vHeader[i] = asString(k1);
|
||||
|
||||
int j = 0;
|
||||
HashMap<?,?> innerMap = (HashMap<?, ?>) v1;
|
||||
for(Entry<?,?> entry2 : innerMap.entrySet()) {
|
||||
Object k2 = entry2.getKey();
|
||||
Object v2 = entry2.getValue();
|
||||
|
||||
hHeader[j] = asString(k2); // Se repite en todas las iteraciones, no deberia
|
||||
data[i][j] = asString(v2);
|
||||
j++;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private String asString(Object item) {
|
||||
|
||||
if(item instanceof Double) {
|
||||
Double val = (Double) item;
|
||||
return DoubleHelper.Draw(val);
|
||||
}
|
||||
|
||||
return StringHelper.Draw(item.toString());
|
||||
}
|
||||
|
||||
private void check(){
|
||||
if(vHeader != null || hHeader != null || data != null)
|
||||
throw new RuntimeException("No");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void setVerticalHeaders(String[] headers) {
|
||||
this.vHeader = headers;
|
||||
}
|
||||
|
||||
public String[] getVerticalHeaders() {
|
||||
return vHeader;
|
||||
}
|
||||
|
||||
public void setHorizontalHeaders(String[] headers) {
|
||||
this.hHeader = headers;
|
||||
}
|
||||
|
||||
public String[] getHorizontalHeaders() {
|
||||
return hHeader;
|
||||
}
|
||||
|
||||
public void setData(String[][] d) {
|
||||
data = d;
|
||||
}
|
||||
|
||||
public String[][] getData(){
|
||||
return data;
|
||||
}
|
||||
|
||||
public String[] getDataRow() {
|
||||
if(data.length != 1)
|
||||
throw new RuntimeException("No");
|
||||
return data[0];
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package flintstones.helper.data.text;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* The Class ClassAttributesToString.
|
||||
*/
|
||||
public class ClassAttributesToString {
|
||||
|
||||
/**
|
||||
* To string.
|
||||
*
|
||||
* @param item the item
|
||||
* @return the string
|
||||
*/
|
||||
public static String toString(Object item) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
String newLine = System.getProperty("line.separator");
|
||||
|
||||
result.append( item.getClass().getName() );
|
||||
result.append( " Object {" );
|
||||
result.append(newLine);
|
||||
|
||||
//determine fields declared in this class only (no fields of superclass)
|
||||
Field[] fields = item.getClass().getDeclaredFields();
|
||||
|
||||
//print field names paired with their values
|
||||
for ( Field field : fields ) {
|
||||
result.append(" ");
|
||||
try {
|
||||
result.append( field.getName() );
|
||||
result.append(": ");
|
||||
//requires access to private field:
|
||||
result.append( toString(field.get(item)) );
|
||||
} catch ( IllegalAccessException ex ) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
result.append(newLine);
|
||||
}
|
||||
result.append("}");
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package flintstones.helper.data.text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import flintstones.helper.data.HashMapList;
|
||||
import flintstones.helper.data.HashMatrix;
|
||||
|
||||
/**
|
||||
* The Class ClassToString.
|
||||
*
|
||||
* @author igmunoz
|
||||
*/
|
||||
/**
|
||||
* @author igmunoz
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class ClassToString {
|
||||
|
||||
/**
|
||||
* Gets the class as string.
|
||||
*
|
||||
* @param item the item
|
||||
* @return the class as string
|
||||
*/
|
||||
public static String getClassAsString(Object item) {
|
||||
|
||||
String value = item.getClass().getSimpleName();
|
||||
String newValue = "";
|
||||
if( item instanceof HashMap ) {
|
||||
newValue = getHashMapAsString((HashMap) item);
|
||||
} else if( item instanceof ArrayList ) {
|
||||
newValue = getArrayListAsString((ArrayList) item);
|
||||
} else if( item instanceof LinkedList ) {
|
||||
newValue = getLinkedListAsString((LinkedList) item);
|
||||
} else if( item instanceof HashMatrix ) {
|
||||
newValue = getHashMatrixAsString((HashMatrix) item);
|
||||
} else if( item instanceof HashMapList ) {
|
||||
newValue = getHashMapListAsString((HashMapList)item);
|
||||
}
|
||||
|
||||
if( newValue != "")
|
||||
return newValue;
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
private static String getHashMapListAsString(HashMapList matrix) {
|
||||
String value = "HashMapList<Empty>";
|
||||
if( matrix.size() > 0 ){
|
||||
value = "HashMapList<";
|
||||
value += getClassAsString(matrix.getMap().keySet().toArray()[0]);
|
||||
value += ",";
|
||||
ArrayList innerArr = (ArrayList) matrix.getMap().values().toArray()[0];
|
||||
value += getClassAsString(innerArr.iterator().next());
|
||||
value += ">";
|
||||
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String getHashMatrixAsString(HashMatrix matrix) {
|
||||
String value = "HashMatrix<Empty>";
|
||||
if( matrix.size() > 0 ){
|
||||
value = "HashMatrix<";
|
||||
value += getClassAsString(matrix.getMap().keySet().toArray()[0]);
|
||||
value += ",";
|
||||
HashMap innerMap = (HashMap) matrix.getMap().values().toArray()[0];
|
||||
value += getClassAsString(innerMap.keySet().toArray()[0]);
|
||||
value += ",";
|
||||
value += getClassAsString(innerMap.values().toArray()[0]);
|
||||
value += ">";
|
||||
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the hash map as string.
|
||||
*
|
||||
* @param map the map
|
||||
* @return the hash map as string
|
||||
*/
|
||||
private static String getHashMapAsString(HashMap map) {
|
||||
|
||||
String value = "HashMap<Empty>";
|
||||
if( map.size() > 0 ) {
|
||||
value = "HashMap<";
|
||||
value += getClassAsString(map.keySet().toArray()[0]);
|
||||
value += ",";
|
||||
value += getClassAsString(map.values().toArray()[0]);
|
||||
value += ">";
|
||||
}
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the array list as string.
|
||||
*
|
||||
* @param arr the arr
|
||||
* @return the array list as string
|
||||
*/
|
||||
private static String getArrayListAsString(ArrayList arr) {
|
||||
|
||||
String value = "ArrayList<Empty>";
|
||||
if( arr.size() > 0) {
|
||||
value = "ArrayList<";
|
||||
value += getClassAsString(arr.get(0));
|
||||
value += ">";
|
||||
}
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the linked list as string.
|
||||
*
|
||||
* @param list the list
|
||||
* @return the linked list as string
|
||||
*/
|
||||
private static String getLinkedListAsString(LinkedList list) {
|
||||
|
||||
String value = "LinkedList<Empty>";
|
||||
if( list.size() > 0) {
|
||||
value = "LinkedList<";
|
||||
value += getClassAsString(list.get(0));
|
||||
value += ">";
|
||||
}
|
||||
return value;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package flintstones.helper.data.text;
|
||||
// https://jeeeyul.wordpress.com/2012/10/18/make-system-out-println-rocks/
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
/**
|
||||
* @author Jeeeyul 2011. 11. 1. 오후 4:36:51
|
||||
* @since M1.10
|
||||
*/
|
||||
public class DebugStream extends PrintStream {
|
||||
private static final DebugStream INSTANCE = new DebugStream();
|
||||
|
||||
public static void activate() {
|
||||
System.setOut(INSTANCE);
|
||||
System.setErr(INSTANCE);
|
||||
}
|
||||
|
||||
private DebugStream() {
|
||||
super(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(Object x) {
|
||||
showLocation();
|
||||
super.println(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(String x) {
|
||||
showLocation();
|
||||
super.println(x);
|
||||
}
|
||||
|
||||
private void showLocation() {
|
||||
StackTraceElement element = Thread.currentThread().getStackTrace()[3];
|
||||
if(element.getFileName().equals("DebugHelper.java"))
|
||||
element = Thread.currentThread().getStackTrace()[4];
|
||||
super.print(MessageFormat.format("({0}:{1, number,#}) : ", element.getFileName(), element.getLineNumber()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user