Pages

How to log at the correct level

TRACE This is the lowest logging level. This should be used for tracing activities such as method invocation and parameter logging, etc.
DEBUG This log level is commonly used for application debugging statements. This level should be used to log messages which are helpful in debugging, but which are too verbose to be written to the log under normal situations.
INFO This is the information log level. This should be used to log statements which are expected to be written to the log during the normal operations. For example, running a important business operation should be logged under this level.
WARN Warning Level. This should be used to log warnings, which are situations that are usually unexpected, but which does not significantly affect the business functionality.
ERROR Error level should be used to log all errors and exceptions which affect the business functionality of the application.
FATAL Fatal log level is used to write extreme situations where the entire application execution could be affected due to some cause.

Deep copy object

import java.io.*;

public class Clone {
/**
* Returns a deep of the object, or null if the object cannot
* be serialized.
*/
public static Object deep(final Object orig) {
Object obj = null;

try {

// Write the object out to a byte array
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(bos);

out.writeObject(orig);
out.flush();
out.close();

// Make an input stream from the byte array and read
// a deep of the object back in.
final ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(bos.toByteArray()));

obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}

return obj;
}
}

The Downside Of Working At Home

The Downside Of Working At Home: "
I've been working at home off and (mostly) on for 16 years...

From theoatmeal.com. Recommended!

Probabilistic choice

Given a mapping between items and their required probability of occurrence, generate a million items randomly subject to the given probabilities and compare the target probability of occurrence versus the generated values.

The total of all the probabilities should equal one. (Because floating point arithmetic is involved this is subject to rounding errors).

package NonUnitTest;

class Probabilistic {
static long TRIALS = 1000000;
static Expv[] items =
{ new Expv("aleph", 0, 0.0, 0.0), new Expv("beth", 0, 0.0, 0.0),
new Expv("gimel", 0, 0.0, 0.0),
new Expv("daleth", 0, 0.0, 0.0), new Expv("he", 0, 0.0, 0.0),
new Expv("waw", 0, 0.0, 0.0),
new Expv("zayin", 0, 0.0, 0.0), new Expv("heth", 0, 0.0, 0.0) };

//~--- METHODS

public static void main(String[] args) {
int i, j;
double rnum,
tsum = 0.0;

for (i = 0, rnum = 5.0; i < 7; i++, rnum += 1.0) {
items[i].expect = 1.0 / rnum;
tsum += items[i].expect;
}

items[7].expect = 1.0 - tsum;
items[0].mapping = 1.0 / 5.0;

for (i = 1; i < 7; i++) {
items[i].mapping = items[i - 1].mapping + 1.0 / ((double) i + 5.0);
}

items[7].mapping = 1.0;

for (i = 0; i < TRIALS; i++) {
rnum = Math.random();

for (j = 0; j < 8; j++) {
if (rnum < items[j].mapping) {
items[j].probcount++;

break;
}
}
}

System.out.printf("Trials: %d\n", TRIALS);
System.out.printf("Items: ");

for (i = 0; i < 8; i++) {
System.out.printf("%-8s ", items[i].name);
}

System.out.printf("\nTarget prob.: ");

for (i = 0; i < 8; i++) {
System.out.printf("%8.6f ", items[i].expect);
}

System.out.printf("\nAttained prob.: ");

for (i = 0; i < 8; i++) {
System.out.printf("%8.6f ",
(double) (items[i].probcount) / (double) TRIALS);
}

System.out.printf("\n");
}

//~--- INNER CLASSES

private static class Expv {
public double expect;
public double mapping;
public String name;
public int probcount;

//~--- CONSTRUCTORS

public Expv(String name, int probcount, double expect, double mapping) {
this.name = name;
this.probcount = probcount;
this.expect = expect;
this.mapping = mapping;
}
}
}

Knuth Shuffle (aka the Fisher-Yates shuffle)

Create a random permutation of an array.
import java.util.Random;

public static final Random gen = new Random();

// version for array of ints
public static void shuffle (int[] array) {
int n = array.length;
while (n > 1) {
//decrements after using the value
int k = gen.nextInt(n--);
int temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}

// version for array of references
public static void shuffle (Object[] array) {
int n = array.length;
while (n > 1) {
//decrements after using the value
int k = gen.nextInt(n--);
Object temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}