Fixed javadoc warnings
This commit is contained in:
parent
2db7cd2e49
commit
1cab6609c0
20 changed files with 1301 additions and 1305 deletions
|
|
@ -43,6 +43,15 @@
|
|||
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.36" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.xerial:sqlite-jdbc:3.8.11.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.carrotsearch:junit-benchmarks:0.7.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-fileupload:commons-fileupload:1.2.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-io:commons-io:2.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: xml-apis:xml-apis:1.0.b2" level="project" />
|
||||
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.36" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.xerial:sqlite-jdbc:3.8.11.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.carrotsearch:junit-benchmarks:0.7.2" level="project" />
|
||||
</component>
|
||||
|
|
|
|||
|
|
@ -99,8 +99,8 @@ public class Hasher {
|
|||
/**
|
||||
* Returns the MD5 hash of the given file
|
||||
*
|
||||
* @param object is the file to hash
|
||||
* @return an String containing the hash
|
||||
* @param file is the file to hash
|
||||
* @return an String containing the hash
|
||||
*/
|
||||
public static String MD5(File file) throws IOException{
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -35,84 +35,84 @@ import java.util.List;
|
|||
* @author Ziver *
|
||||
*/
|
||||
public class StringUtil {
|
||||
public static final String[] sizes = new String[]{"YB", "ZB", "EB", "PB", "TB", "GB", "MB", "kB", "B"};
|
||||
|
||||
/**
|
||||
* Present a size (in bytes) as a human-readable value
|
||||
*
|
||||
* @param bytes size (in bytes)
|
||||
* @return string
|
||||
*/
|
||||
public static String formatByteSizeToString(long bytes){
|
||||
int total = sizes.length-1;
|
||||
double value = bytes;
|
||||
|
||||
for(; value > 1024 ;total--) {
|
||||
value /= 1024;
|
||||
}
|
||||
public static final String[] sizes = new String[]{"YB", "ZB", "EB", "PB", "TB", "GB", "MB", "kB", "B"};
|
||||
|
||||
value = (double)( (int)(value*10) )/10;
|
||||
return value+" "+sizes[total];
|
||||
}
|
||||
|
||||
public static String formatTimeToString(long milisec){
|
||||
StringBuilder str = new StringBuilder();
|
||||
long tmp = 0;
|
||||
|
||||
// Years
|
||||
if( milisec >= 31557032762.3361d ){
|
||||
tmp = (long) (milisec / 31557032762.3361d);
|
||||
milisec -= tmp * 31557032762.3361d;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" years ");
|
||||
else
|
||||
str.append(tmp).append(" year ");
|
||||
}
|
||||
// Months
|
||||
if( milisec >= 2629743830l ){
|
||||
tmp = (long) (milisec / 2629743830l);
|
||||
milisec -= tmp * 2629743830l;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" months ");
|
||||
else
|
||||
str.append(tmp).append(" month ");
|
||||
}
|
||||
// Days
|
||||
if( milisec >= 86400000 ){
|
||||
tmp = (long) (milisec / 86400000);
|
||||
milisec -= tmp * 86400000;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" days ");
|
||||
else
|
||||
str.append(tmp).append(" day ");
|
||||
}
|
||||
// Hours
|
||||
if( milisec >= 3600000 ){
|
||||
tmp = (long) (milisec / 3600000);
|
||||
milisec -= tmp * 3600000;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" hours ");
|
||||
else
|
||||
str.append(tmp).append(" hour ");
|
||||
}
|
||||
// Minutes
|
||||
if( milisec >= 60000 ){
|
||||
tmp = (long) (milisec / 60000);
|
||||
milisec -= tmp * 60000;
|
||||
str.append(tmp).append(" min ");
|
||||
}
|
||||
// sec
|
||||
if( milisec >= 1000 ){
|
||||
tmp = (long) (milisec / 1000);
|
||||
milisec -= tmp * 1000;
|
||||
str.append(tmp).append(" sec ");
|
||||
}
|
||||
if( milisec > 0 ){
|
||||
str.append(milisec).append(" milisec ");
|
||||
}
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
/**
|
||||
* Present a size (in bytes) as a human-readable value
|
||||
*
|
||||
* @param bytes size (in bytes)
|
||||
* @return string
|
||||
*/
|
||||
public static String formatByteSizeToString(long bytes){
|
||||
int total = sizes.length-1;
|
||||
double value = bytes;
|
||||
|
||||
for(; value > 1024 ;total--) {
|
||||
value /= 1024;
|
||||
}
|
||||
|
||||
value = (double)( (int)(value*10) )/10;
|
||||
return value+" "+sizes[total];
|
||||
}
|
||||
|
||||
public static String formatTimeToString(long milisec){
|
||||
StringBuilder str = new StringBuilder();
|
||||
long tmp = 0;
|
||||
|
||||
// Years
|
||||
if( milisec >= 31557032762.3361d ){
|
||||
tmp = (long) (milisec / 31557032762.3361d);
|
||||
milisec -= tmp * 31557032762.3361d;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" years ");
|
||||
else
|
||||
str.append(tmp).append(" year ");
|
||||
}
|
||||
// Months
|
||||
if( milisec >= 2629743830l ){
|
||||
tmp = (long) (milisec / 2629743830l);
|
||||
milisec -= tmp * 2629743830l;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" months ");
|
||||
else
|
||||
str.append(tmp).append(" month ");
|
||||
}
|
||||
// Days
|
||||
if( milisec >= 86400000 ){
|
||||
tmp = (long) (milisec / 86400000);
|
||||
milisec -= tmp * 86400000;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" days ");
|
||||
else
|
||||
str.append(tmp).append(" day ");
|
||||
}
|
||||
// Hours
|
||||
if( milisec >= 3600000 ){
|
||||
tmp = (long) (milisec / 3600000);
|
||||
milisec -= tmp * 3600000;
|
||||
if( tmp > 1 )
|
||||
str.append(tmp).append(" hours ");
|
||||
else
|
||||
str.append(tmp).append(" hour ");
|
||||
}
|
||||
// Minutes
|
||||
if( milisec >= 60000 ){
|
||||
tmp = (long) (milisec / 60000);
|
||||
milisec -= tmp * 60000;
|
||||
str.append(tmp).append(" min ");
|
||||
}
|
||||
// sec
|
||||
if( milisec >= 1000 ){
|
||||
tmp = (long) (milisec / 1000);
|
||||
milisec -= tmp * 1000;
|
||||
str.append(tmp).append(" sec ");
|
||||
}
|
||||
if( milisec > 0 ){
|
||||
str.append(milisec).append(" milisec ");
|
||||
}
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a String where the number has been prefixed
|
||||
|
|
@ -130,20 +130,20 @@ public class StringUtil {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param delimiter a String delimiter that will be added between every entry in the list
|
||||
* @param array a array of object that toString() will be called on
|
||||
* @return a String containing all entries in the list with the specified delimiter in between entries
|
||||
*/
|
||||
public static <T> String join(String delimiter, T... array){
|
||||
return join(delimiter, Arrays.asList(array));
|
||||
}
|
||||
/**
|
||||
* @param delimiter a String delimiter that will be added between every entry in the list
|
||||
* @param list a list of object that toString() will be called on
|
||||
* @param array a array of object that toString() will be called on
|
||||
* @return a String containing all entries in the list with the specified delimiter in between entries
|
||||
*/
|
||||
public static String join(String delimiter, Iterable<?> list){
|
||||
public static <T> String join(String delimiter, T... array){
|
||||
return join(delimiter, Arrays.asList(array));
|
||||
}
|
||||
/**
|
||||
* @param delimiter a String delimiter that will be added between every entry in the list
|
||||
* @param list a list of object that toString() will be called on
|
||||
* @return a String containing all entries in the list with the specified delimiter in between entries
|
||||
*/
|
||||
public static String join(String delimiter, Iterable<?> list){
|
||||
StringBuilder str = new StringBuilder();
|
||||
Iterator<?> it = list.iterator();
|
||||
if(it.hasNext()) {
|
||||
|
|
@ -155,74 +155,73 @@ public class StringUtil {
|
|||
return str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the given char and whitespace at the beginning and the end
|
||||
*
|
||||
* @param str is the string to trim
|
||||
* @param trim is the char to trim
|
||||
* @return a trimmed String
|
||||
*/
|
||||
public static String trim(String str, char trim){
|
||||
if( str == null || str.isEmpty() )
|
||||
return str;
|
||||
int start=0, stop=str.length();
|
||||
// The beginning
|
||||
for(int i=0; i<str.length() ;i++){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
start = i+1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// The end
|
||||
for(int i=str.length()-1; i>start ;i--){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
stop = i;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if( start >= str.length() )
|
||||
return "";
|
||||
//System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop);
|
||||
return str.substring(start, stop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the whitespace and quotes if the string starts and ends with one
|
||||
*
|
||||
* @param str is the string to trim
|
||||
* @return
|
||||
*/
|
||||
public static String trimQuotes(String str){
|
||||
if( str == null )
|
||||
return null;
|
||||
str = str.trim();
|
||||
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
||||
str = str.substring(1, str.length()-1);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
/**
|
||||
* Trims the given char and whitespace at the beginning and the end
|
||||
*
|
||||
* @param str is the string to trim
|
||||
* @param trim is the char to trim
|
||||
* @return a trimmed String
|
||||
*/
|
||||
public static String trim(String str, char trim){
|
||||
if( str == null || str.isEmpty() )
|
||||
return str;
|
||||
int start=0, stop=str.length();
|
||||
// The beginning
|
||||
for(int i=0; i<str.length() ;i++){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
start = i+1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// The end
|
||||
for(int i=str.length()-1; i>start ;i--){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
stop = i;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if( start >= str.length() )
|
||||
return "";
|
||||
//System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop);
|
||||
return str.substring(start, stop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the whitespace and quotes if the string starts and ends with one
|
||||
*
|
||||
* @param str is the string to trim
|
||||
*/
|
||||
public static String trimQuotes(String str){
|
||||
if( str == null )
|
||||
return null;
|
||||
str = str.trim();
|
||||
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
||||
str = str.substring(1, str.length()-1);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
private static ArrayList<String> SPACES = new ArrayList<>();
|
||||
/**
|
||||
* @return A string containing a specific amount of spaces
|
||||
*/
|
||||
public static String getSpaces(int i){
|
||||
if(SPACES.size() <= i){ // Do we need to generate more spaces?
|
||||
synchronized (SPACES) { // Make sure no one else updates the list at the same time
|
||||
if(SPACES.size() <= i) { // Make sure the previous synchronized thread hasn't already generated strings
|
||||
if (SPACES.isEmpty())
|
||||
SPACES.add("");
|
||||
for (int j = SPACES.size(); j <= i; j++) {
|
||||
SPACES.add(SPACES.get(j - 1) + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return SPACES.get(i);
|
||||
}
|
||||
private static ArrayList<String> SPACES = new ArrayList<>();
|
||||
/**
|
||||
* @return A string containing a specific amount of spaces
|
||||
*/
|
||||
public static String getSpaces(int i){
|
||||
if(SPACES.size() <= i){ // Do we need to generate more spaces?
|
||||
synchronized (SPACES) { // Make sure no one else updates the list at the same time
|
||||
if(SPACES.size() <= i) { // Make sure the previous synchronized thread hasn't already generated strings
|
||||
if (SPACES.isEmpty())
|
||||
SPACES.add("");
|
||||
for (int j = SPACES.size(); j <= i; j++) {
|
||||
SPACES.add(SPACES.get(j - 1) + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return SPACES.get(i);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -235,7 +234,7 @@ public class StringUtil {
|
|||
* @param delimiter a single character delimiter
|
||||
* @return a List with all data between the delimiter
|
||||
*/
|
||||
public static List<String> split(String str, char delimiter){
|
||||
public static List<String> split(String str, char delimiter){
|
||||
ArrayList<String> splitList = new ArrayList<>();
|
||||
int from = 0, to = 0;
|
||||
while (to >= 0) {
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ import java.math.BigInteger;
|
|||
* The algorithm solves the discreet log equation x^2 = n mod p
|
||||
*
|
||||
* @author Ziver
|
||||
* @see http://en.wikipedia.org/wiki/Shanks-Tonelli_algorithm
|
||||
* @see <a href="http://en.wikipedia.org/wiki/Shanks-Tonelli_algorithm">Wikipedia</a>
|
||||
*/
|
||||
public class ShanksTonelliAlgo {
|
||||
public static BigInteger calc(BigInteger n, BigInteger p){
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ public class QuickSort{
|
|||
/**
|
||||
* Sort the elements in ascending order using Quicksort.
|
||||
*
|
||||
* @param A is the list to sort.
|
||||
* @param list is the list to sort.
|
||||
*/
|
||||
public static void sort(SortableDataList<?> list){
|
||||
sort(list, 0, list.size()-1, MIDDLE_PIVOT, true);
|
||||
|
|
@ -49,9 +49,9 @@ public class QuickSort{
|
|||
/**
|
||||
* Sort the elements in ascending order using Quicksort.
|
||||
*
|
||||
* @param A is the list to sort.
|
||||
* @param type is the type of pivot
|
||||
* @param insert is if insertion sort will be used
|
||||
* @param list is the list to sort.
|
||||
* @param type is the type of pivot
|
||||
* @param insert is if insertion sort will be used
|
||||
*/
|
||||
public static void sort(SortableDataList<?> list, int type, boolean insert){
|
||||
sort(list, 0, list.size()-1, type, insert);
|
||||
|
|
@ -62,10 +62,10 @@ public class QuickSort{
|
|||
* Reference: http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/quick/quicken.htm
|
||||
* Complexity: O(n*log n) normally, but O(n^2) if the pivot is bad
|
||||
*
|
||||
* @param A is the list to sort.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
* @param type is the type of pivot to use
|
||||
* @param list is the list to sort.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
* @param type is the type of pivot to use
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public static void sort(SortableDataList list, int start, int stop, int type, boolean insertionSort){
|
||||
|
|
|
|||
|
|
@ -39,50 +39,50 @@ public class SimpleSort{
|
|||
* witch is the slowest of the algorithms.
|
||||
* Complexity: O(n^2).
|
||||
*
|
||||
* @param A is the list to sort.
|
||||
* @param list is the list to sort.
|
||||
*/
|
||||
public static void bubbleSort(SortableDataList<?> list){
|
||||
bubbleSort(list, 0, list.size());
|
||||
public static void bubbleSort(SortableDataList<?> list){
|
||||
bubbleSort(list, 0, list.size());
|
||||
}
|
||||
/**
|
||||
/**
|
||||
* Sort the elements in ascending order using bubble sort
|
||||
* witch is the slowest of the algorithms.
|
||||
* Complexity: O(n^2).
|
||||
*
|
||||
* @param A is an array of integers.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
* @param list is an array of integers.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
*/
|
||||
public static void bubbleSort(SortableDataList<?> list, int start, int stop){
|
||||
public static void bubbleSort(SortableDataList<?> list, int start, int stop){
|
||||
for(int i=start; i<stop ;++i){
|
||||
for(int j=stop-2; i<=j ;--j){
|
||||
if(list.compare(j, j+1) > 0){
|
||||
list.swap(j, j+1);
|
||||
list.swap(j, j+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort the elements in ascending order using selection sort
|
||||
* witch in practice is 40% faster than bubble sort.
|
||||
* Complexity: O(n^2).
|
||||
*
|
||||
* @param list is the list to sort.
|
||||
* @param list is the list to sort.
|
||||
*/
|
||||
public static void selectionSort(SortableDataList<?> list){
|
||||
selectionSort(list, 0, list.size());
|
||||
public static void selectionSort(SortableDataList<?> list){
|
||||
selectionSort(list, 0, list.size());
|
||||
}
|
||||
/**
|
||||
* Sort the elements in ascending order using selection sort
|
||||
* witch in practice is 40% faster than bubble sort.
|
||||
* Complexity: O(n^2).
|
||||
*
|
||||
* @param list is the list to sort.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
* @param list is the list to sort.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
*/
|
||||
public static void selectionSort(SortableDataList<?> list, int start, int stop){
|
||||
public static void selectionSort(SortableDataList<?> list, int start, int stop){
|
||||
for (int i = start; i < stop - 1; i++) {
|
||||
// find index m of the minimum element in v[i..n-1]
|
||||
int m = i;
|
||||
|
|
@ -94,32 +94,32 @@ public class SimpleSort{
|
|||
list.swap(i, m);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort the elements in ascending order using insertion sort
|
||||
* witch in practice is 5 times faster than bubble sort.
|
||||
* Complexity: O(n^2).
|
||||
*
|
||||
* @param A is a list to sort.
|
||||
* @param list is a list to sort.
|
||||
*/
|
||||
public static void insertionSort(SortableDataList<?> list){
|
||||
insertionSort(list, 0, list.size());
|
||||
public static void insertionSort(SortableDataList<?> list){
|
||||
insertionSort(list, 0, list.size());
|
||||
}
|
||||
/**
|
||||
/**
|
||||
* Sort the elements in ascending order using insertion sort
|
||||
* witch in practice is 5 times faster than bubble sort.
|
||||
* Complexity: O(n^2).
|
||||
*
|
||||
* @param A is an array of integers.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
* @param list is an array of integers.
|
||||
* @param start is the index to start from
|
||||
* @param stop is the index to stop
|
||||
*/
|
||||
public static void insertionSort(SortableDataList<?> list, int start, int stop){
|
||||
for(int i=start; i<stop ;++i){
|
||||
for(int j=i; start<j && list.compare(j-1, j)>0 ;--j){
|
||||
list.swap(j-1, j);
|
||||
}
|
||||
}
|
||||
public static void insertionSort(SortableDataList<?> list, int start, int stop){
|
||||
for(int i=start; i<stop ;++i){
|
||||
for(int j=i; start<j && list.compare(j-1, j)>0 ;--j){
|
||||
list.swap(j-1, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,60 +25,59 @@
|
|||
package zutil.algo.sort.sortable;
|
||||
|
||||
public interface SortableDataList<T>{
|
||||
|
||||
/**
|
||||
* Returns a is a specific index i the list
|
||||
*
|
||||
* @param i is the index
|
||||
* @return
|
||||
*/
|
||||
public T get(int i);
|
||||
|
||||
/**
|
||||
* Sets an Object in the specified index
|
||||
*
|
||||
* @param i is the index
|
||||
* @param o is the Object
|
||||
*/
|
||||
public void set(int i, T o);
|
||||
|
||||
/**
|
||||
* Returns the size of the list
|
||||
*
|
||||
* @return the size of the list
|
||||
*/
|
||||
public int size();
|
||||
|
||||
/**
|
||||
* Swaps the given indexes
|
||||
*
|
||||
* @param a is the first index
|
||||
* @param b is the second index
|
||||
*/
|
||||
public void swap(int a, int b);
|
||||
|
||||
/**
|
||||
* Compares to indexes and returns:
|
||||
* <0 if a<b ,
|
||||
* >0 if a>b ,
|
||||
* =0 if a=b
|
||||
*
|
||||
* @param a is the first index to compare
|
||||
* @param b is the second index to compare
|
||||
* @return Look at the info
|
||||
*/
|
||||
public int compare(int a, int b);
|
||||
|
||||
/**
|
||||
* Compares to indexes and returns:
|
||||
* <0 if a<b ,
|
||||
* >0 if a>b ,
|
||||
* =0 if a=b
|
||||
*
|
||||
* @param a is the first index to compare
|
||||
* @param b is the second Object to compare
|
||||
* @return Look at the info
|
||||
*/
|
||||
public int compare(int a, T b);
|
||||
|
||||
/**
|
||||
* Returns a is a specific index i the list
|
||||
*
|
||||
* @param i is the index
|
||||
*/
|
||||
T get(int i);
|
||||
|
||||
/**
|
||||
* Sets an Object in the specified index
|
||||
*
|
||||
* @param i is the index
|
||||
* @param o is the Object
|
||||
*/
|
||||
void set(int i, T o);
|
||||
|
||||
/**
|
||||
* Returns the size of the list
|
||||
*
|
||||
* @return the size of the list
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* Swaps the given indexes
|
||||
*
|
||||
* @param a is the first index
|
||||
* @param b is the second index
|
||||
*/
|
||||
void swap(int a, int b);
|
||||
|
||||
/**
|
||||
* Compares to indexes and returns:
|
||||
* <0 if a<b ,
|
||||
* >0 if a>b ,
|
||||
* =0 if a=b
|
||||
*
|
||||
* @param a is the first index to compare
|
||||
* @param b is the second index to compare
|
||||
* @return Look at the info
|
||||
*/
|
||||
int compare(int a, int b);
|
||||
|
||||
/**
|
||||
* Compares to indexes and returns:
|
||||
* <0 if a<b ,
|
||||
* >0 if a>b ,
|
||||
* =0 if a=b
|
||||
*
|
||||
* @param a is the first index to compare
|
||||
* @param b is the second Object to compare
|
||||
* @return Look at the info
|
||||
*/
|
||||
int compare(int a, T b);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,123 +32,123 @@ import java.awt.geom.Line2D;
|
|||
import java.util.logging.Logger;
|
||||
|
||||
public abstract class AbstractChart extends JPanel{
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** The offset from the borders of the panel in pixels */
|
||||
public static final int PADDING = 20;
|
||||
|
||||
protected ChartData data;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected Rectangle chartBound;
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
|
||||
protected void paintComponent(Graphics g){
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
|
||||
Rectangle bound = drawScale( g2 );
|
||||
drawChart( g2, bound );
|
||||
}
|
||||
|
||||
protected Rectangle drawScale(Graphics2D g2){
|
||||
if( data == null )
|
||||
return null;
|
||||
|
||||
// update values
|
||||
width = this.getWidth();
|
||||
height = this.getHeight();
|
||||
Rectangle bound = new Rectangle();
|
||||
|
||||
// Values
|
||||
int stepLength = 7;
|
||||
|
||||
/////// Temp values
|
||||
// Calculate Font sizes
|
||||
FontMetrics metric = g2.getFontMetrics();
|
||||
int fontHeight = metric.getHeight();
|
||||
int fontXWidth = 0;
|
||||
int fontYWidth = 0;
|
||||
for( Point p : data.getPoints() ){
|
||||
int length = 0;
|
||||
String tmp = data.getXString( p.x );
|
||||
if( tmp != null ) length = metric.stringWidth( tmp );
|
||||
else length = metric.stringWidth( ""+p.x );
|
||||
fontXWidth = Math.max(length, fontXWidth);
|
||||
|
||||
tmp = data.getXString( p.y );
|
||||
if( tmp != null ) length = metric.stringWidth( tmp );
|
||||
else length = metric.stringWidth( ""+p.y );
|
||||
fontYWidth = Math.max(length, fontYWidth);
|
||||
}
|
||||
// Calculate origo
|
||||
Point origo = new Point( PADDING+fontYWidth+stepLength, height-PADDING-fontHeight-stepLength );
|
||||
bound.x = (int) (origo.getX()+1);
|
||||
bound.y = PADDING;
|
||||
bound.width = width-bound.x-PADDING;
|
||||
bound.height = (int) (origo.getY()-PADDING-1);
|
||||
// Calculate Axis scales
|
||||
double xScale = (double)(Math.abs(data.getMaxX())+Math.abs(data.getMinX()))/bound.width;
|
||||
double yScale = (double)(Math.abs(data.getMaxY())+Math.abs(data.getMinY()))/bound.height;
|
||||
|
||||
|
||||
/////// Draw
|
||||
// Y Axis
|
||||
g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX(), origo.getY() ));
|
||||
// X Axis
|
||||
g2.draw( new Line2D.Double( origo.getX(), origo.getY(), width-PADDING, origo.getY() ));
|
||||
// Y Axis steps and labels
|
||||
g2.draw( new Line2D.Double( origo.getX(), origo.getY(), origo.getX()-stepLength, origo.getY() ));
|
||||
g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX()-stepLength, PADDING ));
|
||||
|
||||
// X Axis steps and labels
|
||||
g2.draw( new Line2D.Double( width-PADDING, origo.getY(), width-PADDING, origo.getY()+stepLength ));
|
||||
|
||||
// DEBUG
|
||||
/*
|
||||
g2.setColor(Color.red);
|
||||
g2.drawRect(bound.x, bound.y, bound.width, bound.height);
|
||||
*/
|
||||
return bound;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called after the chart scale has been drawn.
|
||||
* This method will draw the actual chart
|
||||
*
|
||||
* @param g is the Graphics object that will paint the chart
|
||||
* @param bound is the bounds of the chart, the drawing should not exceed this bound
|
||||
*/
|
||||
protected abstract void drawChart(Graphics2D g2, Rectangle bound);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the data that will be drawn.
|
||||
*
|
||||
* @param data is the data to draw
|
||||
*/
|
||||
public void setChartData(ChartData data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a x value to ax pixel coordinate
|
||||
*
|
||||
* @param x is the x data value
|
||||
* @return pixel coordinate, or 0 if the chart have not been drawn yet.
|
||||
*/
|
||||
protected int getXCoordinate(int x){
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a y value to a y pixel coordinate
|
||||
*
|
||||
* @param y is the y data value
|
||||
* @return pixel coordinate, or 0 if the chart have not been drawn yet.
|
||||
*/
|
||||
protected int getYCoordinate(int y){
|
||||
return 0;
|
||||
}
|
||||
/** The offset from the borders of the panel in pixels */
|
||||
public static final int PADDING = 20;
|
||||
|
||||
protected ChartData data;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected Rectangle chartBound;
|
||||
|
||||
|
||||
|
||||
protected void paintComponent(Graphics g){
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
|
||||
Rectangle bound = drawScale( g2 );
|
||||
drawChart( g2, bound );
|
||||
}
|
||||
|
||||
protected Rectangle drawScale(Graphics2D g2){
|
||||
if( data == null )
|
||||
return null;
|
||||
|
||||
// update values
|
||||
width = this.getWidth();
|
||||
height = this.getHeight();
|
||||
Rectangle bound = new Rectangle();
|
||||
|
||||
// Values
|
||||
int stepLength = 7;
|
||||
|
||||
/////// Temp values
|
||||
// Calculate Font sizes
|
||||
FontMetrics metric = g2.getFontMetrics();
|
||||
int fontHeight = metric.getHeight();
|
||||
int fontXWidth = 0;
|
||||
int fontYWidth = 0;
|
||||
for( Point p : data.getPoints() ){
|
||||
int length = 0;
|
||||
String tmp = data.getXString( p.x );
|
||||
if( tmp != null ) length = metric.stringWidth( tmp );
|
||||
else length = metric.stringWidth( ""+p.x );
|
||||
fontXWidth = Math.max(length, fontXWidth);
|
||||
|
||||
tmp = data.getXString( p.y );
|
||||
if( tmp != null ) length = metric.stringWidth( tmp );
|
||||
else length = metric.stringWidth( ""+p.y );
|
||||
fontYWidth = Math.max(length, fontYWidth);
|
||||
}
|
||||
// Calculate origo
|
||||
Point origo = new Point( PADDING+fontYWidth+stepLength, height-PADDING-fontHeight-stepLength );
|
||||
bound.x = (int) (origo.getX()+1);
|
||||
bound.y = PADDING;
|
||||
bound.width = width-bound.x-PADDING;
|
||||
bound.height = (int) (origo.getY()-PADDING-1);
|
||||
// Calculate Axis scales
|
||||
double xScale = (double)(Math.abs(data.getMaxX())+Math.abs(data.getMinX()))/bound.width;
|
||||
double yScale = (double)(Math.abs(data.getMaxY())+Math.abs(data.getMinY()))/bound.height;
|
||||
|
||||
|
||||
/////// Draw
|
||||
// Y Axis
|
||||
g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX(), origo.getY() ));
|
||||
// X Axis
|
||||
g2.draw( new Line2D.Double( origo.getX(), origo.getY(), width-PADDING, origo.getY() ));
|
||||
// Y Axis steps and labels
|
||||
g2.draw( new Line2D.Double( origo.getX(), origo.getY(), origo.getX()-stepLength, origo.getY() ));
|
||||
g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX()-stepLength, PADDING ));
|
||||
|
||||
// X Axis steps and labels
|
||||
g2.draw( new Line2D.Double( width-PADDING, origo.getY(), width-PADDING, origo.getY()+stepLength ));
|
||||
|
||||
// DEBUG
|
||||
/*
|
||||
g2.setColor(Color.red);
|
||||
g2.drawRect(bound.x, bound.y, bound.width, bound.height);
|
||||
*/
|
||||
return bound;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called after the chart scale has been drawn.
|
||||
* This method will draw the actual chart
|
||||
*
|
||||
* @param g2 is the Graphics object that will paint the chart
|
||||
* @param bound is the bounds of the chart, the drawing should not exceed this bound
|
||||
*/
|
||||
protected abstract void drawChart(Graphics2D g2, Rectangle bound);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the data that will be drawn.
|
||||
*
|
||||
* @param data is the data to draw
|
||||
*/
|
||||
public void setChartData(ChartData data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a x value to ax pixel coordinate
|
||||
*
|
||||
* @param x is the x data value
|
||||
* @return pixel coordinate, or 0 if the chart have not been drawn yet.
|
||||
*/
|
||||
protected int getXCoordinate(int x){
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a y value to a y pixel coordinate
|
||||
*
|
||||
* @param y is the y data value
|
||||
* @return pixel coordinate, or 0 if the chart have not been drawn yet.
|
||||
*/
|
||||
protected int getYCoordinate(int y){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import java.util.logging.Logger;
|
|||
|
||||
/**
|
||||
* A intermediate class for loading Objects of generic Classes.
|
||||
* The extending class must set the "superBean" parameter to true in {@link @DBBean.DBTable}.
|
||||
* The extending class must set the "superBean" parameter to true in {@link DBBean.DBTable}.
|
||||
* The Object that is stored must use Configurator to define what fields that should be stored.
|
||||
*
|
||||
* This class needs to fields in DB:
|
||||
|
|
|
|||
|
|
@ -30,367 +30,360 @@ package zutil.image;
|
|||
*
|
||||
*/
|
||||
public class RAWImageUtil {
|
||||
|
||||
/**
|
||||
* Returns the peek value in the image
|
||||
*
|
||||
* @param data The image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return The peak value of the image
|
||||
*/
|
||||
public static int getPeakValue(int[][][] data) {
|
||||
return getPeakValue(data, 0, 0, data[0].length, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the peek value in the image
|
||||
*
|
||||
* @param data The image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return The peak value of the image
|
||||
*/
|
||||
public static int getPeakValue(int[][][] data, int startX, int startY, int stopX, int stopY) {
|
||||
int peak = 0;
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
if(data[y][x][1] > peak) peak = data[y][x][1];
|
||||
if(data[y][x][2] > peak) peak = data[y][x][2];
|
||||
if(data[y][x][3] > peak) peak = data[y][x][3];
|
||||
}
|
||||
}
|
||||
return peak;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes the image data by the given scale
|
||||
*
|
||||
* @param data The image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param scale The scale to normalize the image by
|
||||
*/
|
||||
public static void normalize(int[][][] data, int startX, int startY, int stopX, int stopY, double scale) {
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] = (int)(data[y][x][1] * scale);
|
||||
data[y][x][2] = (int)(data[y][x][2] * scale);
|
||||
data[y][x][3] = (int)(data[y][x][3] * scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes the image data by the given scale
|
||||
*
|
||||
* @param output The output data array
|
||||
* @param data The image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param scale The scale to normalize the image by
|
||||
*/
|
||||
public static void normalize(int[][][] output, int[][][] data, int startX, int startY, int stopX, int stopY, double scale) {
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
output[y][x][1] = (int)(data[y][x][1] * scale);
|
||||
output[y][x][2] = (int)(data[y][x][2] * scale);
|
||||
output[y][x][3] = (int)(data[y][x][3] * scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns the peek value in the image
|
||||
*
|
||||
* @param data The image data
|
||||
* @return The peak value of the image
|
||||
*/
|
||||
public static int getPeakValue(int[][][] data) {
|
||||
return getPeakValue(data, 0, 0, data[0].length, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the RMS value of the image
|
||||
* (The RMS value is a measure of the width of the color distribution.)
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return The RMS value for the image
|
||||
*/
|
||||
public static int getRMS(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int pixelCount = 0;
|
||||
long accum = 0;
|
||||
for(int y=startY; y <stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
accum += data[y][x][1] * data[y][x][1];
|
||||
accum += data[y][x][2] * data[y][x][2];
|
||||
accum += data[y][x][3] * data[y][x][3];
|
||||
pixelCount += 3;
|
||||
}
|
||||
}
|
||||
int meanSquare = (int)(accum/pixelCount);
|
||||
int rms = (int)(Math.sqrt(meanSquare));
|
||||
return rms;
|
||||
}
|
||||
/**
|
||||
* Returns the peek value in the image
|
||||
*
|
||||
* @param data The image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the peak value of the image
|
||||
*/
|
||||
public static int getPeakValue(int[][][] data, int startX, int startY, int stopX, int stopY) {
|
||||
int peak = 0;
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
if(data[y][x][1] > peak) peak = data[y][x][1];
|
||||
if(data[y][x][2] > peak) peak = data[y][x][2];
|
||||
if(data[y][x][3] > peak) peak = data[y][x][3];
|
||||
}
|
||||
}
|
||||
return peak;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies the given image data by the given value
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param scale is the number to scale the image color by
|
||||
*/
|
||||
public static void scale(int[][][] data, int startX, int startY, int stopX, int stopY, double scale){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] *= scale;
|
||||
data[y][x][2] *= scale;
|
||||
data[y][x][3] *= scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Normalizes the image data by the given scale
|
||||
*
|
||||
* @param data the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param scale The scale to normalize the image by
|
||||
*/
|
||||
public static void normalize(int[][][] data, int startX, int startY, int stopX, int stopY, double scale) {
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] = (int)(data[y][x][1] * scale);
|
||||
data[y][x][2] = (int)(data[y][x][2] * scale);
|
||||
data[y][x][3] = (int)(data[y][x][3] * scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mean value of the given image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @return the mean value of the image
|
||||
*/
|
||||
public static int getMeanValue(int[][][] data){
|
||||
return getMeanValue(data, 0, 0, data[0].length, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mean value of the given image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the mean value of the image
|
||||
*/
|
||||
public static int getMeanValue(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int[] tmp = getMeanArray(data, startX, startY, stopX, stopY);
|
||||
return (tmp[0] + tmp[1] + tmp[2])/3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an mean array containing a mean value for each color
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the mean value of the image
|
||||
*/
|
||||
public static int[] getMeanArray(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int mean[] = new int[3];
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
mean[0] += data[y][x][1];
|
||||
mean[1] += data[y][x][2];
|
||||
mean[2] += data[y][x][3];
|
||||
}
|
||||
}
|
||||
// calculate the mean value
|
||||
int pixelCount = (stopY-startY)*(stopX-startX);
|
||||
mean[0] /= pixelCount;
|
||||
mean[1] /= pixelCount;
|
||||
mean[2] /= pixelCount;
|
||||
|
||||
return mean;
|
||||
}
|
||||
/**
|
||||
* Normalizes the image data by the given scale
|
||||
*
|
||||
* @param output the output data array
|
||||
* @param data the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param scale the scale to normalize the image by
|
||||
*/
|
||||
public static void normalize(int[][][] output, int[][][] data, int startX, int startY, int stopX, int stopY, double scale) {
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
output[y][x][1] = (int)(data[y][x][1] * scale);
|
||||
output[y][x][2] = (int)(data[y][x][2] * scale);
|
||||
output[y][x][3] = (int)(data[y][x][3] * scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* removes the mean value from the image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is the mean value
|
||||
*/
|
||||
public static void remMeanValue(int[][][] data, int startX, int startY, int stopX, int stopY, int mean){
|
||||
addMeanValue(data, startX, startY, stopX, stopY, -mean);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the mean value to the image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is the mean value
|
||||
*/
|
||||
public static void addMeanValue(int[][][] data, int startX, int startY, int stopX, int stopY, int mean){
|
||||
addMeanArray(data, startX, startY, stopX, stopY, new int[]{mean, mean, mean});
|
||||
}
|
||||
|
||||
/**
|
||||
* removes an mean array containing a mean value for each color
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is an array of length 3 containing a mean value for each color RGB
|
||||
*/
|
||||
public static void remMeanArray(int[][][] data, int startX, int startY, int stopX, int stopY, int[] mean){
|
||||
addMeanArray(data, startX, startY, stopX, stopY, new int[]{-mean[0], -mean[1], -mean[2]});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an mean array containing a mean value for each color
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is an array of length 3 containing a mean value for each color RGB
|
||||
*/
|
||||
public static void addMeanArray(int[][][] data, int startX, int startY, int stopX, int stopY, int[] mean){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] += mean[0];
|
||||
data[y][x][2] += mean[1];
|
||||
data[y][x][3] += mean[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all the pixel data from the image to the new array
|
||||
*
|
||||
* @param data The data to copy
|
||||
* @param xStart X start position on the source
|
||||
* @param yStart Y start position on the source
|
||||
* @param width The amount of pixels to copy
|
||||
* @param hight The amount of pixels to copy
|
||||
* @return A copy of the data array
|
||||
*/
|
||||
public static int[][][] crop(int[][][] data, int xStart, int yStart, int width, int hight){
|
||||
return crop(data, xStart, yStart, null, 0, 0, width, hight);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all the pixel data from the image to the new array
|
||||
*
|
||||
* @param data The data to copy
|
||||
* @param xData X start position in the source
|
||||
* @param yData Y start position in the source
|
||||
* @param crop The destination
|
||||
* @param xCrop X start position in the destination
|
||||
* @param yCrop Y start position in the destination
|
||||
* @param width The amount of pixels to copy
|
||||
* @param hight The amount of pixels to copy
|
||||
* @return A copy of the data array
|
||||
*/
|
||||
public static int[][][] crop(int[][][] data, int xData, int yData, int[][][] crop, int xCrop, int yCrop, int width, int hight){
|
||||
if(crop==null) crop = new int[width][hight][4];
|
||||
for(int y=0; y<width ;y++){
|
||||
for(int x=0; x<hight ;x++){
|
||||
crop[y+yData][x+xData][0] = data[y+yCrop][x+xCrop][0];
|
||||
crop[y+yData][x+xData][1] = data[y+yCrop][x+xCrop][1];
|
||||
crop[y+yData][x+xData][2] = data[y+yCrop][x+xCrop][2];
|
||||
crop[y+yData][x+xData][3] = data[y+yCrop][x+xCrop][3];
|
||||
}
|
||||
}
|
||||
return crop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given array to a new one that it returns
|
||||
*
|
||||
* @param data The data to duplicate
|
||||
* @return an copy of the array
|
||||
*/
|
||||
public static int[][][] copyArray(int[][][] data){
|
||||
return copyArray(data, 0, 0, data[0].length, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given array to a new one that it returns
|
||||
*
|
||||
* @param data The data to duplicate
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return The array copy
|
||||
*/
|
||||
public static int[][][] copyArray(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int[][][] copy = new int[data.length][data[0].length][4];
|
||||
return copyArray(data, copy, startX, startY, stopX, stopY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given array to a new one that it returns
|
||||
*
|
||||
* @param data The data to duplicate
|
||||
* @param dest is the array to copy the data to
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the dest array
|
||||
*/
|
||||
public static int[][][] copyArray(int[][][] data, int[][][] dest, int startX, int startY, int stopX, int stopY){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
dest[y][x][0] = data[y][x][0];
|
||||
dest[y][x][1] = data[y][x][1];
|
||||
dest[y][x][2] = data[y][x][2];
|
||||
dest[y][x][3] = data[y][x][3];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
/**
|
||||
* Returns the RMS value of the image
|
||||
* (The RMS value is a measure of the width of the color distribution.)
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the RMS value for the image
|
||||
*/
|
||||
public static int getRMS(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int pixelCount = 0;
|
||||
long accum = 0;
|
||||
for(int y=startY; y <stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
accum += data[y][x][1] * data[y][x][1];
|
||||
accum += data[y][x][2] * data[y][x][2];
|
||||
accum += data[y][x][3] * data[y][x][3];
|
||||
pixelCount += 3;
|
||||
}
|
||||
}
|
||||
int meanSquare = (int)(accum/pixelCount);
|
||||
int rms = (int)(Math.sqrt(meanSquare));
|
||||
return rms;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method clips the values of the pixel so that they
|
||||
* are in the range 0-255
|
||||
*
|
||||
* @param data The image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
*/
|
||||
public static void clip(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] = clip(data[y][x][1]);
|
||||
data[y][x][2] = clip(data[y][x][2]);
|
||||
data[y][x][3] = clip(data[y][x][3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Multiplies the given image data by the given value
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param scale is the number to scale the image color by
|
||||
*/
|
||||
public static void scale(int[][][] data, int startX, int startY, int stopX, int stopY, double scale){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] *= scale;
|
||||
data[y][x][2] *= scale;
|
||||
data[y][x][3] *= scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method clips the values of a color so that it
|
||||
* is in the range 0-255
|
||||
*
|
||||
* @param color
|
||||
* @return
|
||||
*/
|
||||
public static int clip(int color){
|
||||
if(color < 0)
|
||||
return 0;
|
||||
else if(color > 255)
|
||||
return 255;
|
||||
else
|
||||
return color;
|
||||
}
|
||||
/**
|
||||
* Returns the mean value of the given image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @return the mean value of the image
|
||||
*/
|
||||
public static int getMeanValue(int[][][] data){
|
||||
return getMeanValue(data, 0, 0, data[0].length, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mean value of the given image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the mean value of the image
|
||||
*/
|
||||
public static int getMeanValue(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int[] tmp = getMeanArray(data, startX, startY, stopX, stopY);
|
||||
return (tmp[0] + tmp[1] + tmp[2])/3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an mean array containing a mean value for each color
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the mean value of the image
|
||||
*/
|
||||
public static int[] getMeanArray(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int mean[] = new int[3];
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
mean[0] += data[y][x][1];
|
||||
mean[1] += data[y][x][2];
|
||||
mean[2] += data[y][x][3];
|
||||
}
|
||||
}
|
||||
// calculate the mean value
|
||||
int pixelCount = (stopY-startY)*(stopX-startX);
|
||||
mean[0] /= pixelCount;
|
||||
mean[1] /= pixelCount;
|
||||
mean[2] /= pixelCount;
|
||||
|
||||
return mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* removes the mean value from the image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is the mean value
|
||||
*/
|
||||
public static void remMeanValue(int[][][] data, int startX, int startY, int stopX, int stopY, int mean){
|
||||
addMeanValue(data, startX, startY, stopX, stopY, -mean);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the mean value to the image data
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is the mean value
|
||||
*/
|
||||
public static void addMeanValue(int[][][] data, int startX, int startY, int stopX, int stopY, int mean){
|
||||
addMeanArray(data, startX, startY, stopX, stopY, new int[]{mean, mean, mean});
|
||||
}
|
||||
|
||||
/**
|
||||
* removes an mean array containing a mean value for each color
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is an array of length 3 containing a mean value for each color RGB
|
||||
*/
|
||||
public static void remMeanArray(int[][][] data, int startX, int startY, int stopX, int stopY, int[] mean){
|
||||
addMeanArray(data, startX, startY, stopX, stopY, new int[]{-mean[0], -mean[1], -mean[2]});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an mean array containing a mean value for each color
|
||||
*
|
||||
* @param data is the image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @param mean is an array of length 3 containing a mean value for each color RGB
|
||||
*/
|
||||
public static void addMeanArray(int[][][] data, int startX, int startY, int stopX, int stopY, int[] mean){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] += mean[0];
|
||||
data[y][x][2] += mean[1];
|
||||
data[y][x][3] += mean[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all the pixel data from the image to the new array
|
||||
*
|
||||
* @param data The data to copy
|
||||
* @param xStart X start position on the source
|
||||
* @param yStart Y start position on the source
|
||||
* @param width The amount of pixels to copy
|
||||
* @param height The amount of pixels to copy
|
||||
* @return A copy of the data array
|
||||
*/
|
||||
public static int[][][] crop(int[][][] data, int xStart, int yStart, int width, int height){
|
||||
return crop(data, xStart, yStart, null, 0, 0, width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all the pixel data from the image to the new array
|
||||
*
|
||||
* @param data The data to copy
|
||||
* @param xData X start position in the source
|
||||
* @param yData Y start position in the source
|
||||
* @param crop The destination
|
||||
* @param xCrop X start position in the destination
|
||||
* @param yCrop Y start position in the destination
|
||||
* @param width The amount of pixels to copy
|
||||
* @param height The amount of pixels to copy
|
||||
* @return A copy of the data array
|
||||
*/
|
||||
public static int[][][] crop(int[][][] data, int xData, int yData, int[][][] crop, int xCrop, int yCrop, int width, int height){
|
||||
if(crop==null) crop = new int[width][height][4];
|
||||
for(int y=0; y<width ;y++){
|
||||
for(int x=0; x<height ;x++){
|
||||
crop[y+yData][x+xData][0] = data[y+yCrop][x+xCrop][0];
|
||||
crop[y+yData][x+xData][1] = data[y+yCrop][x+xCrop][1];
|
||||
crop[y+yData][x+xData][2] = data[y+yCrop][x+xCrop][2];
|
||||
crop[y+yData][x+xData][3] = data[y+yCrop][x+xCrop][3];
|
||||
}
|
||||
}
|
||||
return crop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given array to a new one that it returns
|
||||
*
|
||||
* @param data The data to duplicate
|
||||
* @return an copy of the array
|
||||
*/
|
||||
public static int[][][] copyArray(int[][][] data){
|
||||
return copyArray(data, 0, 0, data[0].length, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given array to a new one that it returns
|
||||
*
|
||||
* @param data The data to duplicate
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return The array copy
|
||||
*/
|
||||
public static int[][][] copyArray(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
int[][][] copy = new int[data.length][data[0].length][4];
|
||||
return copyArray(data, copy, startX, startY, stopX, stopY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given array to a new one that it returns
|
||||
*
|
||||
* @param data The data to duplicate
|
||||
* @param dest is the array to copy the data to
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
* @return the dest array
|
||||
*/
|
||||
public static int[][][] copyArray(int[][][] data, int[][][] dest, int startX, int startY, int stopX, int stopY){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
dest[y][x][0] = data[y][x][0];
|
||||
dest[y][x][1] = data[y][x][1];
|
||||
dest[y][x][2] = data[y][x][2];
|
||||
dest[y][x][3] = data[y][x][3];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method clips the values of the pixel so that they
|
||||
* are in the range 0-255
|
||||
*
|
||||
* @param data The image data
|
||||
* @param startX is the x pixel of the image to start from
|
||||
* @param startY is the y pixel of the image to start from
|
||||
* @param stopX is the x pixel of the image to stop
|
||||
* @param stopY is the y pixel of the image to stop
|
||||
*/
|
||||
public static void clip(int[][][] data, int startX, int startY, int stopX, int stopY){
|
||||
for(int y=startY; y<stopY ;y++){
|
||||
for(int x=startX; x<stopX ;x++){
|
||||
data[y][x][1] = clip(data[y][x][1]);
|
||||
data[y][x][2] = clip(data[y][x][2]);
|
||||
data[y][x][3] = clip(data[y][x][3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method clips the values of a color so that it
|
||||
* is in the range 0-255
|
||||
*/
|
||||
public static int clip(int color){
|
||||
if(color < 0)
|
||||
return 0;
|
||||
else if(color > 255)
|
||||
return 255;
|
||||
else
|
||||
return color;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,49 +33,49 @@ import java.io.OutputStream;
|
|||
*
|
||||
*/
|
||||
public class StringOutputStream extends OutputStream{
|
||||
// The buffer
|
||||
protected StringBuilder buffer;
|
||||
|
||||
/**
|
||||
* Creates an new instance of this class
|
||||
*/
|
||||
public StringOutputStream(){
|
||||
clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
buffer.append( b );
|
||||
}
|
||||
// The buffer
|
||||
protected StringBuilder buffer;
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) {
|
||||
buffer.append( new String(b) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) {
|
||||
buffer.append( new String(b, off, len) );
|
||||
}
|
||||
/**
|
||||
* Creates an new instance of this class
|
||||
*/
|
||||
public StringOutputStream(){
|
||||
clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link OutputStream:clear()}
|
||||
*/
|
||||
@Override
|
||||
public void close() {}
|
||||
|
||||
/**
|
||||
* Clears the String buffer
|
||||
*/
|
||||
public void clear(){
|
||||
buffer = new StringBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the String with the data
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return buffer.toString();
|
||||
}
|
||||
@Override
|
||||
public void write(int b) {
|
||||
buffer.append( b );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) {
|
||||
buffer.append( new String(b) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) {
|
||||
buffer.append( new String(b, off, len) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link OutputStream#close()}
|
||||
*/
|
||||
@Override
|
||||
public void close() {}
|
||||
|
||||
/**
|
||||
* Clears the String buffer
|
||||
*/
|
||||
public void clear(){
|
||||
buffer = new StringBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the String with the data
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -273,7 +273,6 @@ public class FileUtil {
|
|||
*
|
||||
* @param file is the name of the file
|
||||
* @param ext is the new extension, without the dot
|
||||
* @return
|
||||
*/
|
||||
public static String replaceExtension(String file, String ext) {
|
||||
if( file == null )
|
||||
|
|
|
|||
|
|
@ -50,217 +50,217 @@ import java.util.logging.Level;
|
|||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* <XMP>
|
||||
* <pre>
|
||||
* Example web.xml:
|
||||
* <servlet>
|
||||
* <servlet-name>Upload</servlet-name>
|
||||
* <servlet-class>zall.util.AjaxFileUpload</servlet-class>
|
||||
* <init-param>
|
||||
* <param-name>JAVASCRIPT</param-name>
|
||||
* <param-value>{FILE_PATH}</param-value>
|
||||
* </init-param>
|
||||
* <init-param>
|
||||
* <param-name>TEMP_PATH</param-name>
|
||||
* <param-value>SYSTEM|SERVLET|{PATH}</param-value>
|
||||
* </init-param>
|
||||
* </servlet>
|
||||
* <servlet>
|
||||
* <servlet-name>Upload</servlet-name>
|
||||
* <servlet-class>zall.util.AjaxFileUpload</servlet-class>
|
||||
* <init-param>
|
||||
* <param-name>JAVASCRIPT</param-name>
|
||||
* <param-value>{FILE_PATH}</param-value>
|
||||
* </init-param>
|
||||
* <init-param>
|
||||
* <param-name>TEMP_PATH</param-name>
|
||||
* <param-value>SYSTEM|SERVLET|{PATH}</param-value>
|
||||
* </init-param>
|
||||
* </servlet>
|
||||
*
|
||||
*
|
||||
* HTML Header:
|
||||
* <script type='text/javascript' src='{PATH_TO_SERVLET}?js'></script>
|
||||
* <script type='text/javascript' src='{PATH_TO_SERVLET}?js'></script>
|
||||
*
|
||||
*
|
||||
* HTML Body:
|
||||
* <FORM id="AjaxFileUpload">
|
||||
* <input type="file" multiple name="file" />
|
||||
* </FORM>
|
||||
* <UL id="UploadQueue"></UL>
|
||||
* <FORM id="AjaxFileUpload">
|
||||
* <input type="file" multiple name="file" />
|
||||
* </FORM>
|
||||
* <UL id="UploadQueue"></UL>
|
||||
*
|
||||
*
|
||||
* </XMP>
|
||||
* </pre>
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public abstract class AjaxFileUpload extends HttpServlet {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String SESSION_FILEUPLOAD_LISTENER = "FILEUPLOAD_LISTENER";
|
||||
public static final String JAVASCRIPT_FILE = "zutil/jee/upload/AjaxFileUpload.js";
|
||||
public static final String SESSION_FILEUPLOAD_LISTENER = "FILEUPLOAD_LISTENER";
|
||||
public static final String JAVASCRIPT_FILE = "zutil/jee/upload/AjaxFileUpload.js";
|
||||
|
||||
public static File TEMPFILE_PATH = null;
|
||||
public static String JAVASCRIPT = "";
|
||||
public static HashSet<String> ALLOWED_EXTENSIONS = new HashSet<String>();
|
||||
public static File TEMPFILE_PATH = null;
|
||||
public static String JAVASCRIPT = "";
|
||||
public static HashSet<String> ALLOWED_EXTENSIONS = new HashSet<String>();
|
||||
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
try {
|
||||
// Read the javascript file to memory
|
||||
String path = JAVASCRIPT_FILE;
|
||||
if(config.getInitParameter("JAVASCRIPT_FILE") != null)
|
||||
path = config.getInitParameter("JAVASCRIPT_FILE");
|
||||
JAVASCRIPT = FileUtil.getContent( FileUtil.findURL(path) );
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
try {
|
||||
// Read the javascript file to memory
|
||||
String path = JAVASCRIPT_FILE;
|
||||
if(config.getInitParameter("JAVASCRIPT_FILE") != null)
|
||||
path = config.getInitParameter("JAVASCRIPT_FILE");
|
||||
JAVASCRIPT = FileUtil.getContent( FileUtil.findURL(path) );
|
||||
|
||||
// Read temp dir
|
||||
if(config.getInitParameter("TEMP_PATH") != null){
|
||||
if( config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SYSTEM") )
|
||||
TEMPFILE_PATH = new File( System.getProperty("java.io.tmpdir") );
|
||||
else if( config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SERVLET") )
|
||||
TEMPFILE_PATH = (File) config.getServletContext().getAttribute("javax.servlet.context.tempdir");
|
||||
else
|
||||
TEMPFILE_PATH = new File( config.getInitParameter("TEMP_PATH") );
|
||||
}
|
||||
|
||||
// Read allowed file types
|
||||
if(config.getInitParameter("ALLOWED_EXTENSIONS") != null){
|
||||
String[] tmp = config.getInitParameter("ALLOWED_EXTENSIONS").split(",");
|
||||
StringBuilder ext_log = new StringBuilder("Allowed extensions: ");
|
||||
for( String ext : tmp ){
|
||||
ALLOWED_EXTENSIONS.add(ext.trim().toLowerCase());
|
||||
ext_log.append(ext).append(", ");
|
||||
}
|
||||
logger.info( ext_log.toString() );
|
||||
}
|
||||
// Read temp dir
|
||||
if(config.getInitParameter("TEMP_PATH") != null){
|
||||
if( config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SYSTEM") )
|
||||
TEMPFILE_PATH = new File( System.getProperty("java.io.tmpdir") );
|
||||
else if( config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SERVLET") )
|
||||
TEMPFILE_PATH = (File) config.getServletContext().getAttribute("javax.servlet.context.tempdir");
|
||||
else
|
||||
TEMPFILE_PATH = new File( config.getInitParameter("TEMP_PATH") );
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// Read allowed file types
|
||||
if(config.getInitParameter("ALLOWED_EXTENSIONS") != null){
|
||||
String[] tmp = config.getInitParameter("ALLOWED_EXTENSIONS").split(",");
|
||||
StringBuilder ext_log = new StringBuilder("Allowed extensions: ");
|
||||
for( String ext : tmp ){
|
||||
ALLOWED_EXTENSIONS.add(ext.trim().toLowerCase());
|
||||
ext_log.append(ext).append(", ");
|
||||
}
|
||||
logger.info( ext_log.toString() );
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void doGet(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
PrintWriter out = response.getWriter();
|
||||
if(request.getParameter("js") != null){
|
||||
response.setContentType("application/x-javascript");
|
||||
String tmp = JAVASCRIPT;
|
||||
tmp = JAVASCRIPT.replaceAll("\\{SERVLET_URL\\}", request.getRequestURI());
|
||||
tmp = tmp.replaceAll("\\{BGUPLOAD\\}", "false");
|
||||
tmp = tmp.replaceAll("\\{PROGHTML\\}", getProgressHTML());
|
||||
out.print(tmp);
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
response.setContentType("application/json");
|
||||
HttpSession session = request.getSession();
|
||||
LinkedList<FileUploadListener> list =
|
||||
(LinkedList<FileUploadListener>)session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
||||
if (list == null) {
|
||||
out.println("[]");
|
||||
return;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void doGet(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
PrintWriter out = response.getWriter();
|
||||
if(request.getParameter("js") != null){
|
||||
response.setContentType("application/x-javascript");
|
||||
String tmp = JAVASCRIPT;
|
||||
tmp = JAVASCRIPT.replaceAll("\\{SERVLET_URL\\}", request.getRequestURI());
|
||||
tmp = tmp.replaceAll("\\{BGUPLOAD\\}", "false");
|
||||
tmp = tmp.replaceAll("\\{PROGHTML\\}", getProgressHTML());
|
||||
out.print(tmp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate JSON
|
||||
DataNode root = new DataNode( DataType.List );
|
||||
Iterator<FileUploadListener> it = list.iterator();
|
||||
while( it.hasNext() ) {
|
||||
FileUploadListener listener = it.next();
|
||||
if( listener.getStatus() == Status.Done || listener.getStatus() == Status.Error ){
|
||||
if( listener.getTime() + 5000 < System.currentTimeMillis() ){
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
root.add( listener.getJSON() );
|
||||
}
|
||||
response.setContentType("application/json");
|
||||
HttpSession session = request.getSession();
|
||||
LinkedList<FileUploadListener> list =
|
||||
(LinkedList<FileUploadListener>)session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
||||
if (list == null) {
|
||||
out.println("[]");
|
||||
return;
|
||||
}
|
||||
|
||||
// Write to the user
|
||||
JSONWriter json_out = new JSONWriter( out );
|
||||
json_out.write(root);
|
||||
}
|
||||
// Generate JSON
|
||||
DataNode root = new DataNode( DataType.List );
|
||||
Iterator<FileUploadListener> it = list.iterator();
|
||||
while( it.hasNext() ) {
|
||||
FileUploadListener listener = it.next();
|
||||
if( listener.getStatus() == Status.Done || listener.getStatus() == Status.Error ){
|
||||
if( listener.getTime() + 5000 < System.currentTimeMillis() ){
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
root.add( listener.getJSON() );
|
||||
}
|
||||
|
||||
// Write to the user
|
||||
JSONWriter json_out = new JSONWriter( out );
|
||||
json_out.write(root);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void doPost(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
FileUploadListener listener = new FileUploadListener();
|
||||
try {
|
||||
// Initiate list and HashMap that will contain the data
|
||||
HashMap<String,String> fields = new HashMap<String,String>();
|
||||
ArrayList<FileItem> files = new ArrayList<FileItem>();
|
||||
|
||||
// Add the listener to the session
|
||||
HttpSession session = request.getSession();
|
||||
LinkedList<FileUploadListener> list =
|
||||
(LinkedList<FileUploadListener>)session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
||||
if(list == null){
|
||||
list = new LinkedList<FileUploadListener>();
|
||||
session.setAttribute(SESSION_FILEUPLOAD_LISTENER, list);
|
||||
}
|
||||
list.add(listener);
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void doPost(HttpServletRequest request,
|
||||
HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
// Create a factory for disk-based file items
|
||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||
if(TEMPFILE_PATH != null)
|
||||
factory.setRepository( TEMPFILE_PATH );
|
||||
// Create a new file upload handler
|
||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||
upload.setProgressListener( listener );
|
||||
// Set overall request size constraint
|
||||
//upload.setSizeMax(yourMaxRequestSize);
|
||||
FileUploadListener listener = new FileUploadListener();
|
||||
try {
|
||||
// Initiate list and HashMap that will contain the data
|
||||
HashMap<String,String> fields = new HashMap<String,String>();
|
||||
ArrayList<FileItem> files = new ArrayList<FileItem>();
|
||||
|
||||
// Parse the request
|
||||
FileItemIterator it = upload.getItemIterator( request );
|
||||
while( it.hasNext() ) {
|
||||
FileItemStream item = it.next();
|
||||
// Is the file type allowed?
|
||||
if( !item.isFormField() && !ALLOWED_EXTENSIONS.contains( FileUtil.getFileExtension(item.getName()).toLowerCase() )){
|
||||
String msg = "Filetype '"+FileUtil.getFileExtension(item.getName())+"' is not allowed!";
|
||||
logger.warning( msg );
|
||||
listener.setStatus(Status.Error);
|
||||
listener.setFileName( item.getName() );
|
||||
listener.setMessage( msg );
|
||||
return;
|
||||
}
|
||||
listener.setFileName( item.getName() );
|
||||
FileItem fileItem = factory.createItem(item.getFieldName(),
|
||||
item.getContentType(), item.isFormField(), item.getName());
|
||||
// Read the file data
|
||||
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
|
||||
if (fileItem instanceof FileItemHeadersSupport) {
|
||||
final FileItemHeaders fih = item.getHeaders();
|
||||
((FileItemHeadersSupport) fileItem).setHeaders(fih);
|
||||
}
|
||||
|
||||
//Handle the item
|
||||
if(fileItem.isFormField()){
|
||||
fields.put( fileItem.getFieldName(), fileItem.getString());
|
||||
}
|
||||
else{
|
||||
files.add( fileItem );
|
||||
logger.info("Recieved file: "+fileItem.getName()+" ("+StringUtil.formatByteSizeToString(fileItem.getSize())+")");
|
||||
}
|
||||
}
|
||||
// Process the upload
|
||||
listener.setStatus( Status.Processing );
|
||||
doUpload( request, response, fields, files );
|
||||
// Done
|
||||
listener.setStatus( Status.Done );
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
listener.setStatus(Status.Error);
|
||||
listener.setFileName("");
|
||||
listener.setMessage( e.getMessage() );
|
||||
}
|
||||
}
|
||||
// Add the listener to the session
|
||||
HttpSession session = request.getSession();
|
||||
LinkedList<FileUploadListener> list =
|
||||
(LinkedList<FileUploadListener>)session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
||||
if(list == null){
|
||||
list = new LinkedList<FileUploadListener>();
|
||||
session.setAttribute(SESSION_FILEUPLOAD_LISTENER, list);
|
||||
}
|
||||
list.add(listener);
|
||||
|
||||
/**
|
||||
* @return the HTML for the progress bar. Special ID's:
|
||||
* <br>-filename = String
|
||||
* <br>-progress = percent
|
||||
* <br>-total = String
|
||||
* <br>-uploaded = String
|
||||
* <br>-status = String (Uploading, Initializing etc)
|
||||
* <br>-speed = String
|
||||
*/
|
||||
public abstract String getProgressHTML();
|
||||
// Create a factory for disk-based file items
|
||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||
if(TEMPFILE_PATH != null)
|
||||
factory.setRepository( TEMPFILE_PATH );
|
||||
// Create a new file upload handler
|
||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||
upload.setProgressListener( listener );
|
||||
// Set overall request size constraint
|
||||
//upload.setSizeMax(yourMaxRequestSize);
|
||||
|
||||
/**
|
||||
* Handle the uppload
|
||||
* @throws ServletException
|
||||
*/
|
||||
public abstract void doUpload(HttpServletRequest request, HttpServletResponse response,
|
||||
Map<String,String> fields, List<FileItem> files) throws ServletException;
|
||||
// Parse the request
|
||||
FileItemIterator it = upload.getItemIterator( request );
|
||||
while( it.hasNext() ) {
|
||||
FileItemStream item = it.next();
|
||||
// Is the file type allowed?
|
||||
if( !item.isFormField() && !ALLOWED_EXTENSIONS.contains( FileUtil.getFileExtension(item.getName()).toLowerCase() )){
|
||||
String msg = "Filetype '"+FileUtil.getFileExtension(item.getName())+"' is not allowed!";
|
||||
logger.warning( msg );
|
||||
listener.setStatus(Status.Error);
|
||||
listener.setFileName( item.getName() );
|
||||
listener.setMessage( msg );
|
||||
return;
|
||||
}
|
||||
listener.setFileName( item.getName() );
|
||||
FileItem fileItem = factory.createItem(item.getFieldName(),
|
||||
item.getContentType(), item.isFormField(), item.getName());
|
||||
// Read the file data
|
||||
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
|
||||
if (fileItem instanceof FileItemHeadersSupport) {
|
||||
final FileItemHeaders fih = item.getHeaders();
|
||||
((FileItemHeadersSupport) fileItem).setHeaders(fih);
|
||||
}
|
||||
|
||||
//Handle the item
|
||||
if(fileItem.isFormField()){
|
||||
fields.put( fileItem.getFieldName(), fileItem.getString());
|
||||
}
|
||||
else{
|
||||
files.add( fileItem );
|
||||
logger.info("Recieved file: "+fileItem.getName()+" ("+StringUtil.formatByteSizeToString(fileItem.getSize())+")");
|
||||
}
|
||||
}
|
||||
// Process the upload
|
||||
listener.setStatus( Status.Processing );
|
||||
doUpload( request, response, fields, files );
|
||||
// Done
|
||||
listener.setStatus( Status.Done );
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
listener.setStatus(Status.Error);
|
||||
listener.setFileName("");
|
||||
listener.setMessage( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the HTML for the progress bar. Special ID's:
|
||||
* <br>-filename = String
|
||||
* <br>-progress = percent
|
||||
* <br>-total = String
|
||||
* <br>-uploaded = String
|
||||
* <br>-status = String (Uploading, Initializing etc)
|
||||
* <br>-speed = String
|
||||
*/
|
||||
public abstract String getProgressHTML();
|
||||
|
||||
/**
|
||||
* Handle the uppload
|
||||
* @throws ServletException
|
||||
*/
|
||||
public abstract void doUpload(HttpServletRequest request, HttpServletResponse response,
|
||||
Map<String,String> fields, List<FileItem> files) throws ServletException;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,45 +27,43 @@ package zutil.net.nio.worker.chat;
|
|||
import zutil.net.nio.message.Message;
|
||||
|
||||
public class ChatMessage implements Message {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static enum ChatMessageType {REGISTER, UNREGISTER, MESSAGE};
|
||||
|
||||
public ChatMessageType type;
|
||||
public String msg;
|
||||
public String room;
|
||||
|
||||
/**
|
||||
* Registers the user to the main chat
|
||||
*
|
||||
* @param name Name of user
|
||||
*/
|
||||
public ChatMessage(){
|
||||
this("", "", ChatMessageType.REGISTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the user to the given room
|
||||
*
|
||||
* @param room The room to register to
|
||||
*/
|
||||
public ChatMessage(String room){
|
||||
this("", room, ChatMessageType.REGISTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the given room
|
||||
*
|
||||
* @param msg The message
|
||||
* @param room The room
|
||||
*/
|
||||
public ChatMessage(String msg, String room){
|
||||
this(msg, room, ChatMessageType.MESSAGE);
|
||||
}
|
||||
|
||||
public ChatMessage(String msg, String room, ChatMessageType type){
|
||||
this.msg = msg;
|
||||
this.room = room;
|
||||
this.type = type;
|
||||
}
|
||||
public static enum ChatMessageType {REGISTER, UNREGISTER, MESSAGE};
|
||||
|
||||
public ChatMessageType type;
|
||||
public String msg;
|
||||
public String room;
|
||||
|
||||
/**
|
||||
* Registers the user to the main chat
|
||||
*/
|
||||
public ChatMessage(){
|
||||
this("", "", ChatMessageType.REGISTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the user to the given room
|
||||
*
|
||||
* @param room The room to register to
|
||||
*/
|
||||
public ChatMessage(String room){
|
||||
this("", room, ChatMessageType.REGISTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the given room
|
||||
*
|
||||
* @param msg The message
|
||||
* @param room The room
|
||||
*/
|
||||
public ChatMessage(String msg, String room){
|
||||
this(msg, room, ChatMessageType.MESSAGE);
|
||||
}
|
||||
|
||||
public ChatMessage(String msg, String room, ChatMessageType type){
|
||||
this.msg = msg;
|
||||
this.room = room;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,22 +38,22 @@ import java.lang.annotation.Target;
|
|||
* private static class Test implements WSInterface{
|
||||
* public Test(){}
|
||||
*
|
||||
* @WSDocumentation("blabla")
|
||||
* @WSDLParamDocumentation("olle = a variable?")
|
||||
* @WSDocumentation("blabla")
|
||||
* @WSDLParamDocumentation("olle = a variable?")
|
||||
* public void pubZ(
|
||||
* @WSParamName("olle") int lol)
|
||||
* @WSParamName("olle") int lol)
|
||||
* throws Exception{
|
||||
* ....
|
||||
* }
|
||||
*
|
||||
* @WSReturnName("param")
|
||||
* @WSReturnName("param")
|
||||
* public String pubA(
|
||||
* @WSParamName(value="lol", optional=true) String lol)
|
||||
* @WSParamName(value="lol", optional=true) String lol)
|
||||
* throws Exception{
|
||||
* ....
|
||||
* }
|
||||
*
|
||||
* @WSIgnore()
|
||||
* @WSIgnore()
|
||||
* public void privaZ(....){
|
||||
* ...
|
||||
* }
|
||||
|
|
@ -63,79 +63,78 @@ import java.lang.annotation.Target;
|
|||
* @author Ziver
|
||||
*/
|
||||
public interface WSInterface {
|
||||
/**
|
||||
* Annotation that assigns a name to an parameters
|
||||
* in an method.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface WSParamName {
|
||||
String value();
|
||||
boolean optional() default false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Annotation that assigns a name to the return value
|
||||
* in an method.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface WSReturnName {
|
||||
String value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Skipp publication of the given method
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface WSIgnore { }
|
||||
|
||||
/**
|
||||
* Method or Parameter comments for the WSDL.
|
||||
* These comments are put in the message part of the WSDL
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface WSDocumentation{
|
||||
String value();
|
||||
}
|
||||
/**
|
||||
* Annotation that assigns a name to an parameters
|
||||
* in an method.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
@interface WSParamName {
|
||||
String value();
|
||||
boolean optional() default false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter comments for the WSDL.
|
||||
* These comments are put in the message part of the WSDL
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface WSParamDocumentation{
|
||||
String value();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will be used in the header.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface WSHeader { }
|
||||
|
||||
/**
|
||||
* Specifies the name space for method.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
//@Target(ElementType.TYPE)
|
||||
public @interface WSNamespace {
|
||||
String value();
|
||||
}
|
||||
/**
|
||||
* Annotation that assigns a name to the return value
|
||||
* in an method.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface WSReturnName {
|
||||
String value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Skipp publication of the given method
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface WSIgnore { }
|
||||
|
||||
/**
|
||||
* Method or Parameter comments for the WSDL.
|
||||
* These comments are put in the message part of the WSDL
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface WSDocumentation{
|
||||
String value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter comments for the WSDL.
|
||||
* These comments are put in the message part of the WSDL
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface WSParamDocumentation{
|
||||
String value();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will be used in the header.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface WSHeader { }
|
||||
|
||||
/**
|
||||
* Specifies the name space for method.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface WSNamespace {
|
||||
String value();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,9 +38,9 @@ import java.lang.reflect.Field;
|
|||
*
|
||||
* <pre>
|
||||
* private static class TestObject implements WSReturnObject{
|
||||
* @WSValueName("name")
|
||||
* @WSValueName("name")
|
||||
* public String name;
|
||||
* @WSValueName("lastname")
|
||||
* @WSValueName("lastname")
|
||||
* public String lastname;
|
||||
*
|
||||
* public TestObject(String n, String l){
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ public class BEncodedParser {
|
|||
* Returns the representation of the data in the BEncoded string
|
||||
*
|
||||
* @param data is the data to be decoded
|
||||
* @return
|
||||
*/
|
||||
public static DataNode read(String data) throws ParseException {
|
||||
return decode_BEncoded(new MutableInt(), new StringBuilder(data));
|
||||
|
|
|
|||
|
|
@ -38,163 +38,164 @@ import java.util.Set;
|
|||
*
|
||||
*/
|
||||
public class BloomFilter<T extends Serializable> implements Set<T>, Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private BitSet bits;
|
||||
private int content_size;
|
||||
private int optimal_size;
|
||||
private int k;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a bloom filter
|
||||
*
|
||||
* @param size The amount of bits in the filter
|
||||
* @param expected_data_count The estimated amount of data to
|
||||
* be inserted(a bigger number is better than a smaller)
|
||||
*/
|
||||
public BloomFilter(int size, int expected_data_count){
|
||||
bits = new BitSet(size);
|
||||
k = (int)((size/expected_data_count) * Math.log(2));
|
||||
content_size = 0;
|
||||
optimal_size = expected_data_count;
|
||||
}
|
||||
private BitSet bits;
|
||||
private int content_size;
|
||||
private int optimal_size;
|
||||
private int k;
|
||||
|
||||
/**
|
||||
* @param e A Serializable object
|
||||
* @return If the optimal size has been reached
|
||||
*/
|
||||
public boolean add(T e) {
|
||||
try {
|
||||
content_size++;
|
||||
int hash = 0;
|
||||
for(int i=0; i<k ;i++){
|
||||
hash = Hasher.MurmurHash(e, hash);
|
||||
hash = Math.abs(hash) % bits.size();
|
||||
bits.set(hash, true);
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return isFull();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a collection to the bloom filter
|
||||
*
|
||||
* @return If the optimal size has been reached
|
||||
*/
|
||||
public boolean addAll(Collection<? extends T> c) {
|
||||
for(T t : c){
|
||||
add(t);
|
||||
}
|
||||
return isFull();
|
||||
}
|
||||
/**
|
||||
* Creates a bloom filter
|
||||
*
|
||||
* @param size The amount of bits in the filter
|
||||
* @param expected_data_count The estimated amount of data to
|
||||
* be inserted(a bigger number is better than a smaller)
|
||||
*/
|
||||
public BloomFilter(int size, int expected_data_count){
|
||||
bits = new BitSet(size);
|
||||
k = (int)((size/expected_data_count) * Math.log(2));
|
||||
content_size = 0;
|
||||
optimal_size = expected_data_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return clears the filter
|
||||
*/
|
||||
public void clear() {
|
||||
content_size = 0;
|
||||
bits.clear();
|
||||
}
|
||||
/**
|
||||
* @param e A Serializable object
|
||||
* @return If the optimal size has been reached
|
||||
*/
|
||||
public boolean add(T e) {
|
||||
try {
|
||||
content_size++;
|
||||
int hash = 0;
|
||||
for(int i=0; i<k ;i++){
|
||||
hash = Hasher.MurmurHash(e, hash);
|
||||
hash = Math.abs(hash) % bits.size();
|
||||
bits.set(hash, true);
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return isFull();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param o is the Serializable object to search for
|
||||
* @return If the object contains in the filter or
|
||||
* false if the Object is not Serializable
|
||||
*/
|
||||
public boolean contains(Object o) {
|
||||
try {
|
||||
if(!(o instanceof Serializable))return false;
|
||||
int hash = 0;
|
||||
for(int i=0; i<k ;i++){
|
||||
hash = Hasher.MurmurHash((Serializable)o, hash);
|
||||
hash = Math.abs(hash) % bits.size();
|
||||
if(!bits.get(hash))
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Adds a collection to the bloom filter
|
||||
*
|
||||
* @return If the optimal size has been reached
|
||||
*/
|
||||
public boolean addAll(Collection<? extends T> c) {
|
||||
for(T t : c){
|
||||
add(t);
|
||||
}
|
||||
return isFull();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the whole collection contains in the filter
|
||||
*
|
||||
* @param c The collection
|
||||
*/
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
for(Object o : c){
|
||||
if(!contains(o)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Clears the filter
|
||||
*/
|
||||
public void clear() {
|
||||
content_size = 0;
|
||||
bits.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If the bloom filter is empty
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return content_size == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If the optimal size has been reached
|
||||
*/
|
||||
public boolean isFull() {
|
||||
return content_size > optimal_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The number of data added
|
||||
*/
|
||||
public int size() {
|
||||
return content_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The false positive probability of the current state of the filter
|
||||
*/
|
||||
public double falsePosetiveProbability(){
|
||||
return Math.pow(0.6185, bits.size()/content_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hash count. Should be set before adding elements
|
||||
* or the already added elements will be lost
|
||||
*
|
||||
* @param k The hash count
|
||||
*/
|
||||
public void setHashCount(int k){
|
||||
this.k = k;
|
||||
}
|
||||
|
||||
//*********************************************************************
|
||||
//*********************************************************************
|
||||
public Object[] toArray() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
/**
|
||||
* @param o is the Serializable object to search for
|
||||
* @return If the object contains in the filter or
|
||||
* false if the Object is not Serializable
|
||||
*/
|
||||
public boolean contains(Object o) {
|
||||
try {
|
||||
if(!(o instanceof Serializable))return false;
|
||||
int hash = 0;
|
||||
for(int i=0; i<k ;i++){
|
||||
hash = Hasher.MurmurHash((Serializable)o, hash);
|
||||
hash = Math.abs(hash) % bits.size();
|
||||
if(!bits.get(hash))
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@SuppressWarnings("hiding")
|
||||
public <T> T[] toArray(T[] a) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Iterator<T> iterator() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
/**
|
||||
* Checks if the whole collection contains in the filter
|
||||
*
|
||||
* @param c The collection
|
||||
*/
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
for(Object o : c){
|
||||
if(!contains(o)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
/**
|
||||
* @return If the bloom filter is empty
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return content_size == 0;
|
||||
}
|
||||
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
/**
|
||||
* @return If the optimal size has been reached
|
||||
*/
|
||||
public boolean isFull() {
|
||||
return content_size > optimal_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The number of data added
|
||||
*/
|
||||
public int size() {
|
||||
return content_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The false positive probability of the current state of the filter
|
||||
*/
|
||||
public double falsePositiveProbability(){
|
||||
return Math.pow(0.6185, bits.size()/content_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hash count. Should be set before adding elements
|
||||
* or the already added elements will be lost
|
||||
*
|
||||
* @param k The hash count
|
||||
*/
|
||||
public void setHashCount(int k){
|
||||
this.k = k;
|
||||
}
|
||||
|
||||
|
||||
//*********************************************************************
|
||||
//*********************************************************************
|
||||
public Object[] toArray() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@SuppressWarnings("hiding")
|
||||
public <T> T[] toArray(T[] a) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Iterator<T> iterator() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,87 +36,87 @@ import java.awt.event.FocusListener;
|
|||
import java.util.HashMap;
|
||||
|
||||
public class WizardActionHandler implements ActionListener, FocusListener, ListSelectionListener{
|
||||
private HashMap<String, Object> values;
|
||||
private HashMap<String, Object> values;
|
||||
|
||||
public WizardActionHandler(HashMap<String, Object> values){
|
||||
this.values = values;
|
||||
}
|
||||
public WizardActionHandler(HashMap<String, Object> values){
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
event(e);
|
||||
}
|
||||
public void focusGained(FocusEvent e) {
|
||||
event(e);
|
||||
}
|
||||
public void focusLost(FocusEvent e) {
|
||||
event(e);
|
||||
}
|
||||
public void event(AWTEvent e){
|
||||
if(e.getSource() instanceof Component)
|
||||
registerValue( (Component)e.getSource() );
|
||||
}
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
if(e.getSource() instanceof Component)
|
||||
registerValue( (Component)e.getSource() );
|
||||
}
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
event(e);
|
||||
}
|
||||
public void focusGained(FocusEvent e) {
|
||||
event(e);
|
||||
}
|
||||
public void focusLost(FocusEvent e) {
|
||||
event(e);
|
||||
}
|
||||
public void event(AWTEvent e){
|
||||
if(e.getSource() instanceof Component)
|
||||
registerValue( (Component)e.getSource() );
|
||||
}
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
if(e.getSource() instanceof Component)
|
||||
registerValue( (Component)e.getSource() );
|
||||
}
|
||||
|
||||
public void registerListener(Component c){
|
||||
/**
|
||||
* JToggleButton
|
||||
* JCheckBox
|
||||
* JRadioButton
|
||||
*/
|
||||
if(c instanceof JToggleButton){
|
||||
JToggleButton o = (JToggleButton) c;
|
||||
o.addActionListener( this );
|
||||
}
|
||||
/**
|
||||
* JEditorPane
|
||||
* JTextArea
|
||||
* JTextField
|
||||
*/
|
||||
else if(c instanceof JTextComponent){
|
||||
JTextComponent o = (JTextComponent) c;
|
||||
o.addFocusListener( this );
|
||||
}
|
||||
/**
|
||||
* JList
|
||||
*/
|
||||
else if(c instanceof JList){
|
||||
JList<?> o = (JList<?>) c;
|
||||
o.addListSelectionListener( this );
|
||||
}
|
||||
}
|
||||
public void registerListener(Component c){
|
||||
/**
|
||||
* JToggleButton
|
||||
* JCheckBox
|
||||
* JRadioButton
|
||||
*/
|
||||
if(c instanceof JToggleButton){
|
||||
JToggleButton o = (JToggleButton) c;
|
||||
o.addActionListener( this );
|
||||
}
|
||||
/**
|
||||
* JEditorPane
|
||||
* JTextArea
|
||||
* JTextField
|
||||
*/
|
||||
else if(c instanceof JTextComponent){
|
||||
JTextComponent o = (JTextComponent) c;
|
||||
o.addFocusListener( this );
|
||||
}
|
||||
/**
|
||||
* JList
|
||||
*/
|
||||
else if(c instanceof JList){
|
||||
JList<?> o = (JList<?>) c;
|
||||
o.addListSelectionListener( this );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the state of the event source
|
||||
* @param e is the event
|
||||
*/
|
||||
public void registerValue(Component c) {
|
||||
/**
|
||||
* JToggleButton
|
||||
* JCheckBox
|
||||
* JRadioButton
|
||||
*/
|
||||
if(c instanceof JToggleButton){
|
||||
JToggleButton o = (JToggleButton) c;
|
||||
values.put( o.getName() , o.isSelected() );
|
||||
}
|
||||
/**
|
||||
* JEditorPane
|
||||
* JTextArea
|
||||
* JTextField
|
||||
*/
|
||||
else if(c instanceof JTextComponent){
|
||||
JTextComponent o = (JTextComponent) c;
|
||||
values.put( o.getName() , o.getText() );
|
||||
}
|
||||
/**
|
||||
* JList
|
||||
*/
|
||||
else if(c instanceof JList){
|
||||
JList<?> o = (JList<?>) c;
|
||||
values.put( o.getName() , o.getSelectedValue() );
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Registers the state of the event source
|
||||
* @param c is the event
|
||||
*/
|
||||
public void registerValue(Component c) {
|
||||
/**
|
||||
* JToggleButton
|
||||
* JCheckBox
|
||||
* JRadioButton
|
||||
*/
|
||||
if(c instanceof JToggleButton){
|
||||
JToggleButton o = (JToggleButton) c;
|
||||
values.put( o.getName() , o.isSelected() );
|
||||
}
|
||||
/**
|
||||
* JEditorPane
|
||||
* JTextArea
|
||||
* JTextField
|
||||
*/
|
||||
else if(c instanceof JTextComponent){
|
||||
JTextComponent o = (JTextComponent) c;
|
||||
values.put( o.getName() , o.getText() );
|
||||
}
|
||||
/**
|
||||
* JList
|
||||
*/
|
||||
else if(c instanceof JList){
|
||||
JList<?> o = (JList<?>) c;
|
||||
values.put( o.getName() , o.getSelectedValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ public class BloomFilterTest extends TestCase {
|
|||
falsePositives++;
|
||||
}
|
||||
}
|
||||
double expectedFP = bf.falsePosetiveProbability();
|
||||
double expectedFP = bf.falsePositiveProbability();
|
||||
double actualFP = (double) falsePositives / (double) addCount;
|
||||
System.out.println("Got " + falsePositives
|
||||
+ " false positives out of " + addCount + " added items, rate = "
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue