Refactored libraries.

This commit is contained in:
Christian P. MOMON 2017-05-01 23:00:58 +02:00
parent 182528b8f5
commit f2b861dd1d
30 changed files with 11 additions and 3574 deletions

View file

@ -3,17 +3,22 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/junit-4.11.jar" sourcepath="lib/junit-4.11-sources.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
<classpathentry kind="lib" path="lib/slf4j-api-1.7.5.jar" sourcepath="lib/slf4j-api-1.7.5-sources.jar"/>
<classpathentry kind="lib" path="lib/slf4j-log4j12-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.1.jar" sourcepath="lib/commons-lang3-3.1-sources.jar"/>
<classpathentry kind="lib" path="lib/joda-time-2.3.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/Logs/log4j-1.2.17-source.zip"/>
<classpathentry kind="lib" path="lib/Logs/log4j-1.2.17.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-api-1.7.5-sources.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-api-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-log4j12-1.7.5-sources.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-log4j12-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/UnitTesting/hamcrest-core-1.3-sources.jar"/>
<classpathentry kind="lib" path="lib/UnitTesting/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/UnitTesting/junit-4.11-sources.jar"/>
<classpathentry kind="lib" path="lib/UnitTesting/junit-4.11.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.1-sources.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

Binary file not shown.

Binary file not shown.

View file

@ -1,256 +0,0 @@
/**
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
/**
* This class defines a content file.
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class DataFile
{
public static int NOID = 0;
public static int DEFAULT_SIZE = 0;
//
protected int id;
protected int contentId;
protected String name;
protected long size;
protected byte[] data;
protected String creationDate;
protected String creationUser;
/**
*
*/
public DataFile()
{
this.id = NOID;
this.contentId = NOID;
this.name = null;
this.size = this.DEFAULT_SIZE;
this.data = null;
this.creationDate = null;
this.creationUser = null;
}
/**
*
*/
public DataFile(final int contentId, final String name, final long size, final byte[] data)
{
this.id = NOID;
this.contentId = contentId;
this.name = name;
this.size = size;
this.data = data;
this.creationDate = null;
this.creationUser = null;
}
/**
*
*/
public DataFile(final String name, final long size, final byte[] data)
{
this.id = NOID;
this.contentId = NOID;
this.name = name;
this.size = size;
this.data = data;
this.creationDate = null;
this.creationUser = null;
}
/**
*
*/
public int contentId()
{
int result;
result = this.contentId;
//
return (result);
}
/**
*
*/
public String creationDate()
{
String result;
result = this.creationDate;
//
return (result);
}
/**
*
*/
public String creationUser()
{
String result;
result = this.creationUser;
//
return (result);
}
/**
*
*/
public byte[] data()
{
byte[] result;
result = this.data;
//
return (result);
}
/**
*
*/
public int id()
{
int result;
result = this.id;
//
return (result);
}
/**
*
*/
public String name()
{
String result;
result = this.name;
//
return (result);
}
/**
*
*/
public void setContentId(final int contentId)
{
this.contentId = contentId;
}
/**
*
*/
public void setCreationDate(final String creationDate)
{
if (creationDate == null)
{
this.creationDate = "";
}
else
{
this.creationDate = creationDate;
}
}
/**
*
*/
public void setCreationUser(final String creationUser)
{
if (creationUser == null)
{
this.creationUser = "";
}
else
{
this.creationUser = creationUser;
}
}
/**
*
*/
public void setData(final byte[] data)
{
this.data = data;
}
/**
*
*/
public void setId(final int id)
{
this.id = id;
}
/**
*
*/
public void setName(final String name)
{
if (name == null)
{
this.name = "";
}
else
{
this.name = name;
}
}
/**
*
*/
public void setSize(final long size)
{
if (size >= 0)
{
this.size = size;
}
else
{
this.size = 0;
}
}
/**
*
*/
public long size()
{
long result;
result = this.size;
//
return (result);
}
}

View file

@ -1,181 +0,0 @@
/**
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.util.ArrayList;
/**
* This class is a collection of DataFile objects whit some specific methods.
*
* @author Christian Pierre MOMON
*/
public class DataFiles extends ArrayList<DataFile>
{
private static final long serialVersionUID = -4584622422555785456L;
/**
*
*
*/
public DataFiles()
{
super();
}
/**
*
* @param source
*/
public DataFiles(final DataFiles source)
{
super(source);
}
/**
*
*/
public DataFiles getByContentId(final int id)
{
DataFiles result = new DataFiles();
for (int nDataFile = 0; nDataFile < this.size(); nDataFile++)
{
DataFile contentFile = this.getByIndex(nDataFile);
if (contentFile.contentId() == id)
{
result.add(contentFile);
}
}
//
return (result);
}
/**
*
*/
public DataFile getById(final int id)
{
DataFile result = null;
boolean ended = false;
int nDataFile = 0;
while (!ended)
{
if (nDataFile >= this.size())
{
ended = true;
result = null;
}
else
{
DataFile contentFile = this.getByIndex(nDataFile);
if (id == contentFile.id())
{
ended = true;
result = contentFile;
}
else
{
nDataFile += 1;
}
}
}
//
return (result);
}
/**
*
*/
public DataFile getByIndex(final int index)
{
DataFile result;
result = super.get(index);
//
return (result);
}
/**
*
*/
public DataFile getByName(final String name)
{
DataFile result = null;
if ((name == null) || (name.equals("")))
{
result = null;
}
else
{
boolean ended = false;
int dataFileIndex = 0;
while (!ended)
{
if (dataFileIndex >= this.size())
{
ended = true;
result = null;
}
else
{
DataFile contentFile = this.getByIndex(dataFileIndex);
if (name.equals(contentFile.name()))
{
ended = true;
result = contentFile;
}
else
{
dataFileIndex += 1;
}
}
}
}
//
return (result);
}
/**
*
*/
@Override
public String toString()
{
StringBuffer result = new StringBuffer();
for (int nDataFile = 0; nDataFile < this.size(); nDataFile++)
{
DataFile contentFile = this.getByIndex(nDataFile);
result.append("== " + contentFile.name() + "\n");
result.append("contentFile " + nDataFile + " - " + contentFile.name() + "\n");
}
//
return (result.toString());
}
}

