Design Pattern |
|
|
Singleton con inizializzazione lazy e thread-safe
public class Singleton {
/**
* Costruttore privato, in quanto la creazione dell'istanza deve essere controllata.
*/
private Singleton() {}
/**
* La classe Contenitore viene caricata/inizializzata alla prima esecuzione di getInstance()
* ovvero al primo accesso a Contenitore.ISTANZA,
* ed in modo thread-safe.
* Anche l'inizializzazione dell'attributo statico, pertanto, viene serializzata.
*/
private static class… |
|
public interface PlaceVisitor {
void visit(Museum museum);
void visit(Church church);
void visit(City city);
}
public interface Place {
void accept(PlaceVisitor visitor);
String getName();
}
public class Museum implements Place{
private String name;
private String street;
public Museum(String name, String street) {
this.name = name;
this.street = street;
}
@Override
public void accept(PlaceVisitor visitor) {
visitor.visit(t… |
CSV |
Come leggere e scrivere un file CSV in Java
Usando JavaCSV
Read CSV from file
/*
File Format:
-----------
ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued
1,Chai,1,1,10 boxes x 20 bags,18,39,0,10,FALSE
2,Chang,1,1,24 - 12 oz bottles,19,17,40,25,FALSE
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import com.csvreader.CsvReader;
public class CsvReaderExample {
public static void main(Str… |
Date & Time |
Ottenere la data attuale come stringa in Java
How to get current date time in Java as a string
Calendar now = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss z dd/MM/yyyy");
String stringDate = sdf.format(now.getTime());
// 20:00:52 CEST 13/05/2011 |
File & Directory |
Come creare un struttura di directory da un path in Java
How to create directory and all sub-directories in Java
Logger logger = LoggerFactory.getLogger(MyClass.class);
String path = "C:/files/docs";
File myFolder = new File(path);
if (!myFolder.exists()) {
if (!myFolder.mkdirs()) {
logger.error("Cannot create folders at '{}'", myFolder.getPath());
} else {
logger.info("Folders created at '{}'", myFolder.getPath());
}
} |
Invocare un processo asincrono in Java |
Asynchronous processing in Java
Classe Runnable:
public class MyThread implements Runnable {
private String myPath;
public MyThread (String myPath) {
this.myPath= myPath;
}
public void run() {
try {
...
} catch (Exception ex) {
...
}
}
} |
Regular Expressions in Java |
Come dividere una stringa in un array quando viene trovata una lettera maiuscola
Split string when an uppercase letter is found
String str = "stringaDaDividere";
String[] strArray = str.split("(?=\\p{Lu})"); // espressione regolare
/*
Produrrà un array con i seguenti elementi:
- stringa
- Da
- Dividere
*/ |
Spring |
Richiamare un metodo di una classe mediante bean spring e ottenere un valore per poi usarlo in un altro bean
com.vincenzodevivo.Config
getProperty
connection.url
String Utils |
|
Pattern UpperCase/LowerCase
public static String patternLowerCase(String pattern, String string) {
if (string == null){
return null;
}
Matcher m = Pattern.compile(pattern).matcher(string);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group().toLowerCase());
}
m.appendTail(sb);
return sb.toString();
}
public static String patternUpperCase(String pattern, Stri… |
Info Varie |
Metodo con un numero variabile di argomenti in Java
Method with a variable number of arguments
public String appendString(String str, String... params) {
String ret = str;
for (String param: params) {
ret+= param;
}
return ret;
} |