Thursday, May 27, 2010

Modificator for @Autowire

Is it the same?
@Autowired
public indentifier name
@Autowired
private indentifier name

1. Speed issuer. With a private modificator reflection algorith adds only one additional call which is nano seconds time.
name.setAccessible(true);
2. But with a private modificator you don't break object encapsulation

Wednesday, December 23, 2009

SLF4J

SLF4J-simple can't be configured via log4j.properties. To configure it use slf4j-log4j12

org.slf4j
slf4j-log4j12
1.5.8

Tuesday, November 10, 2009

WINDOW resize handler

public class XXX implements ResizeHandler{
public XXX(){
Window.addResizeHandler(this);
}

@Override
public void onResize(ResizeEvent event) {
//TODO YOUR CODE AFTER RESIZING
}

}

Tuesday, September 29, 2009

Date Formatter

Is the ParseException happens?

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
try {
//1
date = df.parse("12-34-2007");
} catch (ParseException e) {
System.out.println("Wrong date");
}
System.out.println(date.toString());

Actually not. To prevent this add to //1
df.setLenient(false);

Letient parameter is true by default that's why to match format strictly need
setting parameter to false

Sunday, September 20, 2009

Monday, May 25, 2009

Converting image

As our Designer was fired because of crisis - I had to change images size. So imagemagic is excelent for this use.

mogrify -gravity Center -crop 30X22+0+0 *.png
mogrify -format png *.jpg
mogrify -resize 30x22+0+0 *.*
mogrify -resize 120% *.*

identify -format "%wx%h \n" *.*

Friday, May 8, 2009

Hibernate Custom Type

Database has for example wrong state and can't save 1 or 0, 'Y' or 'N', 'YES' or 'NO'
So you need to save state as 1 and 2. Seems that database need refactoring... yes you are right but if you can't? Here is decision - custom type

package com.types;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class OneTwoBooleanType implements UserType{

private final static int DB_TRUE = 1;
private final static int DB_FALSE = 2;

public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}

public Object deepCopy(Object value) throws HibernateException {
return value;
}

public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}

public boolean equals(Object x, Object y) throws HibernateException {
if(x==null){
return false;
}
return ((Boolean)x).equals(y);
}

public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}

public boolean isMutable() {
return false;
}

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
Number val = (Number) rs.getObject(names[0]);
if(val==null){
return false;
}
return val.intValue() == DB_TRUE ? true : false;
}

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
int dbValue;
if(value==null){
dbValue = DB_FALSE;
} else {
dbValue = (Boolean) value ? DB_TRUE : DB_FALSE;
}
st.setInt(index, dbValue);
}

public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}

public Class returnedClass() {
return Boolean.class;
}

public int[] sqlTypes() {
return new int[]{Types.SMALLINT};
}

}

And It's usage

@Column(name="INTERNAL_STATUS_ID")
@Type(type="com.types.OneTwoBooleanType")
private boolean isVisible;