View file

@ -1,370 +0,0 @@
/**
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class groups function to help in Calendar manipulation.
*
* SimpleDateFormat is not used cause does not thread safe?
*/
public class DateHelper
{
// static private final Logger logger =
// LoggerFactory.getLogger(DateHelper.class);
private static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d";
private static final String SHORT_EUROPEAN_DATE_FORMAT = "%02d/%02d";
private static final String RAW_DATE_FORMAT = "%04d%02d%02d";
private static final String ISO_DATE_FORMAT = "%04d-%02d-%02d";
private static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d";
private static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$";
private static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$";
private static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$";
private static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$";
/**
*
*/
public static String americanFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.DAY_OF_MONTH));
}
//
return (result);
}
/**
*
*/
public static String europeanFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR));
}
//
return (result);
}
/**
*
*/
public static boolean isAmericanFormat(final String date)
{
boolean result;
if (date == null)
{
result = false;
}
else
{
result = date.matches(AMERICAN_DATE_PATTERN);
}
//
return (result);
}
/**
*
*/
public static boolean isEuropeanFormat(final String date)
{
boolean result;
if (date == null)
{
result = false;
}
else
{
result = date.matches(EUROPEAN_DATE_PATTERN);
}
//
return (result);
}
/**
*
*/
public static boolean isISOFormat(final String date)
{
boolean result;
if (date == null)
{
result = false;
}
else
{
result = date.matches(ISO_DATE_PATTERN);
}
//
return (result);
}
/**
*
*/
public static String ISOFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH));
}
//
return (result);
}
/**
*
*/
public static boolean isRawFormat(final String date)
{
boolean result;
if (date == null)
{
result = false;
}
else
{
result = date.matches(RAW_DATE_PATTERN);
}
//
return (result);
}
/**
*
*/
public static boolean isValidDate(final String date)
{
boolean result;
if ((isEuropeanFormat(date)) || (isRawFormat(date)) || (isISOFormat(date)) || (isAmericanFormat(date)))
{
result = true;
}
else
{
result = false;
}
//
return (result);
}
/**
*
*/
public static Calendar parseAmericanDate(final String date)
{
Calendar result;
Pattern pattern = Pattern.compile(AMERICAN_DATE_PATTERN);
Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3))
{
result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(1)) - 1, Integer.parseInt(matcher.group(2)));
}
else
{
result = null;
}
//
return (result);
}
/**
* Note: European parsing test made before the American parsing one.
*/
public static Calendar parseDate(final String date)
{
Calendar result;
if (isEuropeanFormat(date))
{
result = parseEuropeanDate(date);
}
else if (isRawFormat(date))
{
result = parseRawDate(date);
}
else if (isISOFormat(date))
{
result = parseISODate(date);
}
else if (isAmericanFormat(date))
{
result = parseAmericanDate(date);
}
else
{
result = null;
}
//
return (result);
}
/**
*
*/
public static Calendar parseEuropeanDate(final String date)
{
Calendar result;
Pattern pattern = Pattern.compile(EUROPEAN_DATE_PATTERN);
Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3))
{
result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(1)));
}
else
{
result = null;
}
//
return (result);
}
/**
*
*/
public static Calendar parseISODate(final String date)
{
Calendar result;
Pattern pattern = Pattern.compile(ISO_DATE_PATTERN);
Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3))
{
result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)));
}
else
{
result = null;
}
//
return (result);
}
/**
*
*/
public static Calendar parseRawDate(final String date)
{
Calendar result;
Pattern pattern = Pattern.compile(RAW_DATE_PATTERN);
Matcher matcher = pattern.matcher(date);
if ((matcher.find()) && (matcher.groupCount() == 3))
{
result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)));
}
else
{
result = null;
}
//
return (result);
}
/**
*
*/
public static String rawFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1);
}
//
return (result);
}
/**
*
*/
public static String shortEuropeanFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(SHORT_EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1);
}
//
return (result);
}
}

View file

