package commons.enumerate; import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Enumerate implements Comparable,Serializable { private static final long serialVersionUID = 1L; private int m_ordinal; private String m_name; public Enumerate(){ } protected Enumerate(String name, int ordinal) { m_ordinal = ordinal; m_name = name; } private void checkClass(Class other) { if(!other.equals(getClass())) { throw new ClassCastException("Couldn't compare a " + other.getClass() + " to " + this.getClass()); } } public int compareTo(Object o) { checkClass(o.getClass()); return ((Enumerate) o).getOrdinal() - getOrdinal(); } @Override public boolean equals(Object o) { if(null == o){ return false; } if(o instanceof String) return getName().equals(o); if(o.getClass().equals(getClass())) return equals(((Enumerate)o).getOrdinal()); return super.equals(o); } public boolean equals(int value) { return getOrdinal() == value; } public String getName() { return m_name; } public byte getOrdinal() { return (byte)m_ordinal; } private static Enumerate getNullValue(Class theClass) throws NullPointerException { // Cerca il campo marcato con il nome di NULLO Field fields[] = theClass.getFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if(field.getName().equalsIgnoreCase("NULLO")) { try { return (Enumerate)field.get(null); } catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} } } throw new NullPointerException("Object NULLO not found in class " + theClass.getName()); } /** * @param nclass La classe enumerata su cui fare la ricerca * @param b Il valore dell'ordinale da ricercare * @return Il valore dell'enumerato,null se non esiste */ public static Enumerate getValue(Class nclass, int b) { try { Method metodo = nclass.getMethod("values", (Class[])null); Enumerate[] enums = (Enumerate[]) metodo.invoke(null, (Object[])null); for (int i = 0; i < enums.length; i++) { if (enums[i].getOrdinal() == b) { return enums[i]; } } return getNullValue(nclass); } catch (SecurityException e) { throw new InvalidEnumerateException(nclass); } catch (IllegalArgumentException e) { throw new InvalidEnumerateException(nclass); } catch (NoSuchMethodException e) { throw new InvalidEnumerateException(nclass); } catch (IllegalAccessException e) { throw new InvalidEnumerateException(nclass); } catch (InvocationTargetException e) { throw new InvalidEnumerateException(nclass); } } /** * @param nclass La classe enumerata su cui fare la ricerca * @param b Il valore testuale su cui fare la ricerca;se ? un valore numerico in formato * stringa allora viene usata la versione di ricerca sull'ordinale,altrimenti quella * di ricerca sul nome * @return Il valore dell'enumerato,null se non esiste */ public static Enumerate getValue(Class theClass,String value) { try { // Se la stringa ? un numero allora utilizza la versione che ricerca l'ordinale return getValue(theClass,Integer.parseInt(value)); } catch(NumberFormatException nfe) { } try { Method metodo = theClass.getMethod("values", (Class[])null); Enumerate[] enums = (Enumerate[]) metodo.invoke(null, (Object[])null); for (int i = 0; i < enums.length; i++) { if (enums[i].getName().equals(value)) { return enums[i]; } } return getNullValue(theClass); } catch (SecurityException e) { throw new InvalidEnumerateException(theClass); } catch (IllegalArgumentException e) { throw new InvalidEnumerateException(theClass); } catch (NoSuchMethodException e) { throw new InvalidEnumerateException(theClass); } catch (IllegalAccessException e) { throw new InvalidEnumerateException(theClass); } catch (InvocationTargetException e) { throw new InvalidEnumerateException(theClass); } } @Override public String toString(){ return String.valueOf(getOrdinal()); } @Override public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + new Integer(m_ordinal).hashCode(); return result; } }