@ -1,124 +0,0 @@
/**
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.util.Calendar;
/**
* This class groups function to help in Calendar manipulation.
*/
public class DateTimeHelper
{
// static private final Logger logger =
// LoggerFactory.getLogger(DateTimeHelper.class);
private static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d";
private static final String RAW_DATE_FORMAT = "%04d%02d%02d %02d:%02d:%02d";
private static final String ISO_DATE_FORMAT = "%04d-%02d-%02d %02d:%02d:%02d";
private static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d";
private static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$";
private static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$";
private static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$";
private static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$";
/**
*
*/
public static String americanFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY),
time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
}
//
return (result);
}
/**
*
*/
public static String europeanFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY),
time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
}
//
return (result);
}
/**
*
*/
public static String ISOFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE),
time.get(Calendar.SECOND));
}
//
return (result);
}
/**
*
*/
public static String rawFormat(final Calendar time)
{
String result;
if (time == null)
{
result = "";
}
else
{
result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE),
time.get(Calendar.SECOND));
}
//
return (result);
}
}

View file

@ -1,128 +0,0 @@
/**
* Copyright (C) 2006, 2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
/**
* This class is a helper to use MessageDigester class.
*
* @deprecated because of DigestUtils from apache-commons-codec is doing the
* same but better.
*/
@Deprecated
public class Digester
{
/**
* "SHA-1", "MD5", "SHA-256", and "SHA-512"
*/
public static String computeHash(final String digestMethod, final File file) throws Exception
{
String result;
if ((file == null) || (!file.exists()))
{
result = null;
}
else
{
// byte[] hash = null;
InputStream source = null;
try
{
MessageDigest digester = MessageDigest.getInstance(digestMethod);
source = new FileInputStream(file);
boolean ended = false;
int bytesNumber;
byte[] buffer = new byte[100 * 1024];
while (!ended)
{
bytesNumber = source.read(buffer);
if (bytesNumber == -1)
{
ended = true;
}
else
{
digester.update(buffer, 0, bytesNumber);
}
}
byte[] digest = digester.digest();
result = humanReadableDigest(digest);
}
catch (java.security.NoSuchAlgorithmException exception)
{
throw new Exception("Digest method unknown.", exception);
}
catch (java.io.FileNotFoundException exception)
{
throw new Exception("File not found (" + exception.getMessage() + ")", exception);
}
catch (java.io.IOException exception)
{
throw new Exception("Error reading file.", exception);
}
finally
{
if (source != null)
{
source.close();
}
}
}
//
return (result);
}
/**
*
*/
public static String humanReadableDigest(final byte[] digest)
{
String result;
StringBuffer hashString = new StringBuffer();
for (int letterIndex = 0; letterIndex < digest.length; ++letterIndex)
{
String hex = Integer.toHexString(digest[letterIndex]);
if (hex.length() == 1)
{
hashString.append('0');
hashString.append(hex.charAt(hex.length() - 1));
}
else
{
hashString.append(hex.substring(hex.length() - 2));
}
}
result = hashString.toString();
//
return (result);
}
}

View file

@ -1,76 +0,0 @@
/**
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Never used again. Prefer org.apache.commons.io.FileUtils class.
*/
public class FileCopier
{
public static final int BUFFER_SIZE = 4 * 1024;
/**
*
*/
public static void copy(final File source, final File target) throws Exception
{
if ((source == null) || (target == null))
{
throw new Exception("Null parameter.");
}
else
{
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(target);
try
{
byte[] buffer = new byte[BUFFER_SIZE];
boolean ended = false;
while (!ended)
{
int size = in.read(buffer);
if (size == -1)
{
ended = false;
}
else
{
out.write(buffer, 0, size);
}
}
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
}
}
}

View file

@ -1,452 +0,0 @@
/**
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.File;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Pattern;
/**
*
*/
public class FileIterator extends Vector<FileIteratorState> implements Iterator<File>
{
private static final long serialVersionUID = 8790133455427427766L;
protected int currentDepth;
protected Pattern pattern;
protected File previous;
protected boolean followLinks;
/**
*
*/
public FileIterator(final File root)
{
super();
String[] pathnames;
if (root == null)
{
pathnames = null;
}
else
{
pathnames = new String[1];
pathnames[0] = root.getPath();
}
init(pathnames, null, false);
}
/**
*
*/
public FileIterator(final File root, final String filter, final boolean followLinks)
{
super();
String[] pathnames;
if (root == null)
{
pathnames = null;
}
else
{
pathnames = new String[1];
pathnames[0] = root.getPath();
}
init(pathnames, filter, followLinks);
}
/**
*
*/
public FileIterator(final String pathname, final String filter, final boolean followLinks)
{
super();
String[] pathnames;
if (pathname == null)
{
pathnames = null;
}
else
{
pathnames = new String[1];
pathnames[0] = pathname;
}
init(pathnames, filter, followLinks);
}
/**
*
*/
public FileIterator(final String[] pathnames, final String filter, final boolean followLinks)
{
super();
init(pathnames, filter, followLinks);
}
/**
*
*/
public File currentFile()
{
File result;
result = this.currentState().currentFile();
//
return (result);
}
/**
*
*/
protected FileIteratorState currentState()
{
FileIteratorState result;
result = this.get(this.currentDepth);
//
return (result);
}
/**
*
*/
public int directoryFinalCountdown()
{
int result;
result = 0;
while (this.hasNext())
{
if (this.next().isDirectory())
{
result += 1;
}
}
//
return (result);
}
/**
*
*/
public int fileFinalCountdown()
{
int result;
result = 0;
while (this.hasNext())
{
if (!this.next().isDirectory())
{
result += 1;
}
}
//
return (result);
}
/**
*
*/
protected String filter()
{
String result;
if (this.pattern == null)
{
result = ".*";
}
else
{
result = this.pattern.toString();
}
//
return (result);
}
/**
*
*/
public int finalCountdown()
{
int result;
result = 0;
while (this.next() != null)
{
result += 1;
}
//
return (result);
}
/**
*
*/
public boolean follow(final File file)
{
boolean result;
result = false;
try
{
if ((this.followLinks) || (!isLink(file)))
{
result = true;
}
else
{
result = false;
}
// System.out.println("FOLLOWWWWW=[" + file.getPath() + "][" +
// this.followLinks + "][" + isLink(file) + "][" + result + "]");
}
catch (Exception exception)
{
System.err.println("ERROR with file [" + this.next() + "]: " + exception.getMessage());
result = false;
}
//
return (result);
}
/**
*
*/
@Override
public boolean hasNext()
{
boolean result;
result = this.currentState().hasNext();
//
return (result);
}
/**
*
*/
protected void init(final String[] pathnames, final String filter, final boolean followLinks)
{
setFilter(filter);
this.followLinks = followLinks;
this.previous = null;
this.currentDepth = 0;
this.add(new FileIteratorState(pathnames));
shift();
}
/**
*
*/
@Override
public File next()
{
File result;
result = this.currentState().next();
this.previous = result;
if (result != null)
{
if (result.isDirectory())
{
this.push(result);
}
}
shift();
//
return (result);
}
/**
*
*/
public Pattern pattern()
{
Pattern result;
result = this.pattern;
//
return (result);
}
/**
*
*/
public void pop()
{
this.removeElementAt(this.currentDepth);
this.currentDepth -= 1;
}
/**
*
*/
public void push(final File file)
{
if ((file != null) && (file.isDirectory()))
{
this.add(new FileIteratorState(file.listFiles()));
this.currentDepth += 1;
}
}
/**
*
*/
@Override
public void remove()
{
if (this.previous != null)
{
this.previous.delete();
this.previous = null;
}
}
/**
*
*/
public void reset()
{
this.currentDepth = 0;
this.previous = null;
if (this.size() > 0)
{
this.get(0).reset();
FileIteratorState firstState = this.get(0);
this.removeAllElements();
this.add(firstState);
}
shift();
}
/**
*
*/
protected void setFilter(final String filter)
{
if (filter == null)
{
this.pattern = null;
}
else
{
this.pattern = Pattern.compile(filter);
}
}
/**
* Set indexes to the good next item.
*/
public void shift()
{
boolean ended = false;
while (!ended)
{
File next = this.currentFile();
if (next == null)
{
if (this.currentDepth == 0)
{
ended = true;
}
else
{
this.pop();
}
}
else
{
if (((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) && (follow(next)))
{
ended = true;
}
else
{
this.currentState().next();
if (next.isDirectory() && (follow(next)))
{
this.push(next);
}
}
}
}
}
/**
*
*/
@Override
public String toString()
{
String result;
result = "[depth=" + this.currentDepth + "][index=" + this.get(this.currentDepth).currentIndex() + "/" + this.get(this.currentDepth).files.length + "]";
//
return (result);
}
/**
*
*/
public static boolean isLink(final File file) throws Exception
{
boolean result;
if ((file.exists()) && (file.getCanonicalPath().equals(file.getAbsolutePath())))
{
result = false;
}
else
{
result = true;
}
//
return (result);
}
}

View file

@ -1,162 +0,0 @@
/**
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.File;
import java.util.Iterator;
/**
* Used by FileIterator class.
*/
public class FileIteratorState implements Iterator<File>
{
protected File[] files;
protected int currentIndex;
/**
*
*/
public FileIteratorState(final File[] files)
{
// Initialize the state.
this.currentIndex = 0;
if (files == null)
{
this.files = new File[0];
}
else
{
this.files = files;
}
}
/**
* Useful for the depth zero, otherwise parent path is lost.
*/
public FileIteratorState(final String[] pathnames)
{
// Initialize the state.
this.currentIndex = 0;
this.files = new File[pathnames.length];
for (int pathnameIndex = 0; pathnameIndex < pathnames.length; pathnameIndex++)
{
this.files[pathnameIndex] = new File(pathnames[pathnameIndex]);
}
}
/**
*
*/
protected File currentFile()
{
File result;
if (this.currentIndex >= this.files.length)
{
result = null;
}
else
{
result = this.files[this.currentIndex];
}
//
return (result);
}
/**
*
*/
protected int currentIndex()
{
int result;
result = this.currentIndex;
//
return (result);
}
/**
*
*/
protected File[] files()
{
File[] result;
result = this.files;
//
return (result);
}
/**
*
*/
@Override
public boolean hasNext()
{
boolean result;
if (this.currentFile() == null)
{
result = false;
}
else
{
result = true;
}
//
return (result);
}
/**
*
*/
@Override
public File next()
{
File result;
result = this.currentFile();
this.currentIndex += 1;
//
return (result);
}
/**
*
*/
@Override
public void remove()
{
}
/**
*
*/
public void reset()
{
this.currentIndex = 0;
}
}

View file

@ -1,635 +0,0 @@
/**
* Copyright (C) 2008-2015 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import fr.devinsy.util.strings.StringList;
import fr.devinsy.util.strings.StringListUtils;
/**
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class FileTools
{
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/**
*
*
* @param fileName
* Source.
*
* @return Extension value or null.
*/
public static String addBeforeExtension(final String fileName, final String addition)
{
String result;
if (fileName == null)
{
result = null;
}
else if (addition == null)
{
result = fileName;
}
else
{
//
int separatorIndex = fileName.lastIndexOf('.');
//
if (separatorIndex > 0)
{
result = fileName.substring(0, separatorIndex) + addition + fileName.substring(separatorIndex);
}
else
{
result = fileName + addition;
}
}
//
return result;
}
/**
*
*
* @param file
* Source.
*
* @return Extension value or null.
*/
public static File addToName(final File file, final String addition)
{
File result;
if (file == null)
{
result = null;
}
else if (addition == null)
{
result = file;
}
else
{
//
String sourceFileName = file.getAbsolutePath();
String targetFileName = addBeforeExtension(sourceFileName, addition);
result = new File(targetFileName);
}
//
return result;
}
/**
* Get the extension of a file.
*
* @param file
* Source.
*
* @return Extension value or null.
*/
public static String getExtension(final File file)
{
String result;
if (file == null)
{
result = null;
}
else
{
result = getExtension(file.getName());
}
//
return result;
}
/**
* Get the extension of a file.
*
* <ul>
* <li>getExtension(null) = null</li>
* <li>getExtension("") = null</li>
* <li>getExtension("abc") = null</li>
* <li>getExtension("abc.efg") = "efg"</li>
* </ul>
*
* @param file
* Source.
*
* @return Extension value or null.
* @deprecated See
* <code>org.apache.commons.io.FilenameUtils.getExtension</code>
*/
@Deprecated
public static String getExtension(final String fileName)
{
String result;
if (fileName == null)
{
result = null;
}
else
{
int separatorIndex = fileName.lastIndexOf('.');
if (separatorIndex > 0)
{
result = fileName.substring(separatorIndex + 1).toLowerCase();
}
else
{
result = null;
}
}
//
return result;
}
/**
*
* @param file
* @return
* @throws IOException
*/
public static String load(final File source) throws IOException
{
String result;
result = load(source, DEFAULT_CHARSET_NAME);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static String load(final File source, final String charsetName) throws IOException
{
String result;
result = loadToStringBuffer(source, charsetName).toString();
//
return result;
}
/**
*
* @param file
* @return
* @throws IOException
*/
public static String load(final URL source) throws IOException
{
String result;
result = load(source, DEFAULT_CHARSET_NAME);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static String load(final URL source, final String charsetName) throws IOException
{
String result;
//
StringBuffer buffer = new StringBuffer(source.openConnection().getContentLength() + 1);
read(buffer, source.openStream(), charsetName);
//
result = buffer.toString();
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringList loadStringList(final File source) throws IOException
{
StringList result;
result = loadStringList(source, DEFAULT_CHARSET_NAME);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringList loadStringList(final File file, final String charsetName) throws IOException
{
StringList result;
result = StringListUtils.load(file, charsetName);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringBuffer loadToStringBuffer(final File source) throws IOException
{
StringBuffer result;
result = loadToStringBuffer(source, DEFAULT_CHARSET_NAME);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringBuffer loadToStringBuffer(final File file, final String charsetName) throws IOException
{
StringBuffer result;
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
boolean ended = false;
final String LINE_SEPARATOR = System.getProperty("line.separator");
result = new StringBuffer((int) file.length() + 1);
while (!ended)
{
String line = in.readLine();
if (line == null)
{
ended = true;
}
else
{
result.append(line).append(LINE_SEPARATOR);
}
}
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringList loadToStringList(final File source) throws IOException
{
StringList result;
result = loadToStringList(source, DEFAULT_CHARSET_NAME);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringList loadToStringList(final File file, final String charsetName) throws IOException
{
StringList result;
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
boolean ended = false;
final String LINE_SEPARATOR = System.getProperty("line.separator");
result = new StringList();
while (!ended)
{
String line = in.readLine();
if (line == null)
{
ended = true;
}
else
{
result.append(line).append(LINE_SEPARATOR);
}
}
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
//
return result;
}
/**
*
* @param file
* @return
* @throws IOException
*/
public static StringList loadToStringList(final URL source) throws IOException
{
StringList result;
result = loadToStringList(source, DEFAULT_CHARSET_NAME);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static StringList loadToStringList(final URL source, final String charsetName) throws IOException
{
StringList result;
//
result = StringListUtils.load(source, charsetName);
//
return result;
}
/**
*
* @param file
* @throws IOException
*/
public static void read(final StringBuffer out, final InputStream is, final String charsetName) throws IOException
{
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(is, charsetName));
boolean ended = false;
final String LINE_SEPARATOR = System.getProperty("line.separator");
while (!ended)
{
String line = in.readLine();
if (line == null)
{
ended = true;
}
else
{
out.append(line).append(LINE_SEPARATOR);
}
}
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
}
/**
*
* @param source
* @param extension
* @return
*
* @deprecated See
* <code>org.apache.commons.io.FilenameUtils.removeExtension</code>
*/
@Deprecated
public static String removeExtension(final String source)
{
String result;
if (source == null)
{
result = source;
}
else
{
int separatorIndex = source.lastIndexOf('.');
//
if (separatorIndex > 0)
{
result = source.substring(0, separatorIndex);
}
else
{
result = source;
}
}
//
return result;
}
/**
*
* @param file
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public static void save(final File file, final String source) throws UnsupportedEncodingException, FileNotFoundException
{
PrintWriter out = null;
try
{
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_CHARSET_NAME));
out.println(source);
}
finally
{
if (out != null)
{
out.close();
}
}
}
/**
*
* @param file
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public static void save(final File file, final StringBuffer source) throws UnsupportedEncodingException, FileNotFoundException
{
save(file, source.toString());
}
/**
*
* @param file
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public static void save(final File file, final StringList source) throws UnsupportedEncodingException, FileNotFoundException
{
StringListUtils.save(file, source);
}
/**
*
* @param source
* @param extension
* @return
*/
public static File setExtension(final File source, final String extension)
{
File result;
if ((source == null) || (extension == null))
{
result = source;
}
else
{
result = new File(setExtension(source.getAbsolutePath(), extension));
}
//
return result;
}
/**
*
* @param source
* @param extension
* @return
*/
public static String setExtension(final String source, final String extension)
{
String result;
if ((source == null) || (extension == null))
{
result = source;
}
else
{
int separatorIndex = source.lastIndexOf('.');
//
if (separatorIndex > 0)
{
String prefix = source.substring(0, separatorIndex);
if (prefix.endsWith(extension))
{
result = prefix;
}
else
{
result = prefix + extension;
}
}
else
{
result = source + extension;
}
}
//
return result;
}
}

View file

@ -1,188 +0,0 @@
/**
* Copyright (C) 2009-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
/**
* Useful for display beautiful percentage value as string.
*
* @author cpm
*/
public class Fraction
{
protected long numerator;
protected long denominator;
/**
*
*/
public Fraction(final long numerator, final long denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
/**
*
*/
public long denominator()
{
long result;
result = this.denominator;
//
return (result);
}
/**
*
*/
public long numerator()
{
long result;
result = this.numerator;
//
return (result);
}
/**
*
*/
public long percentage() throws Exception
{
long result;
result = percentage(this.numerator, this.denominator);
//
return (result);
}
/**
*
*/
public String percentageFullString()
{
String result;
result = percentageFullString(this.numerator, this.denominator);
//
return (result);
}
/**
*
*/
public String percentageString()
{
String result;
result = percentageString(this.numerator, this.denominator);
//
return (result);
}
/**
*
*/
@Override
public String toString()
{
String result;
result = this.numerator + "/" + this.denominator;
//
return (result);
}
/**
*
*/
public static long percentage(final long numerator, final long denominator) throws Exception
{
long result;
if (denominator == 0)
{
throw new Exception("denominator is zero");
}
else
{
result = Math.round(numerator * 100 / denominator);
}
//
return (result);
}
/**
*
*/
public static String percentageFullString(final long numerator, final long denominator)
{
String result;
result = percentageString(numerator, denominator);
//
return (result);
}
/**
*
*/
public static String percentageString(final long numerator, final long denominator)
{
String result;
try
{
long value = percentage(numerator, denominator);
if (numerator == 0)
{
result = "0%";
}
else if (value == 0)
{
result = "~0%";
}
else if (value < 10)
{
result = "0" + value + "%";
}
else
{
result = value + "%";
}
}
catch (Exception exception)
{
result = "--%";
}
//
return (result);
}
}

View file

@ -1,241 +0,0 @@
/**
* Copyright (C) 2009-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
/**
*
*/
public class InternetProxyConfiguration
{
//
protected String host;
protected int port;
protected String login;
protected String password;
/**
*
*/
public InternetProxyConfiguration()
{
this.host = "";
this.port = 0;
this.login = "";
this.password = "";
}
/**
*
*/
public InternetProxyConfiguration(final String host, final int port, final String login, final String password)
{
//
if (host == null)
{
this.host = "";
}
else
{
this.host = host;
}
//
this.port = port;
//
if (login == null)
{
this.login = "";
}
else
{
this.login = login;
}
//
if (password == null)
{
this.password = "";
}
else
{
this.password = password;
}
}
/**
*
*/
public InternetProxyConfiguration(final String host, final String port, final String login, final String password) throws Exception
{
//
if (host == null)
{
this.host = "";
}
else
{
this.host = host;
}
//
if ((port == null) || (port.trim().length() == 0))
{
this.port = 0;
}
else
{
try
{
this.port = Integer.parseInt(port);
}
catch (Exception exception)
{
String errorMessage = "Incorrect PROXY port value.";
throw new Exception(errorMessage, exception);
}
}
//
if (login == null)
{
this.login = "";
}
else
{
this.login = login;
}
//
if (password == null)
{
this.password = "";
}
else
{
this.password = password;
}
}
/**
*
*/
public String host()
{
String result;
result = this.host;
//
return (result);
}
/**
*
*/
public boolean isInitialized()
{
boolean result;
if ((this.host.length() > 0) && (this.port > 0))
{
result = true;
}
else
{
result = false;
}
//
return (result);
}
/**
*
*/
public String login()
{
String result;
result = this.login;
//
return (result);
}
/**
*
*/
public String password()
{
String result;
result = this.password;
//
return (result);
}
/**
*
*/
public int port()
{
int result;
result = this.port;
//
return (result);
}
/**
*
*/
@Override
public String toString()
{
String result;
String login;
if (this.login.length() == 0)
{
login = "";
}
else
{
login = "********";
}
String password;
if (this.password.length() == 0)
{
password = "";
}
else
{
password = "********";
}
result = "(" + this.host + "," + this.port + "," + login + "," + password + ")";
//
return (result);
}
}

View file

@ -1,133 +0,0 @@
/**
* Copyright (C) 2009-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
/**
* This class defines a simple average manager. For example, it is useful for
* millisecond. The maximum value available in input is one day in millisecond.
*/
public class SimpleAveragemeter
{
//
protected long sum;
protected long cardinal;
protected long MAX_ADD = 1 * 24 * 60 * 60 * 1000; // One day in millisecond.
/**
*
*/
public SimpleAveragemeter()
{
this.reset();
}
/**
*
*/
synchronized public void add(final long value)
{
// Manage the sum limit.
if ((this.sum > Long.MAX_VALUE / 2) && (this.cardinal % 2 == 0))
{
this.sum = this.sum / 2;
this.cardinal = this.cardinal / 2;
}
// Add the new value.
if (this.sum > this.MAX_ADD)
{
this.sum += this.MAX_ADD;
this.cardinal += 1;
}
else
{
this.sum += value;
this.cardinal += 1;
}
}
/**
*
*/
synchronized public long average()
{
long result;
if (this.cardinal == 0)
{
result = 0;
}
else
{
result = this.sum / this.cardinal;
}
//
return (result);
}
/**
*
*/
public long cardinal()
{
long result;
result = this.cardinal;
//
return (result);
}
/**
*
*/
synchronized public void reset()
{
this.sum = 0;
this.cardinal = 0;
}
/**
*
*/
@Override
public String toString()
{
String result;
result = Long.toString(this.average());
//
return (result);
}
/**
*
*/
public long value()
{
long result;
result = this.average();
//
return (result);
}
}

View file

@ -1,164 +0,0 @@
/**
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.util.Date;
/**
*
*/
public class SimpleChronometer
{
//
protected long firstTime;
/**
*
*/
public SimpleChronometer()
{
this.reset();
}
/**
*
*/
public long interval()
{
long result;
result = new Date().getTime() - this.firstTime;
//
return (result);
}
/**
*
*/
public void reset()
{
this.firstTime = new Date().getTime();
}
/**
* TO BE COMPLETED.
*/
public static String humanString(final long interval)
{
String result;
if (interval < 1000)
{
result = interval + "ms";
}
else if (interval < 60 * 1000)
{
result = interval / 1000 + "," + interval % 1000 + "s";
}
else if (interval < 60 * 60 * 1000)
{
result = interval / 1000 + "," + interval % 1000 + "s";
}
else if (interval < 24 * 60 * 60 * 1000)
{
result = interval / 1000 + "," + interval % 1000 + "s";
}
else if (interval < 7 * 24 * 60 * 60 * 1000)
{
result = interval / 1000 + "," + interval % 1000 + "s";
}
else
// if (interval < 7*24*60*60*1000)
{
result = interval / 1000 + "," + interval % 1000 + "s";
}
//
return (result);
}
/**
*
*/
public static String shortHumanString(final long interval)
{
String result;
if (interval < 1000)
{
result = interval + " ms";
}
else if (interval < 2 * 1000)
{
result = interval / 1000 + " seconde";
}
else if (interval < 60 * 1000)
{
result = interval / 1000 + " secondes";
}
else if (interval < 2 * 60 * 1000L)
{
result = interval / (60 * 1000L) + " minute";
}
else if (interval < 60 * 60 * 1000L)
{
result = interval / (60 * 1000L) + " minutes";
}
else if (interval < 2 * 60 * 60 * 1000L)
{
result = interval / (60 * 60 * 1000L) + " heure";
}
else if (interval < 24 * 60 * 60 * 1000L)
{
result = interval / (60 * 60 * 1000L) + " heures";
}
else if (interval < 2 * 24 * 60 * 60 * 1000L)
{
result = interval / (24 * 60 * 60 * 1000L) + " jour";
}
else if (interval < 7 * 24 * 60 * 60 * 1000L)
{
result = interval / (24 * 60 * 60 * 1000L) + " jours";
}
else if (interval < 2 * 7 * 24 * 60 * 60 * 1000L)
{
result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaine";
}
else if (interval < 30 * 24 * 60 * 60 * 1000L)
{
result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaines";
}
else if (interval < 52 * 7 * 24 * 60 * 60 * 1000L)
{
result = interval / (30 * 24 * 60 * 60 * 1000L) + " mois";
}
else if (interval < 2 * 52 * 7 * 24 * 60 * 60 * 1000L)
{
result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " année";
}
else
{
result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " années";
}
//
return (result);
}
}

View file

@ -1,47 +0,0 @@
/**
* Copyright (C) 2010, 2013-2016 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
/**
* @deprecated use <code>SLF4J.Logger.error("blabla", exception)</code> method
* or the ExceptionUtils.getStackTrace(throwable).
*/
@Deprecated
public class StacktraceWriter
{
/**
* @deprecated use <code>SLF4J.Logger.error("blabla", exception)</code>
* method or the ExceptionUtils.getStackTrace(throwable).
*/
@Deprecated
public static String toString(final Exception exception)
{
String result;
ByteArrayOutputStream out = new ByteArrayOutputStream(50000);
exception.printStackTrace(new PrintStream(out));
result = out.toString();
//
return (result);
}
}

View file

@ -1,319 +0,0 @@
/**
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import fr.devinsy.util.strings.StringList;
/**
*
* @author christian.momon@devinsy.fr
*/
public class ToolBox
{
public static String clean(final String source)
{
String result;
result = source.replaceAll("[^\\w ]", " ");
//
return result;
}
/**
* Returns information about the calling class of a calledClass.
*
* @param calledClassName
* the class name which is the subject of the search.
*
* @return information about the calling class.
*/
public static StackTraceElement getCaller(final String calledClassName)
{
StackTraceElement result;
//
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
// System.out.println("////////////////////////////");
// for (int i = 0; (i < stack.length) && (i < 4); i++) {
// System.out.println(i + " " + stack[i].getClassName());
// }
// Search for first entry of class called.
boolean ended = false;
Integer indexOfCalled = null;
int depth = 1;
while (!ended)
{
if (depth < stack.length)
{
String currentClassName = stack[depth].getClassName();
if (currentClassName.equals(calledClassName))
{
ended = true;
indexOfCalled = Integer.valueOf(depth);
}
else
{
depth += 1;
}
}
else
{
ended = true;
result = null;
}
}
// Search for caller of class called.
if (indexOfCalled == null)
{
result = null;
}
else
{
result = null;
ended = false;
depth = indexOfCalled;
while (!ended)
{
if (depth < stack.length)
{
String currentClassName = stack[depth].getClassName();
if (currentClassName.equals(calledClassName))
{
depth += 1;
}
else
{
ended = true;
result = stack[depth];
}
}
else
{
ended = true;
result = null;
}
}
}
//
return result;
}
/**
*
* @param pattern
* @param source
* @return
*/
public static int indexOf(final String pattern, final List<String> source)
{
int result;
if (source == null)
{
result = -1;
}
else
{
boolean ended = false;
result = -1;
int currentIndex = 0;
while (!ended)
{
if (currentIndex < source.size())
{
String sourceString = source.get(currentIndex);
if (StringUtils.equals(sourceString, pattern))
{
ended = true;
result = currentIndex;
}
else
{
currentIndex += 1;
}
}
else
{
ended = true;
currentIndex = -1;
}
}
}
//
return result;
}
/**
*
* @param string
* @param targets
* @return
*/
public static boolean matchesAny(final String string, final String... targets)
{
boolean result;
if ((string == null) || (targets == null))
{
result = false;
}
else
{
//
boolean ended = false;
int index = 0;
result = false;
while (!ended)
{
if (index < targets.length)
{
if (StringUtils.equals(string, targets[index]))
{
ended = true;
result = true;
}
else
{
index += 1;
}
}
else
{
ended = true;
result = false;
}
}
}
//
return result;
}
/**
*
* @return
*/
public static Double[] sort(final Set<Double> source)
{
Double[] result;
if (source == null)
{
result = null;
}
else
{
result = new Double[source.size()];
source.toArray(result);
Arrays.sort(result);
}
//
return result;
}
/**
* Concatenates int values from an array, adding decoration strings.
*
* @param values
* Source of int values.
* @param prefix
* Decoration to put on start.
* @param separator
* Decoration to put between values.
* @param postfix
* Decoration to put on end.
*
* @return A decorated string representing the int values.
*/
public static String toString(final int[] values, final String prefix, final String separator, final String postfix)
{
String result;
StringList buffer = new StringList();
//
if (prefix != null)
{
buffer.append(prefix);
}
//
boolean firstPassed = false;
for (int value : values)
{
if (firstPassed)
{
buffer.append(separator);
}
else
{
firstPassed = true;
}
buffer.append(value);
}
//
if (postfix != null)
{
buffer.append(postfix);
}
//
result = buffer.toString();
//
return result;
}
/**
*
* @param source
* @return
*/
public static String toString(final String source)
{
String result;
if (source == null)
{
result = "";
}
else
{
result = source;
}
//
return result;
}
}

View file

@ -1,88 +0,0 @@
/**
* Copyright (C) 2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
import java.io.IOException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import fr.devinsy.util.strings.StringList;
/**
*
* @author Christian P. Momon
*/
public class FileToolsTest
{
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(FileToolsTest.class);
/**
*
*/
@Before
public void before()
{
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR);
}
/**
*
*/
@Test
public void loadToStringListURL01() throws IOException
{
//
logger.debug("===== test starting...");
//
StringList source = FileTools.loadToStringList(FileTools.class.getResource("/fr/devinsy/util/lines.txt"));
//
Assert.assertEquals(4, source.size());
Assert.assertEquals("trois", source.get(3 - 1));
//
logger.debug("===== test done.");
}
/**
* @throws IOException
*
*/
@Test
public void testGetExtension()
{
//
logger.debug("===== test starting...");
//
String extension = FileTools.getExtension("test.ext");
//
Assert.assertEquals(extension, "ext");
//
logger.debug("===== test done.");
}
}

View file

@ -1,4 +0,0 @@
un
deux
trois
quatre